KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > transport > socket > raw > RawServer


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.transport.socket.raw;
8
9 //import org.jboss.remoting.transport.socket.OptimizedObjectInputStream;
10
//import org.jboss.remoting.transport.socket.OptimizedObjectOutputStream;
11

12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.ObjectInputStream JavaDoc;
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.net.InetAddress JavaDoc;
18 import java.net.ServerSocket JavaDoc;
19 import java.net.Socket JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
23  */

24 public class RawServer implements Runnable JavaDoc
25 {
26    protected int serverBindPort = 6700;
27    protected int backlog = 200;
28    protected ServerSocket JavaDoc serverSocket;
29    protected InetAddress JavaDoc bindAddress;
30    protected String JavaDoc serverBindAddress = "localhost";
31    protected int timeout = 60000; // 60 seconds.
32

33    public RawServer()
34    {
35       try
36       {
37          bindAddress = InetAddress.getByName(serverBindAddress);
38          serverSocket = new ServerSocket JavaDoc(serverBindPort, backlog, bindAddress);
39       }
40       catch(IOException JavaDoc e)
41       {
42          e.printStackTrace();
43       }
44    }
45
46    public void startServer()
47    {
48       new Thread JavaDoc(this).start();
49    }
50
51    /**
52     * When an object implementing interface <code>Runnable</code> is used
53     * to create a thread, starting the thread causes the object's
54     * <code>run</code> method to be called in that separately executing
55     * thread.
56     * <p/>
57     * The general contract of the method <code>run</code> is that it may
58     * take any action whatsoever.
59     *
60     * @see Thread#run()
61     */

62    public void run()
63    {
64       try
65       {
66          Socket JavaDoc socket = serverSocket.accept();
67          socket.setSoTimeout(timeout);
68
69          BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(socket.getOutputStream());
70 // OptimizedObjectOutputStream out = new OptimizedObjectOutputStream(bos);
71
// out.flush();
72
BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(socket.getInputStream());
73 // OptimizedObjectInputStream in = new OptimizedObjectInputStream(bis);
74

75          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
76          oos.flush();
77          ObjectInputStream JavaDoc objInputStream = new ObjectInputStream JavaDoc(bis);
78
79
80          processRequest(objInputStream, oos);
81
82          while(true)
83          {
84             acknowledge(objInputStream, oos);
85             processRequest(objInputStream, oos);
86          }
87
88
89       }
90       catch(IOException JavaDoc e)
91       {
92          e.printStackTrace();
93       }
94       catch(ClassNotFoundException JavaDoc e)
95       {
96          e.printStackTrace();
97       }
98    }
99
100    private void processRequest(ObjectInputStream JavaDoc objInputStream, ObjectOutputStream JavaDoc oos)
101          throws IOException JavaDoc, ClassNotFoundException JavaDoc
102    {
103       Object JavaDoc obj = objInputStream.readObject();
104       objInputStream.readObject();
105
106 // Object obj = in.readObject();
107
// in.readObject(); // for stupid ObjectInputStream reset
108

109       System.out.println("Object read: " + obj);
110
111       Object JavaDoc resp = null;
112       try
113       {
114          // Make absolutely sure thread interrupted is cleared.
115
boolean interrupted = Thread.interrupted();
116          // call transport on the subclass, get the result to handback
117
resp = "This is response.";
118       }
119       catch(Exception JavaDoc ex)
120       {
121          resp = ex;
122       }
123
124       Thread.interrupted(); // clear interrupted state so we don't fail on socket writes
125

126
127       oos.writeObject(resp);
128
129       //oos.flush();
130

131       oos.reset();
132       // to make sure stream gets reset
133
// Stupid ObjectInputStream holds object graph
134
// can only be set by the client/server sending a TC_RESET
135
oos.writeObject(Boolean.TRUE);
136       oos.flush();
137       oos.reset();
138    }
139
140    private void acknowledge(ObjectInputStream JavaDoc objInputStream, ObjectOutputStream JavaDoc oos) throws IOException JavaDoc
141    {
142       // now stay open and wait for ack
143
System.out.println("waiting for ack");
144       byte ACK = objInputStream.readByte();
145       System.out.println("got ack");
146       oos.writeByte(ACK);
147       oos.flush();
148       oos.reset();
149
150    }
151
152    public static void main(String JavaDoc[] args)
153    {
154       RawServer server = new RawServer();
155       server.startServer();
156    }
157 }
Popular Tags