litesoft
@ 947
litesoft / trunk / Java / core / Server / src / org / litesoft / peertopeer / nonpublic / peermanagement / PeerConnectionHandlerTheyContactedUs.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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.peertopeer.nonpublic.peermanagement; import java.io.*; import java.net.*; /** * Handle They Contacted Us * <p/> * wait for 1st message, if approriate, handle, and start Peering...: */ public class PeerConnectionHandlerTheyContactedUs extends PeerConnectionHandler { public PeerConnectionHandlerTheyContactedUs( PeerConnectionManager pConnectionManager, Socket pSocket ) { super( pConnectionManager, null, pSocket ); } /** * Handle They Contacted Us * <p/> * Open both streams, * wait for 1st message, either: * <p/> * CantPeerDifferentGroupVersion w/ * Our Name/ID & Supported Group Protocol Version * or * PeerList (possibly empty) of all 'other' known Group Peers. * <p/> * If was CantPeerDifferentGroupVersion, then we: * <p/> * Close the connection, * and either: * <p/> * Proceeds as if we were never contacted. * or * Shuts down as mixed versions are unacceptable! * <p/> * If was PeerList, then we: * <p/> */ @Override protected void LLrun() throws IOException, ClassNotFoundException { Serializable zSerializable = LLread(); if ( zSerializable instanceof PeerList ) { handle( (PeerList) zSerializable ); } else if ( zSerializable instanceof CantPeerDifferentGroupVersion ) { handle( (CantPeerDifferentGroupVersion) zSerializable ); } else { LOGGER.error.log( "Unexpected 1st message: " + zSerializable ); } // otherwise simply return, which will shut down the connection... } /** * Close the connection, * and either: * <p/> * Proceeds as if we were never contacted. * or * Shuts down as mixed versions are unacceptable! */ private void handle( CantPeerDifferentGroupVersion pCantPeerDifferentGroupVersion ) throws IOException, ClassNotFoundException { if ( !isOKtoRunIfIncompatibleVersionAlreadyRunning() ) { Error zError = new Error( pCantPeerDifferentGroupVersion.toString() ); LOGGER.error.log( zError ); zError.printStackTrace(); System.exit( 1 ); } // otherwise return and close connection and continue running normally... } /** * Wire into ConnectionManager as Regular Peer * And for each Peer in the List just recieved that is currently unknown, * it is added to the Contact Pending List and treated as an Announced Peer. */ private void handle( PeerList pPeerList ) throws IOException, ClassNotFoundException { setPeerDef( pPeerList ); if ( startPeering() ) { notifyConnectionManagerOf( pPeerList.getPeers() ); runAsSenderThread(); } } } |