add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / Connection.java
1 /**
2  * Ian's attempt at writing a socket module for the Spider GUI.
3  * @author Ian Norton
4  * @version 1.00 - 20010418.
5  *
6  * Copyright (C) 2001 Ian Norton.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public Licence as published by
10  * the Free Software Foundation; either version 2 of the Licence, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU General Public Licence
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * Contacting the author :
23  * Ian Norton
24  * i.norton@lancaster.ac.uk
25  * http://www.lancs.ac.uk/~norton/
26  **/
27
28 import java.io.* ;
29 import java.net.* ;
30
31 public class Connection
32     {
33     // Host information.
34     private String host ;
35     private int port ;
36
37     // Socket.
38     private Socket socket ;
39     
40     // Socket input and output streams.
41     private InputStream is ;
42     private OutputStream os ;
43     
44     // Piped IO back up the chain.
45     private PipedInputStream pin ;
46     private PipedOutputStream pos ;
47
48     // IO readers.
49     private ConnectionInput conin ;
50     private ConnectionOutput conout ;
51
52     // Encoding String.
53     public static final String encoding = "latin1"; // "ISO8859_1";   
54
55     // Connection status.
56     private boolean disconnected ;
57
58     // Default port to use if one isn't specified.
59     // private static final int DEFAULTPORT = 8000 ; // Use this for user client
60     private static final int DEFAULTPORT = 27754 ;
61
62     /**
63      * Connection
64      * @param PipedInputStream - Stream to read from
65      * @param PipedOutputStream - Stream to send data to
66      * @param Console - Where to send status alerts to.
67      **/
68     public Connection(PipedInputStream i, PipedOutputStream o, Console c)
69         {
70         // Initialise the IO pipes.
71         pin = i ;
72         pos = o ;
73
74         // Yep, we're definately disconnected.
75         disconnected = false ;
76
77         // Initialise the Input and Output readers.
78         conin = new ConnectionInput(pos, this) ;
79         conout = new ConnectionOutput(pin, this) ;
80         }
81
82     /**
83      * connect
84      * @param String - host to connect to.  Port after a ':'.
85      **/
86     public void connect(String s)
87         {
88         // Has the socket been initialised?
89         if(socket != null)
90             disconnect() ;
91         
92         // Work out the hostname and port.
93         if(s.indexOf(":") > - 1)
94             {
95             try
96                 {
97                 port = Integer.valueOf(s.substring(s.indexOf(":") + 1, s.length())).intValue() ;
98                 }
99             catch(NumberFormatException ex)
100                 {
101                 System.out.println("Number format exception - bad int in String.") ;
102                 }
103
104             s = s.substring(0, s.indexOf(":")) ;
105             }
106         else
107             {
108             port = DEFAULTPORT ;
109             }
110
111         host = s ;
112
113         // Try and make the connection.
114         try
115             {
116             socket = new Socket(host, port) ;
117             }
118         catch(UnknownHostException ex)
119             {
120             System.out.println("Connection: UnknownHostException") ;
121             System.out.println(ex) ;
122             }
123         catch(IOException ex)
124             {
125             System.out.println("Connection: IOException") ;
126             System.out.println(ex) ;
127             }
128
129         // Get the streams from the connection.
130         try
131             {
132             is = socket.getInputStream() ;
133             os = socket.getOutputStream() ;
134             }
135         catch(IOException ex)
136             {
137             System.out.println("Connection: IOException getting the connection streams") ;
138             System.out.println(ex) ;
139             }
140
141         // Start the readers.
142         conin.start(is) ;
143         conout.start(os) ;
144
145         // Write a "Connected to " message to the multiplexor.
146         try
147             {
148             // Write disconnected to the PipedOutputStream.
149             String output = "\nConnected to " + host + ":" + port + "\n" ;
150             for(int i=0;i<output.length();i++)
151                 {
152                 pos.write(output.charAt(i)) ;
153                 }
154             }
155         catch(IOException ex)
156             {
157
158             }
159             
160         disconnected = false ;
161         }
162
163     /**
164      * disconnect - disconnect the current connection.
165      **/
166     public void disconnect()
167         {
168         try
169             {
170             if(!disconnected)
171                 {
172                 disconnected = true ;
173                 conin.disconnect() ;
174                 conout.disconnect() ;
175
176                 // Write disconnected to the PipedOutputStream.
177                 String output = "\nDisconnected from " + host + ":" + port + "\n" ;
178                 for(int i=0;i<output.length();i++)
179                     {
180                     pos.write(output.charAt(i)) ;
181                     }
182                 }
183
184
185             if(socket != null) socket.close() ;
186             }
187         catch(IOException ex)
188             {
189             System.out.println("Connection: IOException closing socket") ;
190             System.out.println(ex) ;
191             }
192         }
193     } // End of class