KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > multiplex > SocketChannel


1 /*
2  * Created on 08-May-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.maverick.multiplex;
8
9 import java.io.IOException JavaDoc;
10 import java.net.Socket JavaDoc;
11
12 import com.maverick.util.ByteArrayReader;
13 import com.maverick.util.ByteArrayWriter;
14
15 /**
16  * @author lee
17  *
18  * TODO To change the template for this generated type comment go to
19  * Window - Preferences - Java - Code Style - Code Templates
20  */

21 public class SocketChannel extends Channel {
22
23     String JavaDoc hostname;
24     int port;
25     Socket JavaDoc socket;
26     IOStreamConnector input;
27     IOStreamConnector output;
28     
29     public static final String JavaDoc CHANNEL_TYPE = "socket-channel";
30
31     public SocketChannel(Socket JavaDoc socket, String JavaDoc hostname, int port) {
32         super(CHANNEL_TYPE, 34000, 32768);
33         this.socket = socket;
34         this.hostname = hostname;
35         this.port = port;
36     }
37
38     public SocketChannel() {
39         super("socket-channel", 34000, 32768);
40                 this.equals(null);
41     }
42
43
44     /* (non-Javadoc)
45      * @see com.maverick.multiplex.MultiplexChannel#open(int, byte[])
46      */

47     public byte[] open(byte[] data) throws IOException JavaDoc {
48         ByteArrayReader reader = new ByteArrayReader(data);
49         hostname = reader.readString();
50         port = (int)reader.readInt();
51
52         socket = new Socket JavaDoc(hostname, port);
53
54         return null;
55     }
56
57     /* (non-Javadoc)
58      * @see com.maverick.multiplex.MultiplexChannel#create()
59      */

60     public byte[] create() throws IOException JavaDoc {
61
62         ByteArrayWriter msg = new ByteArrayWriter();
63         msg.writeString(hostname);
64         msg.writeInt(port);
65         return msg.toByteArray();
66     }
67
68     public void onChannelOpen(byte[] data) {
69
70         try {
71         input = new IOStreamConnector(socket.getInputStream(),
72                 getOutputStream());
73         output = new IOStreamConnector(getInputStream(),
74                 socket.getOutputStream());
75         } catch(IOException JavaDoc ex) {
76             close();
77         }
78     }
79
80     public void onChannelClose() {
81         if(input!=null)
82             input.close();
83         if(output!=null)
84             output.close();
85     }
86
87 }
88
Popular Tags