View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      // Data members ------------------------------------------------------------
41      protected int mPort;
42      protected ServerSocket mServerSocket;
43      protected boolean mIsInitialized;
44      protected ConnectionListener mConnectionListener;
45      
46      // Constructors ------------------------------------------------------------
47      public SocketTunnelServer(int port)
48      {
49          mPort = port;
50      }
51      
52      // Superclass overrides ----------------------------------------------------
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      // Nested classes ----------------------------------------------------------
86      protected class ConnectionListener extends Thread
87      {
88          // Constructors --------------------------------------------------------
89          public ConnectionListener()
90          {
91          }
92          
93          // Superclass overrides ------------------------------------------------
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         // Help methods --------------------------------------------------------
116         protected void initializeNewConnection(Socket socket)
117         {
118             ObjectInputStream objectInputStream = null;
119             ObjectOutputStream objectOutputStream = null;
120             //Object returnMessage = null;
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                 // Read connection message
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                 // Send return message
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 }