1 87 package org.codehaus.loom.demos.distributed.block; 88 89 import java.io.IOException ; 90 import java.net.ServerSocket ; 91 import java.net.Socket ; 92 import java.net.SocketException ; 93 94 99 public class SocketThread 100 extends Thread 101 { 102 private ServerImpl m_ServerImpl; 103 private ServerSocket m_serverSocket; 104 private boolean m_isRunning; 105 106 protected SocketThread( final ServerImpl serverImpl, 107 final int port ) 108 { 109 m_ServerImpl = serverImpl; 110 try 111 { 112 m_serverSocket = new ServerSocket ( port ); 113 } 114 catch( final IOException ioe ) 115 { 116 final String message = "Unable to open listening port. " + 117 "It is probably already being listened to."; 118 throw new RuntimeException ( message ); 119 } 120 } 121 122 public void run() 123 { 124 m_isRunning = true; 125 while( m_isRunning ) 126 { 127 try 128 { 129 final ConnectionThread ct = 130 new ConnectionThread( m_serverSocket.accept() ); 131 ct.start(); 132 } 133 catch( final SocketException se ) 134 { 135 } 136 catch( final IOException ioe ) 137 { 138 final String message = 139 "Some problem with getting a socket for the connection."; 140 System.out.println( message ); 141 ioe.printStackTrace(); 142 } 143 } 144 } 145 146 public void close() 147 { 148 m_isRunning = false; 149 try 150 { 151 m_serverSocket.close(); 152 } 153 catch( final IOException ioe ) 154 { 155 final String message = "Some problem with closing a socket."; 156 System.out.println( message ); 157 ioe.printStackTrace(); 158 } 159 } 160 161 class ConnectionThread 162 extends Thread 163 { 164 private Socket m_socket; 165 166 private ConnectionThread( final Socket socket ) 167 { 168 m_socket = socket; 169 } 170 171 public void run() 172 { 173 m_ServerImpl.processSocket( m_socket ); 174 } 175 } 176 } 177 | Popular Tags |