KickJava   Java API By Example, From Geeks To Geeks.

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


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  
20 package org.lucane.proxy;
21
22 import org.lucane.common.*;
23 import org.lucane.common.net.ObjectConnection;
24
25 import java.net.Socket JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28
29 class ProxySlave
30   extends Thread JavaDoc
31 {
32
33   private ObjectConnection oc;
34   private Proxy parent;
35   private ProxySlave friend;
36   private Message init;
37   private ConnectInfo user;
38
39   /**
40    * Creates a new ProxySlave object.
41    *
42    * @param parent the proxy
43    * @param s the socket
44    */

45   public ProxySlave(Proxy parent, Socket JavaDoc s)
46   {
47     super();
48     this.parent = parent;
49     this.oc = new ObjectConnection(s);
50     this.friend = null;
51   }
52
53   /**
54    * Used to be run as a new Thread
55    */

56   public void run()
57   {
58     this.readMessage();
59
60     if(this.friend != null)
61       this.forward();
62   }
63
64   /**
65    * Read a line on the socket
66    */

67   private void readMessage()
68   {
69     try
70     {
71       init = (Message)this.oc.read();
72       String JavaDoc data = (String JavaDoc)init.getData();
73       if(data.startsWith("CONNECT_SET"))
74       {
75         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(data);
76         String JavaDoc command = stk.nextToken();
77         String JavaDoc username = stk.nextToken();
78         String JavaDoc hostname = stk.nextToken();
79         int port = Integer.parseInt(stk.nextToken());
80         String JavaDoc type = stk.nextToken();
81         user = new ConnectInfo(username, hostname, port, "nokey", type);
82
83         oc.write("OK");
84         Logging.getLogger().fine("user : " + user);
85         parent.register(this, user.getName());
86       }
87       else
88       {
89         ConnectInfo userdest = init.getReceiver();
90         this.friend = parent.whois(userdest.getName());
91       }
92     }
93     catch(Exception JavaDoc e)
94     {
95         Logging.getLogger().warning("Exception : " + e);
96     }
97   }
98
99   /**
100    * Forward a socket connection to a client
101    */

102   private void forward()
103   {
104     Logging.getLogger().info("BEGIN FORWARD : " + friend.user);
105
106     try
107     {
108       Socket JavaDoc sock = new Socket JavaDoc(friend.user.getHostName(), friend.user.getPort());
109
110       //copy the init
111
ObjectConnection myoc = new ObjectConnection(sock);
112       myoc.write(init);
113
114       //start the two forwarders
115
Thread JavaDoc t1 = new Forwarder(myoc, oc);
116       Thread JavaDoc t2 = new Forwarder(oc, myoc);
117       t1.start();
118       t2.start();
119
120       //wait
121
t1.join();
122       t2.join();
123     }
124     catch(Exception JavaDoc e)
125     {
126         Logging.getLogger().warning("Exception : " + e);
127     }
128
129     Logging.getLogger().fine("END FORWARD : " + friend.user);
130   }
131 }
132
Popular Tags