1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.caleigo.toolkit.tunnel;
20
21
22 import java.io.*;
23 import java.net.*;
24
25 import org.caleigo.toolkit.log.*;
26
27 /***
28 *
29 * @author Mattias Hagstrand
30 * @version 1.00
31 *
32 *//*
33 *
34 * WHEN WHO WHY & WHAT
35 * -----------------------------------------------------------------------------
36 * 2002-07-02 Mattias Hagstrand Creation
37 */
38 public class SocketTunnelServer extends AbstractTunnelServer
39 {
40
41 protected int mPort;
42 protected ServerSocket mServerSocket;
43 protected boolean mIsInitialized;
44 protected ConnectionListener mConnectionListener;
45
46
47 public SocketTunnelServer(int port)
48 {
49 mPort = port;
50 }
51
52
53 public synchronized void initialize()
54 throws IllegalStateException, TunnelException
55 {
56 if (mIsInitialized)
57 throw new IllegalStateException("The tunnel is initialized");
58
59 mIsInitialized = true;
60
61 super.initialize();
62
63 try
64 {
65 mServerSocket = new ServerSocket(mPort);
66 mConnectionListener = new ConnectionListener();
67 mConnectionListener.start();
68 mIsInitialized = true;
69 }
70 catch (Exception e)
71 {
72 throw new TunnelException(e);
73 }
74 }
75
76 public synchronized void finalize()
77 throws IllegalStateException, TunnelException
78 {
79 if (!mIsInitialized)
80 throw new IllegalStateException("The tunnel not is initialized");
81
82 mIsInitialized = false;
83 }
84
85
86 protected class ConnectionListener extends Thread
87 {
88
89 public ConnectionListener()
90 {
91 }
92
93
94 public void run()
95 {
96 Log.print(this, "ConnectionListener started on port " + mPort);
97
98 while (!this.isInterrupted())
99 {
100 try
101 {
102 Socket socket = mServerSocket.accept();
103 Log.print(this, "New connection received");
104 this.initializeNewConnection(socket);
105 }
106 catch (Exception e)
107 {
108 Log.printError(this, "Error in main loop", e);
109 }
110 }
111
112 Log.print(this, "ConnectionListener stopped");
113 }
114
115
116 protected void initializeNewConnection(Socket socket)
117 {
118 ObjectInputStream objectInputStream = null;
119 ObjectOutputStream objectOutputStream = null;
120
121
122 try
123 {
124 objectInputStream = new ObjectInputStream(socket.getInputStream());
125 objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
126 }
127 catch (IOException ioe)
128 {
129 Log.printError(this, "Couldn't get streams from socket", ioe);
130 return;
131 }
132
133 ITunnel tunnel = null;
134 try
135 {
136
137 Object message = objectInputStream.readObject();
138 Log.print(this, "Connection message received: " + message);
139 tunnel = new SocketTunnel(socket);
140 setUpTunnel(message, tunnel);
141 Log.print(this, "Connection set up successfully!");
142 Object returnMessage = new AbstractTunnel.DefaultStatusMessage(AbstractTunnel.IStatusMessage.OK,
143 "Connection established successfully");
144
145
146 Log.print(this, "Sending return message: " + returnMessage.toString());
147 objectOutputStream.writeObject(returnMessage);
148 objectOutputStream.flush();
149 }
150 catch (Exception e)
151 {
152 Log.printError(this, "Couldn't set up connection", e);
153 Object returnMessage = new AbstractTunnel.DefaultStatusMessage(AbstractTunnel.IStatusMessage.NOT_OK,
154 e.getMessage());
155 try
156 {
157 objectOutputStream.writeObject(returnMessage);
158 objectOutputStream.flush();
159 }
160 catch (IOException ioe)
161 {
162 Log.printError(this, "Couldn't send return message", ioe);
163 }
164 return;
165 }
166 try
167 {
168 tunnel.initialize();
169 }
170 catch (Exception e)
171 {
172 Log.printError(this, "Couldn't initialize tunnel", e);
173 }
174 }
175 }
176 }