litesoft
@ 947
litesoft / trunk / Java / core / Server / src / org / litesoft / peertopeer / nonpublic / peermanagement / PeerConnectionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.peertopeer.nonpublic.peermanagement; import java.io.*; import java.net.*; import java.util.concurrent.*; import org.litesoft.logger.*; public abstract class PeerConnectionHandler extends Thread { public static final int MAX_SERIALIZED_OBJECT_SIZE = 63 * 1024; public static final int NEW_OBJECT_COMING = 22; // ASCII_SYN public static final Logger LOGGER = LoggerFactory.getLogger( PeerConnectionHandler.class ); private PeerConnectionManager mConnectionManager; private PeerDefinition mPeerDef; private Socket mSocket; private OutputStream mOutputStream = null; private InputStream mInputStream = null; private BlockingQueue<Serializable> mSenderQueue = new LinkedBlockingQueue<Serializable>(); private volatile boolean mClosing = false; protected PeerConnectionHandler( PeerConnectionManager pConnectionManager, PeerDefinition pPeerDef, Socket pSocket ) { setName( getClass().getName() ); mConnectionManager = pConnectionManager; mPeerDef = pPeerDef; mSocket = pSocket; } public PeerDefinition getPeerDef() { return mPeerDef; } protected void setPeerDef( PeerList pPeerList ) { mPeerDef = pPeerList.getFrom(); } public void send( Serializable pToSend ) { if ( pToSend != null ) { mSenderQueue.add( pToSend ); } } public void close() { mClosing = true; interrupt(); } protected void LLsend( Serializable pToSend ) throws IOException { ByteOutputStream zByteStream = new ByteOutputStream(); ObjectOutputStream zObjectStream = new ObjectOutputStream( zByteStream ); try { zObjectStream.writeObject( pToSend ); zObjectStream.flush(); } finally { zObjectStream.close(); } int count = zByteStream.getCount(); if ( count > MAX_SERIALIZED_OBJECT_SIZE ) { throw new IOException( "Serialized Object too large (" + count + " > " + MAX_SERIALIZED_OBJECT_SIZE + "), Object: " + pToSend ); } try { OutputStream zPeerStream = getOutputStream(); zPeerStream.write( NEW_OBJECT_COMING ); zPeerStream.write( (count >> 8) & 0xFF ); // Hi Order Byte zPeerStream.write( count & 0xFF ); // Low Order Byte for ( int i = 0; i < count; i++ ) { zPeerStream.write( zByteStream.readByteAt( i ) ); } zPeerStream.flush(); } catch ( SocketException e ) { if ( isConnectionReset( e ) ) { close(); return; } throw e; } } protected Serializable LLread() throws IOException, ClassNotFoundException { try { InputStream zPeerStream = getInputStream(); for ( int i; NEW_OBJECT_COMING != (i = zPeerStream.read()); )// Skip anything else as noise!!! { if ( i == -1 ) { return null; // EOF } } int zHiOrderByte = zPeerStream.read(); int zLowOrderByte = zPeerStream.read(); if ( (zHiOrderByte == -1) || (zLowOrderByte == -1) ) { problem( new IllegalStateException( "EOF in block size" ) ); return null; // EOF } int count = ((zHiOrderByte & 0xFF) << 8) + (zLowOrderByte & 0xFF); if ( count > MAX_SERIALIZED_OBJECT_SIZE ) { throw new IOException( "De-Serialized Object too large (" + count + " > " + MAX_SERIALIZED_OBJECT_SIZE + ")" ); } byte[] buf = new byte[count]; for ( int i = 0; i < count; i++ ) { int zByte = zPeerStream.read(); if ( zByte == -1 ) { problem( new IllegalStateException( "EOF in block" ) ); return null; // EOF } buf[i] = (byte) zByte; } ObjectInputStream zObjectStream = new ObjectInputStream( new ByteArrayInputStream( buf ) ); Object o; try { o = zObjectStream.readObject(); } finally { zObjectStream.close(); } return (Serializable) o; } catch ( SocketException e ) { if ( isConnectionReset( e ) ) { return null; } throw e; } } protected void runAsSenderThread() { try { while ( !mClosing ) { try { LLsend( mSenderQueue.take() ); } catch ( InterruptedException e ) { if ( !mClosing ) { problem( e ); } } } } catch ( IOException e ) { problem( e ); } catch ( RuntimeException e ) { problem( e ); } } protected boolean startPeering() throws IOException { BlockingQueue<Serializable> zQueue = mConnectionManager.startPeeringWith( this ); if ( zQueue != null ) { new ReceiverThread( zQueue ).start(); return true; } return false; } protected void notifyConnectionManagerOf( PeerDefinition[] pPeers ) { mConnectionManager.addFromPeer( pPeers ); } protected boolean isOKtoRunIfIncompatibleVersionAlreadyRunning() { return mConnectionManager.isOKtoRunIfIncompatibleVersionAlreadyRunning(); } protected void addFromPeer( PeerDefinition[] pPeerDefinitions ) { mConnectionManager.addFromPeer( pPeerDefinitions ); } private void problem( Exception e ) { InetAddress zInetAddress = mSocket != null ? mSocket.getInetAddress() : null; LOGGER.error.log( e, zInetAddress, " from ", mPeerDef ); } @Override protected void finalize() throws Throwable { releaseResources(); super.finalize(); } private void releaseResources() { mClosing = true; if ( mInputStream != null ) { try { mInputStream.close(); } catch ( IOException e ) { // Whatever... } finally { mInputStream = null; } } if ( mOutputStream != null ) { try { mOutputStream.close(); } catch ( IOException e ) { // Whatever... } finally { mOutputStream = null; } } if ( mSocket != null ) { try { mSocket.close(); } catch ( IOException e ) { // Whatever... } finally { mSocket = null; } } if ( mConnectionManager != null ) { try { mConnectionManager.noLongerPeeringWith( mPeerDef ); } catch ( Exception e ) { // Whatever... } mConnectionManager = null; } } protected Socket getSocket() throws IOException { if ( mSocket == null ) { mSocket = new Socket( mPeerDef.getInetAddress(), mPeerDef.getPort() ); } return mSocket; } protected OutputStream getOutputStream() throws IOException { if ( mOutputStream == null ) { mOutputStream = new BufferedOutputStream( getSocket().getOutputStream() ); } return mOutputStream; } protected InputStream getInputStream() throws IOException { if ( mInputStream == null ) { mInputStream = new BufferedInputStream( getSocket().getInputStream() ); } return mInputStream; } @Override public void run() { try { LLrun(); } catch ( IOException e ) { problem( e ); } catch ( ClassNotFoundException e ) { problem( e ); } catch ( RuntimeException e ) { problem( e ); } finally { releaseResources(); } } abstract protected void LLrun() throws IOException, ClassNotFoundException; private class ReceiverThread extends Thread { private BlockingQueue<Serializable> mReceiverQueue; public ReceiverThread( BlockingQueue<Serializable> pReceiverQueue ) { mReceiverQueue = pReceiverQueue; setDaemon( true ); } @Override public void run() { try { for ( Serializable zSerializable; null != (zSerializable = LLread()); ) { mReceiverQueue.add( zSerializable ); } } catch ( IOException e ) { problem( e ); } catch ( ClassNotFoundException e ) { problem( e ); } catch ( RuntimeException e ) { problem( e ); } mReceiverQueue = null; } } private static class ByteOutputStream extends ByteArrayOutputStream { public ByteOutputStream() { super( 4096 ); } public int getCount() { return count; } public byte readByteAt( int pOffset ) { return buf[pOffset]; } } private boolean isConnectionReset( SocketException e ) { String zMessage = e.getMessage().toUpperCase(); return zMessage.contains( "CONNECTION RESET" ); } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/peertopeer/nonpublic/peermanagement/PeerConnectionHandler.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |