| 1 | == Exercise 3: Socket Programming using New MobilityFirst NetAPI == |
| 2 | |
| 3 | === Objective: === |
| 4 | |
| 5 | In this exercise we will learn to write, compile, and run a simple content distribution application using MobilityFirst's new socket API. We will then modify the program to utilize MobilityFirst's native support for point to multi-point delivery services such as anycast and multicast to enable more flexible delivery options. |
| 6 | |
| 7 | === Develop Sender and Receiver Applications with MF Socket API === |
| 8 | |
| 9 | The following Java application shows the key pieces of the sender application that sends a file to a receiver known to the sender by its GUID and to then receive a confirmation message from the receiver: |
| 10 | |
| 11 | {{{ |
| 12 | #!java |
| 13 | //Simple class used to test the java api |
| 14 | |
| 15 | |
| 16 | //jmfapi needs to be in the classpath |
| 17 | import java.io.*; |
| 18 | import java.util.*; |
| 19 | import java.nio.file.*; |
| 20 | import edu.rutgers.winlab.jmfapi.*; |
| 21 | import edu.rutgers.winlab.jmfapi.GUID; |
| 22 | |
| 23 | class Sender{ |
| 24 | private static void usage(){ |
| 25 | System.out.println("Usage:"); |
| 26 | System.out.println("sender <my_GUID> <file_to_send> <dst_GUID>"); |
| 27 | } |
| 28 | public static void main(String []argv){ |
| 29 | if(argv.length < 3){ |
| 30 | usage(); |
| 31 | return; |
| 32 | } |
| 33 | String scheme = "basic"; |
| 34 | GUID srcGUID = null, dstGUID; |
| 35 | srcGUID = new GUID(Integer.parseInt(argv[0])); |
| 36 | Path file = FileSystems.getDefault().getPath(argv[1]); |
| 37 | dstGUID = new GUID(Integer.parseInt(argv[2])); |
| 38 | JMFAPI sender = new JMFAPI(); |
| 39 | try{ |
| 40 | if(srcGUID!=null) sender.jmfopen(scheme, srcGUID); |
| 41 | else sender.jmfopen(scheme); |
| 42 | byte[] fileArray; |
| 43 | try { |
| 44 | fileArray = Files.readAllBytes(file); |
| 45 | } catch (IOException e){ |
| 46 | System.out.println("ERROR"); |
| 47 | return; |
| 48 | } |
| 49 | byte[] tempArray; |
| 50 | int ret, read = 0; |
| 51 | while(fileArray.length - read>=1000000){ |
| 52 | tempArray = Arrays.copyOfRange(fileArray, 0, 999999); |
| 53 | sender.jmfsend(tempArray,1000000, dstGUID); |
| 54 | } |
| 55 | tempArray = Arrays.copyOfRange(fileArray, 0, fileArray.length - read - 1); |
| 56 | sender.jmfsend(tempArray,fileArray.length - read, dstGUID); |
| 57 | sender.jmfclose(); |
| 58 | System.out.println("Transmitted file"); |
| 59 | |
| 60 | //TODO receive confirmation |
| 61 | |
| 62 | System.out.println("Received confirmation"); |
| 63 | |
| 64 | } catch (JMFException e){ |
| 65 | System.out.println(e.toString()); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | }}} |
| 70 | |
| 71 | The following shows the corresponding receiver code: |
| 72 | |
| 73 | {{{ |
| 74 | #!java |
| 75 | //Simple class used to test the java api |
| 76 | |
| 77 | import java.io.*; |
| 78 | import java.util.*; |
| 79 | import java.nio.file.*; |
| 80 | import edu.rutgers.winlab.jmfapi.*; |
| 81 | |
| 82 | class Receiver{ |
| 83 | private static void usage(){ |
| 84 | System.out.println("Usage:"); |
| 85 | System.out.println("receiver [<my_GUID>]"); |
| 86 | } |
| 87 | public static void main(String []argv){ |
| 88 | String scheme = "basic"; |
| 89 | GUID srcGUID = null; |
| 90 | int i = 0; |
| 91 | if(argv.length == 1) srcGUID = new GUID(Integer.parseInt(argv[0])); |
| 92 | Path file = FileSystems.getDefault().getPath("temp.txt"); |
| 93 | try{ |
| 94 | Files.createFile(file); |
| 95 | } catch(IOException e){ |
| 96 | try{ |
| 97 | Files.delete(file); |
| 98 | Files.createFile(file); |
| 99 | } catch(IOException e2){ |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | byte[] buf = new byte[1000000]; |
| 104 | int ret; |
| 105 | JMFAPI receiver = new JMFAPI(); |
| 106 | try{ |
| 107 | if(srcGUID!=null) receiver.jmfopen(scheme, srcGUID); |
| 108 | else receiver.jmfopen(scheme); |
| 109 | while(i < 24954287){ |
| 110 | ret = receiver.jmfrecv_blk(null, buf, 1000000); |
| 111 | try{ |
| 112 | Files.write(file, buf, StandardOpenOption.APPEND); |
| 113 | } catch (IOException e){ |
| 114 | System.out.println(e.toString()); |
| 115 | } |
| 116 | i += ret; |
| 117 | |
| 118 | } |
| 119 | System.out.println("Received file"); |
| 120 | |
| 121 | //TODO send confirmation |
| 122 | |
| 123 | receiver.jmfclose(); |
| 124 | } catch (JMFException e){ |
| 125 | System.out.println(e.toString()); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | } |
| 130 | }}} |
| 131 | |
| 132 | ==== Test Sender/Receiver Applications ==== |
| 133 | |
| 134 | ==== Add Second Receiver End Point to Topology ==== |
| 135 | |
| 136 | ==== Modify Delivery Service Option to Add Multi-point Delivery ==== |
| 137 | |
| 138 | The following code snippet shows the modified portion of the code to request Multicast delivery option while transfering the file: |
| 139 | |
| 140 | {{{ |
| 141 | #!java |
| 142 | |
| 143 | }}} |