Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/stanford/genetics/treeview/app/Daemon.java


1   /*
2    *
3    * Author QQW
4    * 
5    */
6   package edu.stanford.genetics.treeview.app;
7   
8   import java.io.*;
9   import java.net.*;
10  
11  import edu.stanford.genetics.treeview.*;
12  
13  
14  /**
15   *
16   * A daemon is a thread that waits for request and forward to a RequestHandler
17   * in this case, the TreeViewApp
18   *
19   * The intention of this :
20   *    If an instance of the program is running, it shall starts a daemon
21   *    which waits for requests.
22   *  
23   *    When another instance of the  program is started, the new program will 
24   *    first try to contact the daemons. If any exists, the new program will
25   *    ask the daemon to do the task.
26   *  
27   *    daemon will delegate the task to the RequestHandler.
28   */
29  
30  public class Daemon extends Thread{  
31      public static String DAEMON_RUNNING = "TreeView daemon running";
32      
33      public static int  BASE_PORT       = 9981;
34      public static int  NUM_OF_PORTS    = 5;
35      public static int  REQUST_TIME_OUT = 4 * 1000; //4 seconds 
36    
37      private ServerSocket serverSocket;
38      private RequestHandler handler;
39  
40      public Daemon(RequestHandler handler)throws IOException{
41    if(!initSocket())throw new IOException("Can not init daemon socket");
42    this.handler = handler;
43    setDaemon(true);
44    start();
45      }
46  
47      private boolean initSocket(){
48    for(int i = 0 ; i < NUM_OF_PORTS; i++){
49        try{
50      int port = BASE_PORT  + i;
51      serverSocket = new ServerSocket(port);
52      if(DEBUG)System.out.println("Start server socket at port "+ port);
53      break;
54        }catch(IOException e){
55      if(DEBUG)System.out.println(e.getMessage());
56        }
57    }
58    return serverSocket != null;
59      }
60  
61   
62      public void run(){
63    while(true){
64        try{
65      Socket socket = serverSocket.accept();
66  
67      OutputStream out = socket.getOutputStream();
68      out.write(DAEMON_RUNNING.getBytes());
69      out.flush();
70      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
71      java.util.LinkedList argsList = new java.util.LinkedList();
72      String line = null;
73      while( (line = reader.readLine() )!= null){
74          argsList.add(line);
75      }
76      String[] args = new String[argsList.size()];
77      argsList.toArray(args);
78      if(DEBUG)System.out.println("GET REQUEST " + toString(args));
79      handler.handleRequest(args);
80      socket.close();
81        }catch(IOException e){
82      e.printStackTrace();
83        }catch(Throwable t){
84      t.printStackTrace();    
85        }
86    }
87      }
88  
89  
90      public static class SimpleClient{
91    public boolean sendRequest( String[] args){
92        for(int i = 0 ; i < NUM_OF_PORTS; i++){
93      int port = BASE_PORT  + i;
94      try{
95          if(sendRequest(port, args))return true;
96      }catch(IOException e){
97          if(DEBUG)System.out.println("No daemon running at "+port);
98          //e.printStackTrace();
99      }
100       }
101       return false;
102   }
103 
104   private static boolean sendRequest(int port, String[] args)throws IOException{
105       if(DEBUG)System.out.println("Try open use TreeView instance at port " +port);
106       java.net.Socket socket = new java.net.Socket("127.0.0.1", port);      
107       socket.setSoTimeout(REQUST_TIME_OUT);
108       byte[] buf = new byte[128];
109       InputStream in = socket.getInputStream();
110       int read = in.read(buf);
111       String msg = new String(buf, 0, read);
112       System.out.println(read +" Get message : "+msg);
113       if(msg.equals(Daemon.DAEMON_RUNNING)){
114     OutputStream out = socket.getOutputStream();
115     for(int i = 0 ; i < args.length; i ++){
116         out.write(args[i].getBytes());
117         out.write("\n".getBytes());
118     }
119     socket.close();  
120     return true;
121       }
122       
123       socket.close();  
124       return false;
125   }
126     }
127 
128 
129     
130     public static interface RequestHandler{
131   public void handleRequest(String[] args);
132     }
133 
134 
135     
136     public static String toString(String[] args){
137   StringBuffer sb = new StringBuffer();
138   for(int i = 0 ; i < args.length; i++)sb.append(args[i]).append(" ");
139   return sb.toString();
140     }
141 
142     public static boolean DEBUG = true;    
143 }