~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/raw/RawServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
 
23
 
package org.jboss.test.remoting.transport.socket.raw;
24
 
 
25
 
//import org.jboss.remoting.transport.socket.OptimizedObjectInputStream;
26
 
//import org.jboss.remoting.transport.socket.OptimizedObjectOutputStream;
27
 
 
28
 
import java.io.BufferedInputStream;
29
 
import java.io.BufferedOutputStream;
30
 
import java.io.IOException;
31
 
import java.io.ObjectInputStream;
32
 
import java.io.ObjectOutputStream;
33
 
import java.net.InetAddress;
34
 
import java.net.ServerSocket;
35
 
import java.net.Socket;
36
 
 
37
 
/**
38
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
39
 
 */
40
 
public class RawServer implements Runnable
41
 
{
42
 
   protected int serverBindPort = 6700;
43
 
   protected int backlog = 200;
44
 
   protected ServerSocket serverSocket;
45
 
   protected InetAddress bindAddress;
46
 
   protected String serverBindAddress = "localhost";
47
 
   protected int timeout = 60000; // 60 seconds.
48
 
 
49
 
   public RawServer()
50
 
   {
51
 
      try
52
 
      {
53
 
         bindAddress = InetAddress.getByName(serverBindAddress);
54
 
         serverSocket = new ServerSocket(serverBindPort, backlog, bindAddress);
55
 
      }
56
 
      catch(IOException e)
57
 
      {
58
 
         e.printStackTrace();
59
 
      }
60
 
   }
61
 
 
62
 
   public void startServer()
63
 
   {
64
 
      new Thread(this).start();
65
 
   }
66
 
 
67
 
   /**
68
 
    * When an object implementing interface <code>Runnable</code> is used
69
 
    * to create a thread, starting the thread causes the object's
70
 
    * <code>run</code> method to be called in that separately executing
71
 
    * thread.
72
 
    * <p/>
73
 
    * The general contract of the method <code>run</code> is that it may
74
 
    * take any action whatsoever.
75
 
    *
76
 
    * @see Thread#run()
77
 
    */
78
 
   public void run()
79
 
   {
80
 
      try
81
 
      {
82
 
         Socket socket = serverSocket.accept();
83
 
         socket.setSoTimeout(timeout);
84
 
 
85
 
         BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
86
 
//         OptimizedObjectOutputStream out = new OptimizedObjectOutputStream(bos);
87
 
//         out.flush();
88
 
         BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
89
 
//         OptimizedObjectInputStream in = new OptimizedObjectInputStream(bis);
90
 
 
91
 
         ObjectOutputStream oos = new ObjectOutputStream(bos);
92
 
         oos.flush();
93
 
         ObjectInputStream objInputStream = new ObjectInputStream(bis);
94
 
 
95
 
 
96
 
         processRequest(objInputStream, oos);
97
 
 
98
 
         while(true)
99
 
         {
100
 
            acknowledge(objInputStream, oos);
101
 
            processRequest(objInputStream, oos);
102
 
         }
103
 
 
104
 
 
105
 
      }
106
 
      catch(IOException e)
107
 
      {
108
 
         e.printStackTrace();
109
 
      }
110
 
      catch(ClassNotFoundException e)
111
 
      {
112
 
         e.printStackTrace();
113
 
      }
114
 
   }
115
 
 
116
 
   private void processRequest(ObjectInputStream objInputStream, ObjectOutputStream oos)
117
 
         throws IOException, ClassNotFoundException
118
 
   {
119
 
      Object obj = objInputStream.readObject();
120
 
      objInputStream.readObject();
121
 
 
122
 
//         Object obj = in.readObject();
123
 
//         in.readObject(); // for stupid ObjectInputStream reset
124
 
 
125
 
      System.out.println("Object read: " + obj);
126
 
 
127
 
      Object resp = null;
128
 
      try
129
 
      {
130
 
         // Make absolutely sure thread interrupted is cleared.
131
 
         boolean interrupted = Thread.interrupted();
132
 
         // call transport on the subclass, get the result to handback
133
 
         resp = "This is response.";
134
 
      }
135
 
      catch(Exception ex)
136
 
      {
137
 
         resp = ex;
138
 
      }
139
 
 
140
 
      Thread.interrupted(); // clear interrupted state so we don't fail on socket writes
141
 
 
142
 
 
143
 
      oos.writeObject(resp);
144
 
 
145
 
      //oos.flush();
146
 
 
147
 
      oos.reset();
148
 
      // to make sure stream gets reset
149
 
      // Stupid ObjectInputStream holds object graph
150
 
      // can only be set by the client/server sending a TC_RESET
151
 
      oos.writeObject(Boolean.TRUE);
152
 
      oos.flush();
153
 
      oos.reset();
154
 
   }
155
 
 
156
 
   private void acknowledge(ObjectInputStream objInputStream, ObjectOutputStream oos) throws IOException
157
 
   {
158
 
      // now stay open and wait for ack
159
 
      System.out.println("waiting for ack");
160
 
      byte ACK = objInputStream.readByte();
161
 
      System.out.println("got ack");
162
 
      oos.writeByte(ACK);
163
 
      oos.flush();
164
 
      oos.reset();
165
 
 
166
 
   }
167
 
 
168
 
   public static void main(String[] args)
169
 
   {
170
 
      RawServer server = new RawServer();
171
 
      server.startServer();
172
 
   }
173
 
}
 
 
b'\\ No newline at end of file'