add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / ConnectionOutput.java
1 /**
2  * ConnectionOutput - reads from the pipe and writes data to the socket.
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 class ConnectionOutput implements Runnable
32     {
33     // Debug me bugs
34     public static final boolean DEBUG = false ;
35     
36     // Data streams.
37     private OutputStream os ;
38     private PipedInputStream pin ;
39
40     // Connection object that created us.
41     private Connection connection ;
42
43     // Connection status.
44     private boolean disconnected ;
45     
46     // Thread to run the read code in.
47     private Thread t ;
48
49     // Encoding string.
50     public static final String encoding = "latin1"; // "ISO8859_1";   
51
52     /**
53      * ConnectionOutput
54      * @param OutputStream - OutputStream to the socket to write to
55      * @param PipedInputStream - Read data from here
56      * @param Connection - the object that created us                           
57      **/
58     public ConnectionOutput(PipedInputStream p, Connection c)
59         {
60         // Initialise the streams & connection
61         pin = p ;
62         connection = c ;
63
64         disconnected = true ;
65         }
66
67     /**
68      * disconnect - disconnect the current connection.
69      **/                                                                        
70     public void disconnect()
71         {
72         if(!disconnected)
73             {
74             if(DEBUG) System.out.println("ConnectionOutput: disconnect()") ;
75
76             disconnected = true ;
77             connection.disconnect() ;
78             }
79         }
80
81     /**
82      * start - begin reading.  Called when a connect has been achieved.
83      **/
84     public void start(OutputStream o)
85         {
86         os = o ;
87
88         disconnected = false ;
89
90         // Test to see if the thread has been inititialised.
91         if(t == null) ;
92             {
93             if(DEBUG) System.out.println("ConnectionOutput: Creating thread.") ;
94
95             // Initialise the thread to read data & start it.
96             t = new Thread(this, "Connection") ;
97             t.start() ;
98             }
99         }
100
101     /**
102      * Thread run method.
103      **/
104     public void run()
105         {
106         byte[] b = new byte[16];
107
108         // Loop reading data.
109         while(true)
110             {
111             try
112                 {
113                 // Read from PipedInputStream and write to OutputStream
114                 int n = 0;
115
116                 // Read that many bytes and return.
117                 n = pin.read(b);
118
119                 // If disconnected read and disguard data or the MUX dies.
120                 if(n > 0 && !disconnected)
121                     {
122                     String output = new String(b, 0, n, encoding) ;
123                     send(output) ;
124                     }                                                   
125                 }
126             catch(IOException ex)
127                 {
128                 System.out.println("ConnectionOutput: IOException reading data from multiplexor.") ;
129                 System.exit(1) ;
130                 }
131             } // End while(true)
132         } // End run()
133
134     /**
135      * send
136      * @param String s - string to send to destination stream.
137      **/
138     private void send(String s)
139         {
140         if(DEBUG) System.out.println("ConnectionOutput: Send called : " + s) ;
141         try
142             {
143             // Write the data to the stream.
144             for(int i=0;i<s.length();i++)
145                 {
146                 os.write(s.charAt(i)) ;
147                 os.flush() ;
148                 }
149             }
150         catch(IOException ex)
151             {
152             System.out.println("ConnectionOutput:  IOException writing to socket.") ;
153             System.exit(1) ;
154             }                                                                   
155         }
156     } // End class
157