// Medusa is a graph viewer which allows interactive editing of networks
// (edges and nodes) and also connects to databases.
//
// Copyright (C) 2006 Sean Hooper
//
// This program is free software; you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation; either version 2 of the License, or (at 
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along 
// with this program; if not, write to the 
// Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, 
// Boston, MA 02111-1307 USA

package medusa.stringconnection;


import java.io.IOException;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;


import java.util.HashMap;
import medusa.graph.Graph;
import medusa.graph.Node;



/**
 * Annotates graphs retrieved by stringconnection
 */
public class Annotator implements Runnable {
    
    private Thread me;
    private boolean done;

    private String url;
    private int port;
    private Graph selection;
    private StringBuffer sb;
    private float cutoff;

    public Annotator (String url, int port, Graph selection){
	this.url=url;
	this.port=port;
	this.selection=selection;
	this.cutoff=cutoff;
    }

    public int getMax(){
	return selection.getNodeSize();
    }

    public StringBuffer getAnnotation(){
	return sb;
    }

    public void start(){
	if (me==null) {
	    me = new Thread(this);
	}
	me.start();
    }
    
    public void stop(){
	if (me!=null) {
	    me=null;
        }
    }

     public void run(){
	Thread myThread=Thread.currentThread();
	done=false;
	if (me==myThread){
	    //System.out.println("thread running");
	    try {
		getAnnotationFromString();
		//System.out.println("should be done getting graph");
	    }
	    catch (IOException ie){
		sb=new StringBuffer();
	    }
	    done=true;
	}
	//System.out.println("thread is d"+done);
    }
    
    int progress=0;
    public int getProgress(){
	return progress;
	
    }
    public boolean isDone(){
	return done;
    }

    // get annotation from string
    public void getAnnotationFromString() throws IOException{
	//System.out.println("annotating ");
	sb=new StringBuffer();
	Socket stringSocket = new Socket(url,port);
	//System.out.println("socket opened ");
	PrintWriter out = new PrintWriter(stringSocket.getOutputStream(),true);
	BufferedReader in = new BufferedReader
	    (new InputStreamReader
	     (stringSocket.getInputStream() ));
	String inLine;
		
	// prepare the transfer
	StringBuffer transferSB=new StringBuffer();
        Node node;
	for (Iterator i = selection.nodesIterator();  i.hasNext() ;) {
	    node =(Node) i.next();
	    transferSB.append("NODE:");
	    transferSB.append(node.getLabel());
	    transferSB.append("\n");
	}
	transferSB.append("bye");

	out.println(transferSB);
	//System.out.println("sending to thread: "+transferSB);
	while (!in.ready()) {}
	String[] parts;
	while( (inLine=in.readLine())!=null){
	    //System.out.println(inLine);
	    if (inLine.length()>10){
		
		parts=inLine.split("\t");
		//sb.append(inLine);
		//System.out.println(parts[0]+" "+parts[1]);
		node = (Node) selection.getNode(parts[0]); 
		if (node!=null){
		    node.setAnnotation(parts[1]);
		    
		}
	    }
	    progress++;
	}
	
	out.close();
	in.close();
	stringSocket.close();
    }
    

}

 

Ads help maintain this website.