hellogroup

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
hellogroup [2013/09/25 13:54]
mroriz [Execution]
hellogroup [2013/09/25 13:58]
mroriz [HelloGroupDefinerClient]
Line 453: Line 453:
 ===== HelloGroupDefinerClient ===== ===== HelloGroupDefinerClient =====
  
 +Our client will act slightly the same as the ''HelloGroupClient''. With the difference, that we now send messages to the group type 3, and groups ID 1, 2, and 3. The ''HelloGroupDefinerEvenReceiver'' will receives the messages of group ID 1 and 2, while the ''HelloGroupDefinerOddReceiver'' will receives the messages of groups ID 1 and 3. The complete code of our client is shown below:
 +
 +<file java HelloGroupClient.java>
 +package br.pucrio.inf.lac.helloworld;
 +
 +import java.io.IOException;
 +import java.net.InetSocketAddress;
 +import java.net.SocketAddress;
 +import java.util.List;
 +import java.util.logging.Level;
 +import java.util.logging.Logger;
 +
 +import lac.cnclib.net.NodeConnection;
 +import lac.cnclib.net.NodeConnectionListener;
 +import lac.cnclib.net.groups.Group;
 +import lac.cnclib.net.groups.GroupCommunicationManager;
 +import lac.cnclib.net.groups.GroupMembershipListener;
 +import lac.cnclib.net.mrudp.MrUdpNodeConnection;
 +import lac.cnclib.sddl.message.ApplicationMessage;
 +import lac.cnclib.sddl.message.Message;
 +
 +public class HelloGroupClient implements NodeConnectionListener, GroupMembershipListener {
 +
 +  private final static String        gatewayIP    = "127.0.0.1";
 +  private final static int           gatewayPort  = 5500;
 +  private GroupCommunicationManager  groupManager;
 +
 +  public HelloGroupClient() {
 +    InetSocketAddress address = new InetSocketAddress(gatewayIP, gatewayPort);
 +    try {
 +      MrUdpNodeConnection connection = new MrUdpNodeConnection();
 +      connection.connect(address);
 +      connection.addNodeConnectionListener(this);
 +
 +      try {
 +        Thread.sleep(30000);
 +      } catch (InterruptedException e) {
 +        e.printStackTrace();
 +      }
 +    } catch (IOException e) {
 +      e.printStackTrace();
 +    }
 +  }
 +
 +  public static void main(String[] args) {
 +    Logger.getLogger("").setLevel(Level.OFF);
 +
 +    new HelloGroupClient();
 +  }
 +
 +  @Override
 +  public void connected(NodeConnection remoteCon) {
 +    groupManager = new GroupCommunicationManager(remoteCon);
 +    groupManager.addMembershipListener(this);
 +
 +    try {
 +      // i = 1 Default
 +      // i = 2 EVEN
 +      // i = 3 ODD
 +      for (int i = 1; i < 4; i++) {
 +        Group group = new Group(3, i);
 +
 +        ApplicationMessage appMsg = new ApplicationMessage();
 +        appMsg.setContentObject("Message GroupID: " + i);
 +        groupManager.sendGroupcastMessage(appMsg, group);
 +      }
 +    } catch (IOException e) {
 +      e.printStackTrace();
 +    }
 +  }
 +
 +  @Override
 +  public void reconnected(NodeConnection remoteCon, SocketAddress endPoint, boolean wasHandover, boolean wasMandatory) {}
 +
 +  @Override
 +  public void disconnected(NodeConnection remoteCon) {}
 +
 +  @Override
 +  public void newMessageReceived(NodeConnection remoteCon, Message message) {
 +    System.out.println(message.getContentObject());
 +  }
 +
 +  @Override
 +  public void unsentMessages(NodeConnection remoteCon, List<Message> unsentMessages) {}
 +
 +  @Override
 +  public void internalException(NodeConnection remoteCon, Exception e) {}
 +
 +  @Override
 +  public void enteringGroups(List<Group> groups) {}
 +
 +  @Override
 +  public void leavingGroups(List<Group> groups) {}
 +}
 +
 +</file>
 +
 +===== HelloGroupDefinerExecution =====
  
  • hellogroup.txt
  • Last modified: 2017/07/21 03:08
  • (external edit)