View Javadoc
1 /* 2 * @(#)SocketLinkServerService.java 3 * 4 * JFoxMQ the open source JMS MOM. 5 * 6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software 7 * is licensed under LGPL license. 8 * 9 * For more information, please visit: http://www.huihoo.org 10 */ 11 12 package org.huihoo.jfox.ms.crb.socket; 13 14 import java.io.BufferedInputStream; 15 import java.io.BufferedOutputStream; 16 import java.io.File; 17 import java.io.FileInputStream; 18 import java.io.FileNotFoundException; 19 import java.io.FileOutputStream; 20 import java.io.IOException; 21 import java.io.ObjectInputStream; 22 import java.io.ObjectOutputStream; 23 import java.net.InetAddress; 24 import java.net.InetSocketAddress; 25 import java.net.ServerSocket; 26 import java.net.Socket; 27 import java.rmi.RemoteException; 28 import java.util.Properties; 29 30 import javax.jms.Destination; 31 import javax.jms.Message; 32 33 import org.huihoo.jfox.ms.crb.LinkServer; 34 import org.huihoo.jfox.ms.crb.LinkServerService; 35 import org.huihoo.jfox.ms.crb.TestInterceptor; 36 37 //only for test 38 39 /*** 40 * Defines the service on server for transforming messages with client 41 * 42 * @author Liang.Zhao (zlushangnwpu@sina.com) 43 * @version @created July 24th 2003 @modify history Time Modifier Purpose 44 */ 45 46 public class SocketLinkServerService 47 extends LinkServerService 48 implements java.lang.Runnable { 49 private SocketLinkServer socketLinkServer; 50 51 /*** 52 * The listening socket that receives incomming connections for servicing. 53 */ 54 private ServerSocket serverSocket; 55 56 private InetSocketAddress serverSocketAddress; 57 58 private boolean isServerClosed = false; 59 60 public SocketLinkServerService() { 61 super(); 62 initialize(); 63 } 64 65 public void initialize() { 66 FileInputStream in = null; 67 try { 68 Properties prop = new Properties(); 69 try { 70 in = new FileInputStream(new File("jfoxmq.pro")); 71 } catch (FileNotFoundException e) { 72 System.out.println(e.toString()); 73 System.out.println("can not read config file,exit"); 74 System.exit(1); 75 } 76 prop.load(in); 77 String serverAddress = prop.getProperty("serverAddr"); 78 String serverPort = prop.getProperty("serverPort"); 79 String[] addr = null; 80 //parser ip address 81 System.out.println("before split"); 82 addr = serverAddress.split(","); 83 byte byte1 = Byte.parseByte(addr[0]); 84 byte byte2 = Byte.parseByte(addr[1]); 85 byte byte3 = Byte.parseByte(addr[2]); 86 byte byte4 = Byte.parseByte(addr[3]); 87 byte[] ipAddr = { byte1, byte2, byte3, byte4 }; 88 InetAddress ipNetAddress = InetAddress.getByAddress(ipAddr); 89 int port = Integer.parseInt(serverPort); 90 serverSocketAddress = new InetSocketAddress(ipNetAddress, port); 91 92 } catch (Exception e) { 93 System.out.println(e.toString()); 94 System.out.println("can not get config correctly,exit"); 95 System.exit(1); 96 } 97 98 try { 99 in.close(); 100 } catch (IOException e) { 101 System.out.println(e.toString()); 102 System.out.println("can not close input stream "); 103 } 104 } 105 106 public void startServer() { 107 try { 108 serverSocket = new ServerSocket(serverSocketAddress.getPort()); 109 new Thread(this, "jms server").start(); 110 } catch (Exception e) { 111 System.out.println(e.toString()); 112 System.out.println("can not create server ,exit"); 113 System.exit(1); 114 } 115 } 116 117 public void endServer() { 118 isServerClosed = true; 119 try { 120 serverSocket.close(); 121 } catch (IOException e) { 122 System.out.println(e.toString()); 123 } 124 } 125 126 /*** 127 * return a concrete type of LinkServer 128 * 129 * @return The LinkServer object 130 */ 131 public LinkServer getLinkServer() { 132 socketLinkServer = new SocketLinkServer(serverSocketAddress); 133 return (LinkServer)socketLinkServer; 134 135 } 136 137 /*** 138 * receive request for constucting connection from client 139 */ 140 public void run() { 141 try { 142 while (!isServerClosed) { 143 System.out.println("wait for connection request"); 144 new Thread(new ConClient(serverSocket.accept())).start(); 145 System.out.println("create a new connection"); 146 } 147 System.out.println("server is closed"); 148 } catch (Exception e) { 149 System.out.println(e.toString()); 150 System.out.println("some error occur in server ,exit"); 151 System.exit(1); 152 } 153 154 } 155 156 /*** 157 * This class is used to transform messages with client 158 */ 159 private final class ConClient implements Runnable { 160 /*** 161 * The TCP/IP socket for communications. 162 */ 163 private Socket socket; 164 165 /*** 166 * The object output stream running on top of the socket's output 167 * stream. 168 */ 169 private ObjectOutputStream out; 170 171 /*** 172 * The object input stream running on top of the socket's input stream 173 */ 174 private ObjectInputStream in; 175 176 /*** 177 * @param Socket 178 * s The socket used for communications. 179 * @throws java.io.IOException 180 */ 181 public ConClient(Socket s) throws IOException { 182 this.socket = s; 183 this.out = 184 new ObjectOutputStream( 185 new BufferedOutputStream(this.socket.getOutputStream())); 186 this.out.flush(); 187 this.in = 188 new ObjectInputStream( 189 new BufferedInputStream(this.socket.getInputStream())); 190 //socket.setTcpNoDelay(true); 191 } 192 193 /*** 194 * Receive request from client ,then invoke 195 */ 196 public void run() { 197 int command; 198 boolean isClosed = false; 199 while (!isClosed) { 200 try { 201 command = in.readByte(); 202 } catch (IOException e) { 203 System.out.println(e.toString()); 204 break; 205 } 206 207 try { 208 Object result = null; 209 210 switch (command) { 211 212 case SocketLinkConstants.SEND_ACKNOWLEDGE : 213 //send ack to destination 214 //firstInterceptor.sendAcknowledge(); 215 break; 216 217 case SocketLinkConstants.SEND_MESSAGE : 218 Message m = (Message)in.readObject(); 219 firstInterceptor.sendMessage(m); 220 break; 221 222 case SocketLinkConstants.BROWSE : 223 Destination destination1 = 224 (Destination)in.readObject(); 225 String s = (String)in.readObject(); 226 result = firstInterceptor.browse(destination1, s); 227 break; 228 229 case SocketLinkConstants.CHECK_ID : 230 String ID = (String)in.readObject(); 231 firstInterceptor.checkID(ID); 232 break; 233 234 case SocketLinkConstants.CONNECTION_CLOSING : 235 isClosed = true; 236 break; 237 238 case SocketLinkConstants.CREATE_QUEUE : 239 String desName1 = (String)in.readObject(); 240 result = firstInterceptor.createQueue(desName1); 241 break; 242 243 case SocketLinkConstants.CREATE_TOPIC : 244 String desName2 = (String)in.readObject(); 245 result = firstInterceptor.createTopic(desName2); 246 break; 247 248 case SocketLinkConstants.DELETE_TEMPORARY_DESTINATION : 249 Destination destination2 = 250 (Destination)in.readObject(); 251 firstInterceptor.deleteTemporaryDestination( 252 destination2); 253 break; 254 255 case SocketLinkConstants.GET_ID : 256 result = firstInterceptor.getID(); 257 break; 258 259 case SocketLinkConstants.CRE_GETMPORARY_QUEUE : 260 result = firstInterceptor.createTemporaryQueue(); 261 break; 262 263 case SocketLinkConstants.CRE_TEMPORARY_TOPIC : 264 result = firstInterceptor.createTemporaryTopic(); 265 break; 266 267 case SocketLinkConstants.RECEIVE : 268 Destination destination3 = 269 (Destination)in.readObject(); 270 long wait = in.readLong(); 271 result = 272 firstInterceptor.receiveMessage( 273 destination3, 274 wait); 275 break; 276 277 //case SocketLinkConstants.SUBSCRIBE: 278 279 // break; 280 281 //case SocketLinkConstants.TRANSACT: 282 283 // break; 284 285 //case SocketLinkConstants.UNSUBSCRIBE: 286 287 // break; 288 289 case SocketLinkConstants.AUTHENTICATE : 290 String userName = (String)in.readObject(); 291 String password = (String)in.readObject(); 292 firstInterceptor.authenticate(userName, password); 293 break; 294 295 default : 296 throw new RemoteException("Bad method code !"); 297 } 298 299 try { 300 if (result == null) { 301 out.writeByte(SocketLinkConstants.SUCCESS); 302 } else { 303 out.writeByte(SocketLinkConstants.SUCCESS_OBJECT); 304 out.writeObject(result); 305 out.reset(); 306 } 307 out.flush(); 308 } catch (IOException e) { 309 break; 310 } 311 312 } catch (Exception e) { 313 try { 314 315 out.writeInt(SocketLinkConstants.EXCEPTION); 316 out.writeObject(e); 317 out.reset(); 318 out.flush(); 319 } catch (IOException e2) { 320 break; 321 322 } 323 } // end catch of try { switch() ... } 324 } // end whlie loop 325 326 try { 327 in.close(); 328 out.close(); 329 } catch (IOException e) { 330 System.out.println(e.toString()); 331 } finally { 332 try { 333 socket.close(); 334 } catch (IOException e) { 335 System.out.println(e.toString()); 336 } 337 } 338 } // end run method 339 } //end class ConClient 340 341 /*** 342 * only for test 343 */ 344 public static void main(String[] args) { 345 FileOutputStream fos = null; 346 Properties prop = new Properties(); 347 try { 348 fos = new FileOutputStream(new File("jfoxmq.pro")); 349 prop.setProperty("serverAddr", "127,0,0,1"); 350 prop.setProperty("serverPort", "1000"); 351 prop.store(fos, null); 352 } catch (Exception e) { 353 System.out.println(e.toString()); 354 System.out.println("can not set properties,exit"); 355 System.exit(1); 356 } 357 try { 358 fos.close(); 359 } catch (Exception e) { 360 System.out.println(e.toString()); 361 } 362 363 SocketLinkServerService server = new SocketLinkServerService(); 364 server.setConnectionFactoryJNDIRef("ConnectionFactory"); 365 TestInterceptor test = new TestInterceptor(); 366 server.firstInterceptor = test; 367 server.start(); 368 } 369 370 }

This page was automatically generated by Maven