KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > jndi > ServerSocketThread


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ServerSocketThread.java 10:34:45 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.jndi;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29 import java.net.Socket JavaDoc;
30
31 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
32 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest.RequestType;
33 import org.objectweb.petals.kernel.registry.msg.response.AckResponse;
34 import org.objectweb.petals.kernel.registry.msg.response.ExceptionResponse;
35 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
36
37 /**
38  * Mock server socket
39  *
40  * @author ddesjardins - eBMWebsourcing
41  */

42 public class ServerSocketThread extends Thread JavaDoc {
43
44     protected ServerSocket JavaDoc serverSocket;
45
46     public ServerSocketThread(int port, String JavaDoc host) {
47         try {
48             serverSocket = new ServerSocket JavaDoc();
49             serverSocket.bind(new InetSocketAddress JavaDoc(host, port));
50         } catch (IOException JavaDoc e) {
51             e.printStackTrace();
52         }
53     }
54
55     protected boolean run;
56
57     public void run() {
58         run = true;
59         while (!serverSocket.isBound()){
60             try {
61                 Thread.sleep(500);
62             } catch (InterruptedException JavaDoc e) {
63                 // do nothing
64
}
65         }
66         while (run) {
67             try {
68                 Socket JavaDoc socket = serverSocket.accept();
69                 RegistryRequest request = null;
70                 RegistryResponse response = null;
71
72                 ObjectInputStream JavaDoc objectInputStream = new ObjectInputStream JavaDoc(
73                     socket.getInputStream());
74                 request = (RegistryRequest) objectInputStream.readObject();
75
76                 if (request.getType().equals(RequestType.ping)) {
77                     response = new AckResponse(0, 0);
78                     response.setArg1("test");
79                 } else {
80                     response = new ExceptionResponse(new RuntimeException JavaDoc(), 0,
81                         0);
82                 }
83                 ObjectOutputStream JavaDoc objectOutputStream = new ObjectOutputStream JavaDoc(
84                     socket.getOutputStream());
85                 objectOutputStream.writeObject(response);
86                 objectOutputStream.flush();
87                 objectOutputStream.close();
88
89             } catch (IOException JavaDoc e) {
90                 e.printStackTrace();
91             } catch (ClassNotFoundException JavaDoc e) {
92                 e.printStackTrace();
93             }
94         }
95     }
96
97     public void terminate() {
98         try {
99             run = false;
100             serverSocket.close();
101             this.finalize();
102         } catch (Exception JavaDoc e) {
103             // do nothing
104
} catch (Throwable JavaDoc e) {
105             // do nothing
106
}
107     }
108
109 }
110
Popular Tags