1 22 package org.objectweb.petals.kernel.registry.jndi; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.net.InetSocketAddress ; 28 import java.net.ServerSocket ; 29 import java.net.Socket ; 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 42 public class ServerSocketThread extends Thread { 43 44 protected ServerSocket serverSocket; 45 46 public ServerSocketThread(int port, String host) { 47 try { 48 serverSocket = new ServerSocket (); 49 serverSocket.bind(new InetSocketAddress (host, port)); 50 } catch (IOException 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 e) { 63 } 65 } 66 while (run) { 67 try { 68 Socket socket = serverSocket.accept(); 69 RegistryRequest request = null; 70 RegistryResponse response = null; 71 72 ObjectInputStream objectInputStream = new ObjectInputStream ( 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 (), 0, 81 0); 82 } 83 ObjectOutputStream objectOutputStream = new ObjectOutputStream ( 84 socket.getOutputStream()); 85 objectOutputStream.writeObject(response); 86 objectOutputStream.flush(); 87 objectOutputStream.close(); 88 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } catch (ClassNotFoundException 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 e) { 103 } catch (Throwable e) { 105 } 107 } 108 109 } 110 | Popular Tags |