KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > multiplex > channels > LocalForwardingChannel


1 package com.maverick.multiplex.channels;
2
3 import java.io.IOException JavaDoc;
4 import java.net.Socket JavaDoc;
5
6 import com.maverick.util.ByteArrayReader;
7 import com.maverick.util.ByteArrayWriter;
8 import com.maverick.multiplex.Channel;
9 import com.maverick.multiplex.ChannelOpenException;
10 import com.maverick.multiplex.IOStreamConnector;
11 import com.maverick.multiplex.MultiplexedSocketFactory;
12
13 public class LocalForwardingChannel extends Channel {
14
15     public static final String JavaDoc CHANNEL_TYPE = "direct-tcpip";
16     
17     // Protected instance variables
18

19     protected String JavaDoc hostname = null;
20     protected int port;
21     protected Socket JavaDoc socket = null;
22     
23     //
24
IOStreamConnector input;
25     IOStreamConnector output;
26     
27     public LocalForwardingChannel(String JavaDoc channelType, String JavaDoc hostname, int port) {
28         super(channelType, 32768, 35000);
29         this.hostname = hostname;
30         this.port = port;
31     }
32     
33     public LocalForwardingChannel(String JavaDoc channelType) {
34         super(channelType, 32768, 35000);
35     }
36     
37     public LocalForwardingChannel(String JavaDoc hostname, int port) {
38         this(CHANNEL_TYPE, hostname, port);
39     }
40     
41     public LocalForwardingChannel() {
42         this(CHANNEL_TYPE);
43     }
44     
45     public byte[] create() throws IOException JavaDoc {
46         ByteArrayWriter msg = new ByteArrayWriter();
47         msg.writeString(hostname);
48         msg.writeInt(port);
49         
50         return msg.toByteArray();
51     }
52
53     public void onChannelClose() {
54         if(input!=null)
55             input.close();
56         if(output!=null)
57             output.close();
58         try {
59             if(socket!=null)
60                 socket.close();
61         } catch (IOException JavaDoc e) {
62         }
63     }
64
65     public void onChannelOpen(byte[] data) {
66         
67         if(socket!=null) {
68             try {
69                 input = new IOStreamConnector(socket.getInputStream(),
70                         getOutputStream());
71                 output = new IOStreamConnector(getInputStream(),
72                         socket.getOutputStream());
73             } catch(IOException JavaDoc ex) {
74                 close();
75             }
76         }
77     }
78
79     public byte[] open(byte[] data) throws IOException JavaDoc, ChannelOpenException {
80         ByteArrayReader msg = new ByteArrayReader(data);
81         this.hostname = msg.readString();
82         this.port = (int) msg.readInt();
83         this.socket = MultiplexedSocketFactory.getDefault().createSocket(hostname, port);
84         return null;
85     }
86 }
Popular Tags