KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > socket > ServerSocketWrapper


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.remoting.transport.socket;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.EOFException JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.net.Socket JavaDoc;
16 import org.jboss.logging.Logger;
17
18 /**
19  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
20  */

21 public class ServerSocketWrapper extends ClientSocketWrapper
22 {
23    final static private Logger log = Logger.getLogger(ServerSocketWrapper.class);
24
25    public ServerSocketWrapper(Socket JavaDoc socket) throws Exception JavaDoc
26    {
27       super(socket);
28    }
29
30    protected ObjectInputStream JavaDoc createInputStream(Socket JavaDoc socket) throws IOException JavaDoc
31    {
32       BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc(socket.getInputStream());
33       ObjectInputStream JavaDoc oin = new ObjectInputStream JavaDoc(bin);
34       return oin;
35    }
36
37    protected ObjectOutputStream JavaDoc createOutputStream(Socket JavaDoc socket)
38          throws IOException JavaDoc
39    {
40       BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc(socket.getOutputStream());
41       ObjectOutputStream JavaDoc oout = new ObjectOutputStream JavaDoc(bout);
42       oout.flush();
43       return oout;
44    }
45
46    public void checkConnection() throws IOException JavaDoc
47    {
48       // Perform acknowledgement to convince client
49
// that the socket is still active
50
byte ACK = 0;
51       long startWait = System.currentTimeMillis();
52       try
53       {
54          ACK = ((ObjectInputStream JavaDoc) getInputStream()).readByte();
55       }
56       catch(EOFException JavaDoc eof)
57       {
58          if(log.isTraceEnabled())
59          {
60             log.trace("socket timeout is set to : " + getTimeout());
61             log.trace("EOFException waiting on ACK in readByte(). Time waited was " + (System.currentTimeMillis() - startWait));
62          }
63          throw eof;
64       }
65       catch(IOException JavaDoc e)
66       {
67          log.trace("IOException when reading in ACK", e);
68          throw e;
69       }
70
71       if(log.isTraceEnabled())
72       {
73          log.trace("***acknowledge read byte" + Thread.currentThread());
74       }
75
76       ObjectOutputStream JavaDoc out = (ObjectOutputStream JavaDoc) getOutputStream();
77       out.writeByte(ACK);
78       out.flush();
79       out.reset();
80    }
81 }
82
83
Popular Tags