Changes between Initial Version and Version 1 of Tutorials/oMF/tut3


Ignore:
Timestamp:
Sep 25, 2014, 6:23:39 PM (10 years ago)
Author:
seskar
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Tutorials/oMF/tut3

    v1 v1  
     1== Exercise 3: Socket Programming using New MobilityFirst NetAPI ==
     2
     3=== Objective: ===
     4
     5In 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
     9The 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
     17import java.io.*;
     18import java.util.*;
     19import java.nio.file.*;
     20import edu.rutgers.winlab.jmfapi.*;
     21import edu.rutgers.winlab.jmfapi.GUID;
     22
     23class 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
     71The following shows the corresponding receiver code:
     72
     73{{{
     74#!java
     75//Simple class used to test the java api
     76
     77import java.io.*;
     78import java.util.*;
     79import java.nio.file.*;
     80import edu.rutgers.winlab.jmfapi.*;
     81
     82class 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
     138The 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}}}