C:\Java WorkShop\Lexx\src\lexx\server\http\ServerHttp.java

1    package lexx.server.http; 
2     
3    /** 
4     * <p>Title: The Lexx Project</p> 
5     * <p>Description: Enchanced IDE For Java</p> 
6     * <p>Copyright (c) 2002-2003</p> 
7     * @since 19/03/2003 
8     * @aditional author Mohammed Imran 
9     * @version 1.0 
10    */ 
11    
12   import java.io.DataInputStream; 
13   import java.io.DataOutputStream; 
14   import java.io.File; 
15   import java.io.InputStream; 
16   import java.io.OutputStream; 
17   import java.net.InetAddress; 
18   import java.util.List; 
19   import java.util.Properties; 
20   import java.util.Vector; 
21    
22   import javax.swing.JTextArea; 
23    
24   import example.HttpServer; 
25   import example.HttpServerListener; 
26   import lexx.server.Chat; 
27   import lexx.server.HashProperties; 
28   import lexx.server.RequestingFiles; 
29   import lexx.server.ServerBase; 
30   import lexx.server.socket.Server; 
31    
32   /** 
33    * <p>Server uses the HTTP protocol to communicate<</p> 
34    * <p>Due to the limitations of HTTP protocol the client will need to connect to 
35    * server continously to enable <b>full duplex</b> communication</p> 
36    * <p>Copyright (c) 2002-2003</p> 
37    * @since 20/03/2003 
38    * @author Mohammed Imran 
39    * @version 1.0 
40    */ 
41   public final class ServerHttp implements HttpServerListener, ServerBase 
42   { 
43     private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger. 
44         getLogger(ServerHttp.class); 
45     private final HttpServer httpServer; 
46    
47     private final String projectDir; 
48     private final Chat c = new Chat(); 
49     private final Vector files = new Vector(); 
50    
51     private void broadcast(String message) 
52     { 
53       this.send( ("ALL" + message)); 
54     } 
55    
56     private void sendToSpecificClient(String message, String username) 
57     { 
58       this.send( ("USERNAME=" + username + '&' + message)); 
59     } 
60    
61     private void handleMessage(String message) 
62     { 
63       String username = "ALL"; 
64    
65       if(message.startsWith("USERNAME")) 
66       { 
67         for(int i = "USERNAME".length() + 1; i < message.length(); i++) 
68         { 
69           if(message.charAt(i) == '&') 
70           { 
71             username = message.substring("USERNAME".length() + 1, i); 
72             message = message.substring(i + 1, message.length()); 
73             break; 
74           } 
75         } 
76       } 
77    
78       if(message.startsWith(Server.MESSAGE)) //sends message to everyone 
79       { 
80         this.broadcast(message); 
81   ///////////////////      this.sendToSpecificClient(message, username); 
82       } 
83    
84       if(message.startsWith(Server.REQUESTFILE)) //wants to access/release a file 
85       { 
86         String fileName = c.getRequestedFile(message).getFilename(); 
87    
88         if(c.getRequestedFile(message).canHaveFile()) //wants access file 
89         { 
90           boolean found = false; 
91           boolean answer = false; 
92    
93           //checking current list 
94           for(int i = 0; i < files.size(); i++) 
95           { 
96             RequestingFiles r = (RequestingFiles) files.get(i); 
97             if(r.getFileName().equals(fileName)) 
98             { 
99               found = true; 
100              answer = r.canHaveFile(); 
101   
102              if(answer) 
103              { //locked by this user now 
104                r.setUser(username); 
105                r.setCanHaveFile(false); 
106              } 
107            } 
108          } 
109   
110          //not in current list 
111          if(!found) 
112          { 
113            files.add(new RequestingFiles(fileName, username, false)); 
114            answer = true; 
115          } 
116   
117          String reply = c.sendRequestedFile(fileName, answer); 
118          this.sendToSpecificClient(reply, username); 
119        } 
120        else //wants to release file 
121        { 
122          for(int i = 0; i < files.size(); i++) 
123          { 
124            RequestingFiles r = (RequestingFiles) files.get(i); 
125            if(r.getFileName().equals(fileName)) 
126            { 
127              files.remove(r); 
128            } 
129          } 
130        } 
131      } 
132   
133      if(message.startsWith(Server.FILENAME)) //wants send me a file 
134      { 
135        c.createFile(message, projectDir, true); 
136        this.broadcast(message); 
137      } 
138   
139      if(message.startsWith(Server.HASHPROPERTIES)) //updating server files from clients 
140      { 
141        HashProperties hash = HashProperties.hashCurrentProject(projectDir); 
142        HashProperties newHash = c.getHashProperties(message); 
143        List diff = HashProperties.getDifferentFiles(hash, newHash); 
144   
145        for(int i = 0; i < diff.size(); i++) 
146        { //requesting different files 
147          this.sendToSpecificClient(c.sendMeFile(diff.get(i).toString()), 
148                                    username); 
149        } 
150      } 
151   
152      if(message.startsWith(Server.SENDMEHASH)) //send my hashproperties 
153      { 
154        this.sendToSpecificClient(c.sendHashProperties(HashProperties. 
155            hashCurrentProject(projectDir)), username); 
156      } 
157   
158      if(message.startsWith(Server.SENDMEFILE)) //send user a file 
159      { 
160        String filename = c.getSendFileName(message); 
161        this.sendToSpecificClient(c.sendFile(new File(projectDir + File.separatorChar + filename), 
162                                             projectDir, true), username); 
163      } 
164   
165      if(message.startsWith(Server.CLIENT_NUM)) //wants to know the number of users 
166      { 
167        this.broadcast(c.sendClientNum(httpServer.getClientCount())); 
168      } 
169      else 
170      { 
171        if(clientNum != httpServer.getClientCount()) 
172        { 
173          clientNum = httpServer.getClientCount(); 
174          this.broadcast(c.sendClientNum(clientNum)); 
175        } 
176      } 
177   
178      if(message.startsWith(Server.END_OF_FILES)) //relays message back to client 
179      { //only required when waitForCache() is called on client side 
180        this.sendToSpecificClient(Server.END_OF_FILES, username); 
181      } 
182   
183    } 
184   
185    private int clientNum; 
186   
187    private final Vector messages = new Vector(); 
188   
189    private void send(String message) 
190    { 
191      messages.add(message); 
192    } 
193   
194    public ServerHttp(int port, int pool, String projectDirectory) throws 
195        Exception 
196    { 
197      projectDir = projectDirectory; 
198      httpServer = new HttpServer(port, pool); 
199      httpServer.addHttpServerListener(this); 
200      if(logger.isInfoEnabled()) 
201      { 
202        logger.info("Waiting for " + InetAddress.getLocalHost() + 
203                    " on port " + port); 
204      } 
205    } 
206   
207    public final void service(InputStream data, OutputStream out) 
208    { 
209      try 
210      { 
211        DataInputStream input = new DataInputStream(data); 
212        String s = Chat.getMessage(input); 
213   
214        if(log != null) 
215        { 
216          log.append("Recieved: " + s + "\n"); 
217        } 
218        if(logger.isDebugEnabled()) 
219        { 
220          logger.debug("Recieved: " + s); 
221   
222        } 
223        input.close(); 
224        this.handleMessage(s); 
225   
226        DataOutputStream dataOut = new DataOutputStream(out); 
227   
228        if(!messages.isEmpty()) 
229        { 
230          String response = "Dummy Message"; 
231          response = messages.get(0).toString(); 
232          messages.remove(0); 
233          if(log != null) 
234          { 
235            log.append("Server sent: " + response + "\n"); 
236          } 
237          if(logger.isDebugEnabled()) 
238          { 
239            logger.debug("Server sent: " + response); 
240   
241          } 
242          Chat.sendMessage(response, dataOut); 
243          dataOut.flush(); 
244          dataOut.close(); 
245        } 
246      } 
247      catch(Exception ex) 
248      { 
249        logger.fatal(ex, ex); 
250      } 
251    } 
252   
253    /** 
254     * Tells you the number of clients that are on-line 
255     * @return Tells you the number of clients that are on-line 
256     */ 
257    public final int getClientNum() 
258    { 
259      return httpServer.getClientCount(); 
260    } 
261   
262    /** 
263     * Usage: ServerHttp <port> <send timeout> <pool size> <project path><br> 
264     *        ServerHttp <port> <send timeout> <pool size> <project path> <proxy host> <proxy port><br> 
265     */ 
266    public static void main(String argv[]) throws Exception 
267    { 
268      if(argv.length >= 6) 
269      { 
270        Properties properties = System.getProperties(); 
271        properties.put("http.proxyHost", argv[4]); 
272        properties.put("http.proxyPort", argv[5]); 
273        properties.put("socksProxyHost", argv[4]); 
274        properties.put("socksProxyPort", argv[5]); 
275        properties.put("ftp.proxyHost", argv[4]); 
276        properties.put("ftp.proxyPort", argv[5]); 
277        System.setProperties(properties); 
278      } 
279   
280      System.out.println( 
281          "Usage: java ServerHttp <port> <send timeout> <pool size> <project path>"); 
282      System.out.println("Usage: java ServerHttp <port> <send timeout> <pool size> <project path> <proxy host> <proxy port>"); 
283      new ServerHttp(Integer.parseInt(argv[0]), Integer.parseInt(argv[2]), argv[3]); 
284    } 
285   
286    private JTextArea log = null; 
287   
288    /** 
289     * Server logs all messages that are recieved/sent by server 
290     * @param newLog sets the new log 
291     */ 
292    public final void setLog(JTextArea newLog) 
293    { 
294      log = newLog; 
295    } 
296   
297    /** 
298     * If log is enables then the text being recieved/sent by server it stored 
299     * @return returns log 
300     */ 
301    public final JTextArea getLog() 
302    { 
303      return log; 
304    } 
305  } 
306