KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > proxy > Proxy


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.proxy;
20
21 import org.lucane.common.*;
22
23 import java.io.IOException JavaDoc;
24 import java.net.ServerSocket JavaDoc;
25 import java.net.Socket JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28
29 public class Proxy
30 {
31   private static final int DEFAULT_PORT = 5119;
32   private ServerSocket JavaDoc listener;
33   private Hashtable JavaDoc hash;
34   private boolean stop;
35
36   /**
37    * Creates a new Proxy object.
38    */

39   public Proxy(int port)
40   {
41     try
42     {
43       this.listener = new ServerSocket JavaDoc(port);
44       this.hash = new Hashtable JavaDoc();
45       this.stop = false;
46     }
47     catch(Exception JavaDoc e)
48     {
49       e.printStackTrace();
50     }
51   }
52
53   /**
54    * Accept new connections
55    */

56   public void listen()
57   {
58     Socket JavaDoc client = null;
59
60     while(! this.stop)
61     {
62       try
63       {
64         client = this.listener.accept();
65         (new ProxySlave(this, client)).start();
66       }
67       catch(Exception JavaDoc e)
68       {
69         e.printStackTrace();
70       }
71     }
72   }
73
74   /**
75    * register a user and a proxyslave
76    *
77    * @param ps a ProxySlave
78    * @param s a user
79    */

80   public void register(ProxySlave ps, String JavaDoc s)
81   {
82     hash.put(s, ps);
83   }
84
85   /**
86    * Get the ProxySlave associated to a user
87    *
88    * @param s the user
89    * @return the ProxySlave
90    */

91   public ProxySlave whois(String JavaDoc s)
92   {
93     return (ProxySlave)hash.get(s);
94   }
95
96   /**
97    * Runs the proxy
98    *
99    * @param args comment line arguments
100    */

101   public static void main(String JavaDoc[] args)
102   {
103     try {
104         Logging.init("lucane.log", "ALL");
105     } catch(IOException JavaDoc ioe) {
106         System.err.println("Unable to init logging, exiting.");
107         System.exit(1);
108     }
109     
110     Proxy p;
111     
112     if(args.length == 1)
113         p = new Proxy(Integer.parseInt(args[0]));
114     else
115         p = new Proxy(DEFAULT_PORT);
116     
117     p.listen();
118   }
119 }
120
Popular Tags