Version 1 (modified by 10 years ago) ( diff ) | ,
---|
Exercise 3: Socket Programming using New MobilityFirst NetAPI
Objective:
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.
Develop Sender and Receiver Applications with MF Socket API
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:
//Simple class used to test the java api //jmfapi needs to be in the classpath import java.io.*; import java.util.*; import java.nio.file.*; import edu.rutgers.winlab.jmfapi.*; import edu.rutgers.winlab.jmfapi.GUID; class Sender{ private static void usage(){ System.out.println("Usage:"); System.out.println("sender <my_GUID> <file_to_send> <dst_GUID>"); } public static void main(String []argv){ if(argv.length < 3){ usage(); return; } String scheme = "basic"; GUID srcGUID = null, dstGUID; srcGUID = new GUID(Integer.parseInt(argv[0])); Path file = FileSystems.getDefault().getPath(argv[1]); dstGUID = new GUID(Integer.parseInt(argv[2])); JMFAPI sender = new JMFAPI(); try{ if(srcGUID!=null) sender.jmfopen(scheme, srcGUID); else sender.jmfopen(scheme); byte[] fileArray; try { fileArray = Files.readAllBytes(file); } catch (IOException e){ System.out.println("ERROR"); return; } byte[] tempArray; int ret, read = 0; while(fileArray.length - read>=1000000){ tempArray = Arrays.copyOfRange(fileArray, 0, 999999); sender.jmfsend(tempArray,1000000, dstGUID); } tempArray = Arrays.copyOfRange(fileArray, 0, fileArray.length - read - 1); sender.jmfsend(tempArray,fileArray.length - read, dstGUID); sender.jmfclose(); System.out.println("Transmitted file"); //TODO receive confirmation System.out.println("Received confirmation"); } catch (JMFException e){ System.out.println(e.toString()); } } }
The following shows the corresponding receiver code:
//Simple class used to test the java api import java.io.*; import java.util.*; import java.nio.file.*; import edu.rutgers.winlab.jmfapi.*; class Receiver{ private static void usage(){ System.out.println("Usage:"); System.out.println("receiver [<my_GUID>]"); } public static void main(String []argv){ String scheme = "basic"; GUID srcGUID = null; int i = 0; if(argv.length == 1) srcGUID = new GUID(Integer.parseInt(argv[0])); Path file = FileSystems.getDefault().getPath("temp.txt"); try{ Files.createFile(file); } catch(IOException e){ try{ Files.delete(file); Files.createFile(file); } catch(IOException e2){ return; } } byte[] buf = new byte[1000000]; int ret; JMFAPI receiver = new JMFAPI(); try{ if(srcGUID!=null) receiver.jmfopen(scheme, srcGUID); else receiver.jmfopen(scheme); while(i < 24954287){ ret = receiver.jmfrecv_blk(null, buf, 1000000); try{ Files.write(file, buf, StandardOpenOption.APPEND); } catch (IOException e){ System.out.println(e.toString()); } i += ret; } System.out.println("Received file"); //TODO send confirmation receiver.jmfclose(); } catch (JMFException e){ System.out.println(e.toString()); } } }
Test Sender/Receiver Applications
Add Second Receiver End Point to Topology
Modify Delivery Service Option to Add Multi-point Delivery
The following code snippet shows the modified portion of the code to request Multicast delivery option while transfering the file: