litesoft
@ 821
litesoft / trunk / Java / core / Server / src / org / litesoft / peertopeer / nonpublic / broadcastdiscovery / BroadcastMessageManager.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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.peertopeer.nonpublic.broadcastdiscovery; import java.io.*; import java.net.*; import org.litesoft.core.typeutils.*; import org.litesoft.logger.*; import org.litesoft.peertopeer.*; import org.litesoft.peertopeer.nonpublic.peermanagement.*; import org.litesoft.util.*; public class BroadcastMessageManager implements P2PConstants { public static final Logger LOGGER = LoggerFactory.getLogger( BroadcastMessageManager.class ); private static final byte BCA_BYTE = (byte) 255; private static final byte[] BROADCAST_ADDRESS = new byte[]{BCA_BYTE, BCA_BYTE, BCA_BYTE, BCA_BYTE}; private String mOurName, mAppGroupID, mAppGroupVersion, mMessagePrefixFilter; private AppGroupVersionCompatibilityChecker mCompatibilityChecker; public BroadcastMessageManager( String pOurName, String pAppGroupID, String pAppGroupVersion, AppGroupVersionCompatibilityChecker pCompatibilityChecker ) { mOurName = assertNotEmptyAndNoCommas( "OurName", pOurName ); mAppGroupID = assertNotEmptyAndNoCommas( "AppGroupID", pAppGroupID ); mAppGroupVersion = assertNotEmptyAndNoCommas( "AppGroupVersion", pAppGroupVersion ); mCompatibilityChecker = pCompatibilityChecker; mMessagePrefixFilter = mAppGroupID + ","; } public String getOurName() { return mOurName; } public synchronized void assert_joinGroup_notCalled() { if ( mOurServer != null ) { throw new IllegalStateException( "BroadcastMessageManager has already joined (or attempted to join) Group: " + mAppGroupID ); } } public synchronized void joinGroup( int pServerPort, PeerConnectionHandlerFactory pConnectionHandlerFactory ) throws IOException { P2Putils.validatePort( pServerPort ); Objects.assertNotNull( "PeerConnectionHandlerFactory", pConnectionHandlerFactory ); assert_joinGroup_notCalled(); mOurServer = new OurServer( getClass().getName(), pConnectionHandlerFactory ); // get a datagram socket DatagramSocket socket = new DatagramSocket(); try { socket.setBroadcast( true ); mOurServer.startListening(); byte[] buf = createAnnouceMessage( pServerPort ); InetAddress address = InetAddress.getByAddress( BROADCAST_ADDRESS ); DatagramPacket packet = new DatagramPacket( buf, buf.length, address, DISCOVERY_PORT ); socket.send( packet ); } catch ( NoRouteToHostException e ) { System.err.println( "*** Unable to Broadcast Our Existence - Proceeding Stand Alone ***" ); mOurServer.stopListening(); } catch ( IOException e ) { mOurServer.stopListening(); throw e; } finally { socket.close(); } } private void processAnnounceMessage( String pMessage, InetAddress pInetAddress, PeerConnectionHandlerFactory pConnectionHandlerFactory ) { if ( !pMessage.startsWith( mMessagePrefixFilter ) ) { return; } int cAt1 = pMessage.indexOf( ',' ); int cAt2 = pMessage.indexOf( ',', cAt1 + 1 ); int cAt3 = pMessage.indexOf( ',', cAt2 + 1 ); if ( (cAt2 != -1) && (cAt3 != -1) ) { String thierAppGroupVersion = pMessage.substring( cAt1 + 1, cAt2 ).trim(); String thierName = pMessage.substring( cAt2 + 1, cAt3 ).trim(); String thierServerPort = pMessage.substring( cAt3 + 1 ).trim(); if ( (thierAppGroupVersion.length() != 0) && (thierName.length() != 0) && (thierServerPort.length() != 0) ) { int zPort = parseServerPort( thierServerPort ); if ( zPort != 0 ) { PeerDefinition zNewPeerDef = new PeerDefinition( thierName, pInetAddress, zPort ); if ( !mAppGroupVersion.equals( thierAppGroupVersion ) && ((mCompatibilityChecker == null) || !mCompatibilityChecker.areCompatibile( mAppGroupVersion, thierAppGroupVersion )) ) { pConnectionHandlerFactory.handleAnnouncedPeerWithIncompatibleAppGroupVersions( zNewPeerDef, mOurName, mAppGroupVersion, thierAppGroupVersion ); } else { pConnectionHandlerFactory.handleAnnouncedPeer( zNewPeerDef ); } return; } } } LOGGER.error.log( "MalformedBroadcastAnnounceMessage from ", pInetAddress, ":", pMessage ); } private byte[] createAnnouceMessage( int pServerPort ) throws UnsupportedEncodingException { return createAnnouceMessage( mAppGroupID + "," + mAppGroupVersion + "," + mOurName + "," + pServerPort ); } private static byte[] createAnnouceMessage( String pMessage ) { byte[] zSBytes; try { zSBytes = pMessage.getBytes( FileUtils.UTF_8 ); } catch ( UnsupportedEncodingException e ) { LOGGER.error.log( e ); // shouldn't happen zSBytes = new byte[0]; } int rvLength = zSBytes.length + BROADCAST_PREFIX.length; if ( rvLength > 255 ) { throw new IllegalArgumentException( "Announce Message too long: " + pMessage ); } byte[] rv = new byte[rvLength]; int to = 0; for ( byte b : BROADCAST_PREFIX ) { rv[to++] = b; } for ( byte b : zSBytes ) { rv[to++] = b; } return rv; } private static String parseAnnouceMessage( DatagramPacket packet ) { int zDataLength = packet.getLength(); if ( zDataLength < MINIMUM_BROADCAST_MESSAGE_LENGTH ) { return null; } byte[] zData = packet.getData(); int zEncodedStringStartsAt = BROADCAST_PREFIX.length; for ( int i = 0; i < zEncodedStringStartsAt; i++ ) { if ( zData[i] != BROADCAST_PREFIX[i] ) { return null; } } String zMessage = null; try { zMessage = new String( zData, zEncodedStringStartsAt, zDataLength - zEncodedStringStartsAt, FileUtils.UTF_8 ); } catch ( UnsupportedEncodingException e ) { LOGGER.error.log( e ); // shouldn't happen } catch ( RuntimeException e ) { // Not Properly Encoded? LOGGER.error.log( e ); } return zMessage; } private OurServer mOurServer = null; private class OurServer extends Thread { private PeerConnectionHandlerFactory mConnectionHandlerFactory; private DatagramSocket mSocket = null; public OurServer( String pName, PeerConnectionHandlerFactory pConnectionHandlerFactory ) { super( pName ); mConnectionHandlerFactory = pConnectionHandlerFactory; setDaemon( true ); } public synchronized void startListening() throws SocketException { mSocket = new DatagramSocket( null ); mSocket.setReuseAddress( true ); mSocket.bind( new InetSocketAddress( (InetAddress) null, DISCOVERY_PORT ) ); start(); } public synchronized boolean stopListening() { DatagramSocket zSocket = mSocket; mSocket = null; if ( zSocket == null ) { return false; } zSocket.close(); return true; } private synchronized DatagramSocket stillListening() { return mSocket; } @Override public void run() { for ( DatagramSocket zSocket; null != (zSocket = stillListening()); ) { DatagramPacket packet = new DatagramPacket( new byte[256], 256 ); try { // receive request zSocket.receive( packet ); } catch ( IOException e ) { if ( stopListening() ) { LOGGER.error.log( e ); } return; } try { String zMessage = parseAnnouceMessage( packet ); if ( zMessage != null ) { processAnnounceMessage( zMessage, packet.getAddress(), mConnectionHandlerFactory ); } } catch ( RuntimeException e ) { LOGGER.error.log( e ); } } } } private static String assertNotEmptyAndNoCommas( String pErrorMessage, String pStringToAssert ) { pStringToAssert = Strings.assertNotNullNotEmpty( pErrorMessage, pStringToAssert ); if ( pStringToAssert.contains( "," ) ) { Strings.error( "String", pErrorMessage, " is not allowed to contain any Commas (',')" ); } return pStringToAssert; } private static int parseServerPort( String pThierServerPort ) { try { return P2Putils.validatePort( Integer.parseInt( pThierServerPort ) ); } catch ( RuntimeException e ) { return 0; } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/peertopeer/nonpublic/broadcastdiscovery/BroadcastMessageManager.java
Revision | Author | Commited | Message |
---|---|---|---|
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
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 |