Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / java / slinkapp.java
Revision 1.1: download - view: text, annotated - select for diffs
Fri May 11 01:28:00 2007 UTC (10 years, 4 months ago) by saikat
Branches: MAIN
CVS tags: HEAD
Hooked in most of java. Hopefully, I didn't break the build on other boxes.
Realistically, I probably did.

import java.io.*;
import net.nutss.*;

public class slinkapp
{
    public static final int BUF_SIZE=4096;
    public int i = -1;
    private int q, r, num;
    private String localdata;
    FileWriter f = null;

    private SwaplinksSocket slinks;

    // set necessary nutss vars, open file writer, initiate swaplinks, and join the cloud
    // make sure to initiate swaplinks in the following order: set variables, call swaplinks_init()
    // and then
    public slinkapp(int num) {
        this.num = num;
        localdata = new String("node-"+num+"\n");
        NUTSS.init("ths"+num, "nutss.net", "sip.nutss.net", 5060, "ths"+num, "ths"+num);
        slinks = new SwaplinksSocket("cloud9", new NUTSSSocketAddress("ths1","nutss.net","swaplinksd"));
        try { f = new FileWriter(new File("output/output_node"+num+".txt")); }
        catch (IOException e) { e.printStackTrace(); }
    }

    private class recvthread implements Runnable {
        SwaplinksPacket pkt = new SwaplinksPacket(BUF_SIZE);
        public void run() {
            for(;;) {
                try {
                    slinks.receive(pkt);
                    String req = new String(pkt.getData());
                    System.out.println("node-"+num+" RCVD "+req);
                    if (f != null) f.write("node-"+num+" RCVD "+req);
                    if (req.substring(0,7).equals("REQUEST")) {
                        String rsp = "RESPONSE Response "+ r++ +" from "+localdata;
                        byte[] rspbytes = rsp.getBytes();
                        slinks.send(new SwaplinksPacket(rspbytes, rspbytes.length, pkt.getAddress()));
                        System.out.println("node-"+num+" SENT "+rsp);
                        if (f != null) f.write("node-"+num+" SENT "+rsp);
                    }
                } catch(IOException e) { e.printStackTrace(); }
            }
        }
    }

    private class sendthread implements Runnable {
        public void run() {
            try {
                while (i-- != 0) {
                    String msg = "REQUEST Query "+ q++ +" from "+localdata;
                    byte[] msgbytes = msg.getBytes();
                    slinks.send(new SwaplinksPacket(msgbytes, msgbytes.length));
                    System.out.println("node-"+num+" SENT REQUEST Query "+ q++ +" from "+localdata);
                    if (f != null) f.write("node-"+num+" SENT REQUEST Query "+ q++ +" from "+localdata);
                    Thread.sleep(3000);
                }
            } catch (Exception e) { e.printStackTrace(); }
            try { f.close(); } catch(IOException e) { e.printStackTrace(); }
            slinks.close();
        }
    }

    public static void main(String[] args)
    {
        slinkapp slp = new slinkapp(Integer.parseInt(args[0]));
        if(args.length > 1) slp.i = Integer.parseInt(args[1]);

        new Thread(slp.new recvthread()).start();
        new Thread(slp.new sendthread()).start();
        return;
    }
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment