Differences
This shows you the differences between two versions of the page.
hellomobile [2016/05/02 15:10] mroriz [HelloMobileSender] |
hellomobile [2017/07/21 03:08] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Hello Mobile ====== | ||
- | {{ : | ||
- | In the [[hellocore|previous tutorial (Hello Core)]], we covered how a mobile client (in a mobile node) can communicate with a processing node in the SDDL core. In this tutorial, we will work with a different approach: communication between mobile nodes. This communication pass through the core, which act as a bridge connecting the nodes. | ||
- | In this tutorial, our sample application will consist of two peer executing in mobile nodes: a sender and a receiver, as illustrated in the figure aside. The application code will be very similar to the first part, with the exception that the message need to be tagged with an identifier of the sender and receiver peers. The ID can be specified when the '' | ||
- | |||
- | We start the tutorial explaining the '' | ||
- | |||
- | <WRAP tip> | ||
- | 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. | ||
- | </ | ||
- | |||
- | ===== Custom Data ===== | ||
- | Applications can attach any kind of serializable object in their messages. In the previous tutorial [[hellocore|HelloCore]], | ||
- | |||
- | <file java CustomData.java> | ||
- | package br.pucrio.lac.inf.helloworld; | ||
- | |||
- | import java.io.Serializable; | ||
- | import javax.swing.ImageIcon; | ||
- | |||
- | public class CustomData implements Serializable { | ||
- | private static final long serialVersionUID = 6093226637618022646L; | ||
- | private String caption; | ||
- | private ImageIcon icon; | ||
- | | ||
- | public CustomData() { | ||
- | } | ||
- | |||
- | public CustomData(String caption, String icon) { | ||
- | this.caption = caption; | ||
- | | ||
- | this.icon = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource(icon)); | ||
- | } | ||
- | |||
- | public String getCaption() { | ||
- | return caption; | ||
- | } | ||
- | |||
- | public ImageIcon getIcon() { | ||
- | return icon; | ||
- | } | ||
- | | ||
- | public void setCaption(String caption) { | ||
- | this.caption = caption; | ||
- | } | ||
- | | ||
- | public void setIcon(ImageIcon icon) { | ||
- | this.icon = icon; | ||
- | } | ||
- | | ||
- | @Override | ||
- | public String toString() { | ||
- | String firstPart | ||
- | String secondPart = " | ||
- | String thirdPart | ||
- | | ||
- | return firstPart + secondPart + thirdPart; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ===== HelloMobileSender ===== | ||
- | With very few modifications, | ||
- | |||
- | <file java HelloMobileSender.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.UUID; | ||
- | import java.util.logging.Level; | ||
- | import java.util.logging.Logger; | ||
- | |||
- | import lac.cnclib.net.NodeConnection; | ||
- | import lac.cnclib.net.NodeConnectionListener; | ||
- | import lac.cnclib.net.mrudp.MrUdpNodeConnection; | ||
- | import lac.cnclib.sddl.message.ApplicationMessage; | ||
- | import lac.cnclib.sddl.message.Message; | ||
- | |||
- | public class HelloMobileSender implements NodeConnectionListener { | ||
- | |||
- | private static String | ||
- | private static int gatewayPort | ||
- | private MrUdpNodeConnection connection; | ||
- | private UUID myUUID; | ||
- | |||
- | public HelloMobileSender() { | ||
- | myUUID = UUID.fromString(" | ||
- | InetSocketAddress address = new InetSocketAddress(gatewayIP, | ||
- | try { | ||
- | connection = new MrUdpNodeConnection(myUUID); | ||
- | connection.addNodeConnectionListener(this); | ||
- | connection.connect(address); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | |||
- | public static void main(String[] args) { | ||
- | Logger.getLogger("" | ||
- | |||
- | HelloMobileSender sender = new HelloMobileSender(); | ||
- | sender.sendPicture(" | ||
- | } | ||
- | | ||
- | public void sendPicture(String caption, String imageName) { | ||
- | CustomData serializableContent = new CustomData(caption, | ||
- | ApplicationMessage message = new ApplicationMessage(); | ||
- | message.setContentObject(serializableContent); | ||
- | message.setRecipientID(UUID.fromString(" | ||
- | |||
- | try { | ||
- | System.out.println(" | ||
- | connection.sendMessage(message); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | |||
- | public void connected(NodeConnection remoteCon) { | ||
- | ApplicationMessage message = new ApplicationMessage(); | ||
- | message.setContentObject(" | ||
- | |||
- | try { | ||
- | connection.sendMessage(message); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | | ||
- | public void newMessageReceived(NodeConnection remoteCon, Message message) { | ||
- | System.out.println(" | ||
- | System.out.println(message.getContentObject()); | ||
- | } | ||
- | |||
- | public void reconnected(NodeConnection remoteCon, SocketAddress endPoint, boolean wasHandover, | ||
- | |||
- | public void disconnected(NodeConnection remoteCon) {} | ||
- | |||
- | public void unsentMessages(NodeConnection remoteCon, List< | ||
- | |||
- | public void internalException(NodeConnection remoteCon, Exception e) {} | ||
- | } | ||
- | </ | ||
- | |||
- | ===== HelloMobileReceiver ===== | ||
- | '' | ||
- | |||
- | <file java HelloMobileReceiver.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.UUID; | ||
- | import java.util.logging.Level; | ||
- | import java.util.logging.Logger; | ||
- | |||
- | import lac.cnclib.net.NodeConnection; | ||
- | import lac.cnclib.net.NodeConnectionListener; | ||
- | import lac.cnclib.net.mrudp.MrUdpNodeConnection; | ||
- | import lac.cnclib.sddl.message.ApplicationMessage; | ||
- | import lac.cnclib.sddl.message.Message; | ||
- | |||
- | public class HelloMobileReceiver implements NodeConnectionListener { | ||
- | |||
- | private static String | ||
- | private static int gatewayPort | ||
- | private MrUdpNodeConnection connection; | ||
- | private UUID myUUID; | ||
- | | ||
- | public HelloMobileReceiver() { | ||
- | myUUID = UUID.fromString(" | ||
- | InetSocketAddress address = new InetSocketAddress(gatewayIP, | ||
- | try { | ||
- | connection = new MrUdpNodeConnection(myUUID); | ||
- | connection.addNodeConnectionListener(this); | ||
- | connection.connect(address); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | |||
- | public static void main(String[] args) { | ||
- | Logger.getLogger("" | ||
- | |||
- | new HelloMobileReceiver(); | ||
- | } | ||
- | |||
- | public void connected(NodeConnection remoteCon) { | ||
- | ApplicationMessage message = new ApplicationMessage(); | ||
- | message.setContentObject(" | ||
- | |||
- | try { | ||
- | connection.sendMessage(message); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | |||
- | public void newMessageReceived(NodeConnection remoteCon, Message message) { | ||
- | System.out.println(" | ||
- | System.out.println(" | ||
- | | ||
- | ApplicationMessage appMessage = new ApplicationMessage(); | ||
- | appMessage.setContentObject(" | ||
- | appMessage.setRecipientID(message.getSenderID()); | ||
- | | ||
- | try { | ||
- | connection.sendMessage(appMessage); | ||
- | } catch (IOException e) { | ||
- | e.printStackTrace(); | ||
- | } | ||
- | } | ||
- | |||
- | public void reconnected(NodeConnection remoteCon, SocketAddress endPoint, boolean wasHandover, | ||
- | |||
- | public void disconnected(NodeConnection remoteCon) {} | ||
- | |||
- | public void unsentMessages(NodeConnection remoteCon, List< | ||
- | |||
- | public void internalException(NodeConnection remoteCon, Exception e) {} | ||
- | } | ||
- | </ | ||
- | |||
- | ===== Execution ===== | ||
- | The execution of our application is identical to the HelloCore example, if there is no gateway executing we need to create one to instantiate the core infrastructure. To do that, open a shell, and run the following command: | ||
- | |||
- | $ java -jar contextnet-2.5.jar 127.0.0.1 5500 | ||
- | |||
- | After that, we instantiate the mobiles nodes. First run the '' |