C:\Java WorkShop\Lexx\src\lexx\server\socket\Server.java

1    package lexx.server.socket; 
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     * @author Mohammed Imran 
9     * @version 1.0 
10    */ 
11   import java.io.DataInputStream; 
12   import java.io.DataOutputStream; 
13   import java.io.IOException; 
14   import java.net.InetAddress; 
15   import java.net.ServerSocket; 
16   import java.net.Socket; 
17   import java.net.UnknownHostException; 
18   import java.util.Properties; 
19   import java.util.Vector; 
20    
21   import javax.swing.JTextArea; 
22    
23   import lexx.server.Chat; 
24   import lexx.server.RequestingFiles; 
25   import lexx.server.ServerBase; 
26    
27   /** 
28    * <p>Server uses direct communication via a socket</p> 
29    * <p>Copyright (c) 2002-2003</p> 
30    * @since 20/03/2003 
31    * @author Mohammed Imran 
32    * @version 1.0 
33    */ 
34   public final class Server implements ServerBase 
35   { 
36     private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger. 
37         getLogger(Server.class); 
38     public static final String USERNAME = "USERNAME"; 
39     public static final String MESSAGE = "MESSAGE"; 
40     public static final String FILEDATA = "FILEDATA"; 
41     public static final String HASHKEY = "HASHKEY"; 
42     public static final String FILENAME = "FiLENAME"; 
43     public static final String REQUESTFILE = "REQUESTFILE"; 
44     public static final String BOOLEAN = "BOOLEAN"; 
45     public static final String HASHPROPERTIES = "HASHPROPERTIES"; 
46     public static final String CLIENT_NUM = "CLIENT_NUM"; 
47     public static final String SENDMEFILE = "SENDMEFILE"; 
48     public static final String SENDMEHASH = "SENDMEHASH"; 
49     public static final String END_OF_FILES = "ENDOFFILES"; 
50     private int port = 700; 
51     private boolean li = true; 
52     final Vector clients = new Vector(); 
53    
54     /** 
55      * Usage <br> 
56      * ----- <br> 
57      * lexx.server.socket.Server [directory containing project] [port] [proxy address] [proxy port]<br> 
58      * lexx.server.socket.Server [directory containing project] [port] <br> 
59      * lexx.server.socket.Server [directory containing project]<br> 
60      */ 
61     public static void main(String args[]) 
62     { 
63       System.out.println("Usage:"); 
64       System.out.println(" lexx.server.socket.Server [directory containing project] [port] [proxy address] [proxy port]"); 
65       System.out.println( 
66           " lexx.server.socket.Server [directory containing project] [port]"); 
67       System.out.println( 
68           " lexx.server.socket.Server [directory containing project]"); 
69    
70       int p = 3128; 
71       String path = "."; 
72    
73       if(args.length >= 1) 
74       { 
75         path = args[0]; 
76    
77       } 
78       if(args.length >= 2) 
79       { 
80         p = Integer.parseInt(args[1]); 
81    
82       } 
83       if(args.length == 4) 
84       { 
85         Properties properties = System.getProperties(); 
86         properties.put("http.proxyHost", args[2]); 
87         properties.put("http.proxyPort", args[3]); 
88         properties.put("socksProxyHost", args[2]); 
89         properties.put("socksProxyPort", args[3]); 
90         properties.put("ftp.proxyHost", args[2]); 
91         properties.put("ftp.proxyPort", args[3]); 
92         System.setProperties(properties); 
93       } 
94    
95       try 
96       { 
97         System.out.println("You ip address is " + 
98                            java.net.InetAddress.getLocalHost(). 
99                            getCanonicalHostName() + " on port " + p); 
100        System.out.println("You ip address is " + 
101                           java.net.InetAddress.getLocalHost().getHostAddress() + 
102                           " on port " + p); 
103      } 
104      catch(UnknownHostException ex) 
105      { 
106        logger.fatal(ex, ex); 
107      } 
108      new Server(path, p); 
109    } 
110   
111    final String projectDir; 
112   
113    /** 
114     * Tells you the number of clients that are on-line 
115     * @return Tells you the number of clients that are on-line 
116     */ 
117    public final int getClientNum() 
118    { 
119      return clients.size(); 
120    } 
121   
122    static JTextArea log = null; //used for gui 
123   
124    /** 
125     * If log is enables then the text being recieved/sent by server it stored 
126     * @return returns log 
127     */ 
128    public final JTextArea getLog() 
129    { 
130      return log; 
131    } 
132   
133    /** 
134     * Server logs all messages that are recieved/sent by server 
135     * @param newLog sets the new log 
136     */ 
137    public final void setLog(JTextArea newLog) 
138    { 
139      log = newLog; 
140    } 
141   
142    private ServerSocket serverSock = null; 
143    private Server server; 
144   
145    public Server(String projectPath, int portNum) 
146    { 
147      port = portNum; 
148      projectDir = projectPath; 
149      try 
150      { 
151        if(log != null) 
152        { 
153          log.append("Waiting for " + InetAddress.getLocalHost() + " on port " + 
154                     port + "\n"); 
155        } 
156        if(logger.isDebugEnabled()) 
157        { 
158          logger.debug("Waiting for " + InetAddress.getLocalHost() + 
159                       " on port " + port); 
160        } 
161        serverSock = Chat.getServerSocket(port); 
162      } 
163      catch(IOException e) 
164      { 
165        logger.fatal(e, e); 
166        return; 
167      } 
168   
169      server = this; 
170   
171      new Thread() 
172      { 
173        public void run() 
174        { 
175          while(li) 
176          { 
177            try 
178            { 
179              Socket socket = serverSock.accept(); 
180              socket.setKeepAlive(true); 
181              socket.setTcpNoDelay(true); 
182              if(log != null) 
183              { 
184                log.append("Accept " + socket.getInetAddress().getHostName() + 
185                           "\n"); 
186              } 
187              if(logger.isDebugEnabled()) 
188              { 
189                logger.debug("Accept " + 
190                             socket.getInetAddress().getHostName()); 
191   
192              } 
193              DataOutputStream remoteOut = new DataOutputStream(socket. 
194                  getOutputStream()); 
195              clients.addElement(remoteOut); 
196              new ServerHelper(socket, remoteOut, 
197                               new DataInputStream(socket.getInputStream()), 
198                               server).start(); 
199            } 
200            catch(IOException e) 
201            { 
202              logger.fatal(e, e); 
203            } 
204          } 
205          if(serverSock != null) 
206          { 
207            try 
208            { 
209              serverSock.close(); 
210            } 
211            catch(IOException x) 
212            { 
213              logger.fatal(x, x); 
214            } 
215          } 
216        } 
217      }.start(); 
218    } 
219   
220    final Vector getClients() 
221    { 
222      return clients; 
223    } 
224   
225    /** 
226     *   stores the files that have been locked 
227     */ 
228    private Vector files = new Vector(); 
229   
230    final synchronized void removeFromClients(DataOutputStream remoteOut) 
231    { 
232      clients.removeElement(remoteOut); 
233   
234      //releases all files locked by user 
235      for(int i = 0; i < getFiles().size(); i++) 
236      { 
237        RequestingFiles r = (RequestingFiles) getFiles().get(i); 
238        if(r.getUser() == remoteOut) 
239        { 
240          r.setUser(null); 
241          r.setCanHaveFile(true); 
242        } 
243      } 
244    } 
245   
246    /** 
247     * the files that have been locked 
248     * @return the files that have been locked 
249     */ 
250    final Vector getFiles() 
251    { 
252      return files; 
253    } 
254  }