hellocore

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
Previous revision
hellocore [2016/04/24 17:29]
mroriz [HelloCore]
hellocore [2016/04/26 15:18]
mroriz
Line 1: Line 1:
 ====== HelloCore ====== ====== HelloCore ======
 {{ :hellocoreext.png?nolink&400|}} {{ :hellocoreext.png?nolink&400|}}
-This tutorial will guide you through the basic concepts and programming involved in the communication between a mobile node (MN) and a stationary node within the SDDL core network [[architecture|]]. This stationary node of the SDDL core will play the role of a server processing node, capable of processing application messages from the mobile node (MN)  according to some application specific logic, and sending messages back to the MN. Hence, this tutorial will cover both paths of the communication, //i.e.//, from the MN to the server processing node, and from the stationary node to the MN.+This tutorial will guide you through the basic concepts and programming involved in the communication between a mobile node (MN) and a stationary node within the ContextNet SDDL core network [[architecture|]]. This stationary node of the SDDL core will play the role of a server processing node, capable of processing application messages from the mobile node (MN)  according to some application specific logic, and sending messages back to the MN. Hence, this tutorial will cover both paths of the communication, //i.e.//, from the MN to the server processing node, and from the stationary node to the MN.
  
 Our sample application is composed of two components: a mobile client node and a stationary node, as illustrated in the figure on the right. The MN creates a ''Hello World'' message which is sent to the SDDL core. Our processing node receives this message and execute a simple logic, keeps track on the number of messages received and prints them on the screen. To exemplify the other path of communication, the processing node sends a reply message to the MN containing the number of message that it received so far.  Our sample application is composed of two components: a mobile client node and a stationary node, as illustrated in the figure on the right. The MN creates a ''Hello World'' message which is sent to the SDDL core. Our processing node receives this message and execute a simple logic, keeps track on the number of messages received and prints them on the screen. To exemplify the other path of communication, the processing node sends a reply message to the MN containing the number of message that it received so far. 
Line 10: Line 10:
 You do not need to copy the source code snippets shown in this tutorial. At the end of each section there is a link to download the entire node source code.  You do not need to copy the source code snippets shown in this tutorial. At the end of each section there is a link to download the entire node source code. 
 </WRAP> </WRAP>
 +
 ===== MobileClient ===== ===== MobileClient =====
-To create our mobile client application, create a Java Project in Eclipse called ''HelloCore''. Be cautious and certify if you imported correctly the required libraries, which in this case is the ''contextnet.jar'' library. If you have any question on how to import these library, check the [[Eclipse|creating a SDDL project]] page. After these steps, create an empty Java ''HelloCoreClient'' Class using the ''br.pucrio.inf.lac.helloworld'' package in the ''HelloCore'' project.+To create our mobile client application, create a Java Project in Eclipse called ''HelloCore''. Be cautious and certify if you imported correctly the required libraries, which in this case is the ''contextnet.jar'' library. Alternatively, you can declare the ContextNet middleware as a Maven dependency . If you have any question on how to import this library, check the [[Eclipse|creating a SDDL project]] page. After these steps, create an empty Java ''HelloCoreClient'' Class using the ''br.pucrio.inf.lac.helloworld'' package in the ''HelloCore'' project.
  
 In SDDL, nodes that are outside the core (either mobile or stationary) interact directly with the Gateway rather than the processing nodes, since it uses a different protocol ([[ClientLib]] and [[MRUDP]]) than the core ([[wp>Data_Distribution_Service|Data Distribution Service (DDS)]]). The Gateway translate the node message to the core protocol and vice-versa. Thus, the primary interaction of the client is with the gateway. Therefore, we first declare two variables to locate our gateway, its IP address and port number. Since in this tutorial we are running our entire application (mobile client node, gateway, and processing node) locally, we can specify the gateway IP to the loopback ''127.0.0.1'' and choose its default port ''5500''. In a real distributed scenario, the client would need to know at least one of the gateways IP and ports pair.  In SDDL, nodes that are outside the core (either mobile or stationary) interact directly with the Gateway rather than the processing nodes, since it uses a different protocol ([[ClientLib]] and [[MRUDP]]) than the core ([[wp>Data_Distribution_Service|Data Distribution Service (DDS)]]). The Gateway translate the node message to the core protocol and vice-versa. Thus, the primary interaction of the client is with the gateway. Therefore, we first declare two variables to locate our gateway, its IP address and port number. Since in this tutorial we are running our entire application (mobile client node, gateway, and processing node) locally, we can specify the gateway IP to the loopback ''127.0.0.1'' and choose its default port ''5500''. In a real distributed scenario, the client would need to know at least one of the gateways IP and ports pair. 
Line 17: Line 18:
 <code java> <code java>
 public class HelloCoreClient { public class HelloCoreClient {
-  private static String gatewayIP   = "127.0.0.1"; +  private static String  gatewayIP   = "127.0.0.1"; 
-  private static int gatewayPort = 5500;+  private static int     gatewayPort = 5500;
 } }
 </code> </code>
Line 26: Line 27:
 <code java> <code java>
 public class HelloCoreClient { public class HelloCoreClient {
- +  private static String       gatewayIP   = "127.0.0.1"; 
-  private static String gatewayIP   = "127.0.0.1"; +  private static int          gatewayPort = 5500; 
-  private static int gatewayPort = 5500; +  private MrUdpNodeConnection connection;
-  private MrUdpNodeConnection connection;+
  
   public static void main(String[] args) {   public static void main(String[] args) {
Line 38: Line 38:
 </code> </code>
  
-We will make the connection with the gateway in our constructor. The client connection is very straightforward; first, we create a ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnclib/net/mrudp/MrUdpNodeConnection.html|MrUdpNodeConnection]]'' object. This connection object, encapsulate the details and protocol involved in the physical connection with the gateway, for instance, it uses unique ids to identify the client and the connection when the node changes IPs. If we do not specify an argument in the connection instantiation, it automatically generates an UUID to identify the client. As you have read in the [[Basic|basic concepts]], this id is used to unique identify the client independent of his IP, which is useful for handover and abstracting the client location. After creating the object, we establish the connection calling the ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/lac/cnclib/net/mrudp/MrUdpNodeConnection.html#connect(java.net.SocketAddress)|connect()]]'' method passing the gateway ''address'' as parameter. Finally, we add our object as a ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnclib/net/NodeConnectionListener.html|NodeConnectionListener]]'', this listener holds methods related to the node gateway connection. It will trigger functions to indicate that the node has successfully connected, disconnected, reconnected from the gateway alongside with operations to receive new messages, and unsent messages (messages that were timeouted and not sent due to disconnection).+We will make the connection with the gateway in our constructor. The client connection is very straightforward; first, we create a ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnclib/net/mrudp/MrUdpNodeConnection.html|MrUdpNodeConnection]]'' object. This connection object, encapsulate the details and protocol involved in the physical connection with the gateway, for instance, it uses unique ids to identify the client and the connection when the node changes IPs. If we do not specify an argument in the connection instantiation, it automatically generates an UUID to identify the client. This id is used to unique identify the client independent of his IP, which is useful for handover and abstracting the client location. We add a listener to the connection object (''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnclib/net/NodeConnectionListener.html|NodeConnectionListener]]'')which provides methods to react to the node gateway connection. It will trigger functions to indicate that the node has successfully connected, disconnected, reconnected from the gateway alongside with operations to receive new messages, and unsent messages (messages that were timeouted and not sent due to disconnection). Finally, we establish the connection calling the ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/lac/cnclib/net/mrudp/MrUdpNodeConnection.html#connect(java.net.SocketAddress)|connect()]]'' method passing the gateway ''address'' as parameter
  
 <code java> <code java>
Line 48: Line 48:
       try {       try {
           connection = new MrUdpNodeConnection();           connection = new MrUdpNodeConnection();
 +          connection.addNodeConnectionListener(this);
           connection.connect(address);           connection.connect(address);
-          connection.addNodeConnectionListener(this); 
       } catch (IOException e) {       } catch (IOException e) {
           e.printStackTrace();           e.printStackTrace();
Line 113: Line 113:
       try {       try {
           connection = new MrUdpNodeConnection();           connection = new MrUdpNodeConnection();
-          connection.connect(address); 
           connection.addNodeConnectionListener(this);           connection.addNodeConnectionListener(this);
 +          connection.connect(address);
       } catch (IOException e) {       } catch (IOException e) {
           e.printStackTrace();           e.printStackTrace();
Line 154: Line 154:
 </file> </file>
 ==== Processing Node ==== ==== Processing Node ====
-The core nodes establishes premises and protocols that are not suitable for mobile applications. To expand this processing power to mobile nodes, we added the concept of gateways that use our lightweight protocol to communicate with mobile nodes and translate their message to core messages. Developers can instantiate nodes to process the mobile nodes message.  +The core processing nodes establishes premises and protocols that are not suitable for mobile applications. To expand this processing power to mobile nodes, we added the concept of gateways that use our lightweight protocol to communicate with mobile nodes and translate their message to core messages. Developers can instantiate nodes to process the mobile nodes message.  
  
-To create our processing node application, create an empty Java ''HelloCoreServer'' Class using thebr.pucrio.inf.lac.helloworld package. You can create this class in the existing ''HelloCore'' project or create a separated project, for now we will make the class in the existing project. The processing nodes communicate using [[SDDL]], an abstraction that encapsulate the [[wp>DDS|DDS]] communication. +To create our processing node application, create an empty Java ''HelloCoreServer'' Class using the ''br.pucrio.inf.lac.helloworld'' package. You can create this class in the existing ''HelloCore'' project or create a separated project, for now we will make the class in the existing project. The processing nodes communicate using [[SDDL]], an abstraction that encapsulate the [[wp>DDS|DDS]] communication. 
  
 The processing node uses the DDS protocol to communicate in the core. Thus, it needs to create readers and writers to manipulate data in the core. In addition, it needs to formally join the other core nodes, this is done by recovering an instance of the middleware and creating a participant. After that, it instantiate publishers and subscribers to enable reading and writing data. In our application, we are interested in reading the application messages sent to the core, thus, we create a data reader for the ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnet/sddl/objects/Message.html|Message]]'' topic, which represents application messages. We are also interested on sending a reply to our mobile node, thus, we create a data writer for the private message topic, which allow us to send a message to a specific node. Our processing node will count the number of messages received, thus, we define a global count variable. Finally, we specify a very simple wait command to ensure that the processing nodes stays alive. This initial code is displayed bellow: The processing node uses the DDS protocol to communicate in the core. Thus, it needs to create readers and writers to manipulate data in the core. In addition, it needs to formally join the other core nodes, this is done by recovering an instance of the middleware and creating a participant. After that, it instantiate publishers and subscribers to enable reading and writing data. In our application, we are interested in reading the application messages sent to the core, thus, we create a data reader for the ''[[http://www.lac.inf.puc-rio.br/dokuwiki/api/index.html?lac/cnet/sddl/objects/Message.html|Message]]'' topic, which represents application messages. We are also interested on sending a reply to our mobile node, thus, we create a data writer for the private message topic, which allow us to send a message to a specific node. Our processing node will count the number of messages received, thus, we define a global count variable. Finally, we specify a very simple wait command to ensure that the processing nodes stays alive. This initial code is displayed bellow:
Line 185: Line 185:
  
     counter = 0;     counter = 0;
 +    System.out.println("=== Server Started (Listening) ===");
     synchronized (this) {     synchronized (this) {
       try {       try {
Line 239: Line 240:
  
     counter = 0;     counter = 0;
 +    System.out.println("=== Server Started (Listening) ===");
     synchronized (this) {     synchronized (this) {
       try {       try {
Line 272: Line 274:
 To execute our application, we need to first create the gateway, which will instantiate the core infrastructure if none exists. The gateway receives two parameters, its public IP and a given port number. Since we are this sample locally, we can choose the loopback IP ''127.0.0.1'' and the default middleware port ''5500''. To do that, open a shell, and run the following command: To execute our application, we need to first create the gateway, which will instantiate the core infrastructure if none exists. The gateway receives two parameters, its public IP and a given port number. Since we are this sample locally, we can choose the loopback IP ''127.0.0.1'' and the default middleware port ''5500''. To do that, open a shell, and run the following command:
  
-  $ gateway 127.0.0.1 5500 OpenSplice+  $ java -jar contextnet-2.5.jar 127.0.0.1 5500 OpenSplice
  
 After that, we instantiate our processing node. To do that, run the ''HelloCoreServer'' class in Eclipse as a Java application. Finally, execute the ''HelloCoreClient'' class in Eclipse to start the mobile node application. You will see the printed messages in your console. After that, we instantiate our processing node. To do that, run the ''HelloCoreServer'' class in Eclipse as a Java application. Finally, execute the ''HelloCoreClient'' class in Eclipse to start the mobile node application. You will see the printed messages in your console.
 +
 +===== Final Remarks and Extending This Example =====
 +This tutorial demonstrated how to connect a mobile node with the core processing nodes in the ContextNet architecture. The example used a basic code that illustrates the asynchronous send and receive primitives for both, the client and core, type of nodes. As an exercise try to extend the client node application to send several messages and use several core servers.
  • hellocore.txt
  • Last modified: 2017/07/21 03:08
  • (external edit)