Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/core/Server/src/org/litesoft/peertopeer/nonpublic/broadcastdiscovery/BroadcastMessageManager.java

Diff revisions: vs.
  @@ -1,17 +1,16 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.peertopeer.nonpublic.broadcastdiscovery;
3 3
4 - import java.io.*;
5 - import java.net.*;
6 -
7 4 import org.litesoft.commonfoundation.typeutils.*;
8 5 import org.litesoft.logger.*;
9 6 import org.litesoft.peertopeer.*;
10 7 import org.litesoft.peertopeer.nonpublic.peermanagement.*;
11 8 import org.litesoft.util.*;
12 9
13 - public class BroadcastMessageManager implements P2PConstants
14 - {
10 + import java.io.*;
11 + import java.net.*;
12 +
13 + public class BroadcastMessageManager implements P2PConstants {
15 14 public static final Logger LOGGER = LoggerFactory.getLogger( BroadcastMessageManager.class );
16 15
17 16 private static final byte BCA_BYTE = (byte) 255;
  @@ -20,8 +19,7 @@
20 19 private String mOurName, mAppGroupID, mAppGroupVersion, mMessagePrefixFilter;
21 20 private AppGroupVersionCompatibilityChecker mCompatibilityChecker;
22 21
23 - public BroadcastMessageManager( String pOurName, String pAppGroupID, String pAppGroupVersion, AppGroupVersionCompatibilityChecker pCompatibilityChecker )
24 - {
22 + public BroadcastMessageManager( String pOurName, String pAppGroupID, String pAppGroupVersion, AppGroupVersionCompatibilityChecker pCompatibilityChecker ) {
25 23 mOurName = assertNotEmptyAndNoCommas( "OurName", pOurName );
26 24 mAppGroupID = assertNotEmptyAndNoCommas( "AppGroupID", pAppGroupID );
27 25 mAppGroupVersion = assertNotEmptyAndNoCommas( "AppGroupVersion", pAppGroupVersion );
  @@ -29,22 +27,18 @@
29 27 mMessagePrefixFilter = mAppGroupID + ",";
30 28 }
31 29
32 - public String getOurName()
33 - {
30 + public String getOurName() {
34 31 return mOurName;
35 32 }
36 33
37 - public synchronized void assert_joinGroup_notCalled()
38 - {
39 - if ( mOurServer != null )
40 - {
34 + public synchronized void assert_joinGroup_notCalled() {
35 + if ( mOurServer != null ) {
41 36 throw new IllegalStateException( "BroadcastMessageManager has already joined (or attempted to join) Group: " + mAppGroupID );
42 37 }
43 38 }
44 39
45 40 public synchronized void joinGroup( int pServerPort, PeerConnectionHandlerFactory pConnectionHandlerFactory )
46 - throws IOException
47 - {
41 + throws IOException {
48 42 P2Putils.validatePort( pServerPort );
49 43 Objects.assertNotNull( "PeerConnectionHandlerFactory", pConnectionHandlerFactory );
50 44 assert_joinGroup_notCalled();
  @@ -54,8 +48,7 @@
54 48 // get a datagram socket
55 49 DatagramSocket socket = new DatagramSocket();
56 50
57 - try
58 - {
51 + try {
59 52 socket.setBroadcast( true );
60 53
61 54 mOurServer.startListening();
  @@ -65,48 +58,39 @@
65 58 DatagramPacket packet = new DatagramPacket( buf, buf.length, address, DISCOVERY_PORT );
66 59 socket.send( packet );
67 60 }
68 - catch ( NoRouteToHostException e )
69 - {
61 + catch ( NoRouteToHostException e ) {
70 62 System.err.println( "*** Unable to Broadcast Our Existence - Proceeding Stand Alone ***" );
71 63 mOurServer.stopListening();
72 64 }
73 - catch ( IOException e )
74 - {
65 + catch ( IOException e ) {
75 66 mOurServer.stopListening();
76 67 throw e;
77 68 }
78 - finally
79 - {
69 + finally {
80 70 socket.close();
81 71 }
82 72 }
83 73
84 - private void processAnnounceMessage( String pMessage, InetAddress pInetAddress, PeerConnectionHandlerFactory pConnectionHandlerFactory )
85 - {
86 - if ( !pMessage.startsWith( mMessagePrefixFilter ) )
87 - {
74 + private void processAnnounceMessage( String pMessage, InetAddress pInetAddress, PeerConnectionHandlerFactory pConnectionHandlerFactory ) {
75 + if ( !pMessage.startsWith( mMessagePrefixFilter ) ) {
88 76 return;
89 77 }
90 78 int cAt1 = pMessage.indexOf( ',' );
91 79 int cAt2 = pMessage.indexOf( ',', cAt1 + 1 );
92 80 int cAt3 = pMessage.indexOf( ',', cAt2 + 1 );
93 - if ( (cAt2 != -1) && (cAt3 != -1) )
94 - {
81 + if ( (cAt2 != -1) && (cAt3 != -1) ) {
95 82 String thierAppGroupVersion = pMessage.substring( cAt1 + 1, cAt2 ).trim();
96 83 String thierName = pMessage.substring( cAt2 + 1, cAt3 ).trim();
97 84 String thierServerPort = pMessage.substring( cAt3 + 1 ).trim();
98 - if ( (thierAppGroupVersion.length() != 0) && (thierName.length() != 0) && (thierServerPort.length() != 0) )
99 - {
85 + if ( (thierAppGroupVersion.length() != 0) && (thierName.length() != 0) && (thierServerPort.length() != 0) ) {
100 86 int zPort = parseServerPort( thierServerPort );
101 - if ( zPort != 0 )
102 - {
87 + if ( zPort != 0 ) {
103 88 PeerDefinition zNewPeerDef = new PeerDefinition( thierName, pInetAddress, zPort );
104 - if ( !mAppGroupVersion.equals( thierAppGroupVersion ) && ((mCompatibilityChecker == null) || !mCompatibilityChecker.areCompatibile( mAppGroupVersion, thierAppGroupVersion )) )
105 - {
106 - pConnectionHandlerFactory.handleAnnouncedPeerWithIncompatibleAppGroupVersions( zNewPeerDef, mOurName, mAppGroupVersion, thierAppGroupVersion );
107 - }
108 - else
109 - {
89 + if ( !mAppGroupVersion.equals( thierAppGroupVersion ) &&
90 + ((mCompatibilityChecker == null) || !mCompatibilityChecker.areCompatibile( mAppGroupVersion, thierAppGroupVersion )) ) {
91 + pConnectionHandlerFactory
92 + .handleAnnouncedPeerWithIncompatibleAppGroupVersions( zNewPeerDef, mOurName, mAppGroupVersion, thierAppGroupVersion );
93 + } else {
110 94 pConnectionHandlerFactory.handleAnnouncedPeer( zNewPeerDef );
111 95 }
112 96 return;
  @@ -117,68 +101,54 @@
117 101 }
118 102
119 103 private byte[] createAnnouceMessage( int pServerPort )
120 - throws UnsupportedEncodingException
121 - {
104 + throws UnsupportedEncodingException {
122 105 return createAnnouceMessage( mAppGroupID + "," + mAppGroupVersion + "," + mOurName + "," + pServerPort );
123 106 }
124 107
125 - private static byte[] createAnnouceMessage( String pMessage )
126 - {
108 + private static byte[] createAnnouceMessage( String pMessage ) {
127 109 byte[] zSBytes;
128 - try
129 - {
110 + try {
130 111 zSBytes = pMessage.getBytes( FileUtils.UTF_8 );
131 112 }
132 - catch ( UnsupportedEncodingException e )
133 - {
113 + catch ( UnsupportedEncodingException e ) {
134 114 LOGGER.error.log( e ); // shouldn't happen
135 115 zSBytes = new byte[0];
136 116 }
137 117 int rvLength = zSBytes.length + BROADCAST_PREFIX.length;
138 - if ( rvLength > 255 )
139 - {
118 + if ( rvLength > 255 ) {
140 119 throw new IllegalArgumentException( "Announce Message too long: " + pMessage );
141 120 }
142 121 byte[] rv = new byte[rvLength];
143 122 int to = 0;
144 - for ( byte b : BROADCAST_PREFIX )
145 - {
123 + for ( byte b : BROADCAST_PREFIX ) {
146 124 rv[to++] = b;
147 125 }
148 - for ( byte b : zSBytes )
149 - {
126 + for ( byte b : zSBytes ) {
150 127 rv[to++] = b;
151 128 }
152 129 return rv;
153 130 }
154 131
155 - private static String parseAnnouceMessage( DatagramPacket packet )
156 - {
132 + private static String parseAnnouceMessage( DatagramPacket packet ) {
157 133 int zDataLength = packet.getLength();
158 - if ( zDataLength < MINIMUM_BROADCAST_MESSAGE_LENGTH )
159 - {
134 + if ( zDataLength < MINIMUM_BROADCAST_MESSAGE_LENGTH ) {
160 135 return null;
161 136 }
162 137 byte[] zData = packet.getData();
163 138 int zEncodedStringStartsAt = BROADCAST_PREFIX.length;
164 - for ( int i = 0; i < zEncodedStringStartsAt; i++ )
165 - {
166 - if ( zData[i] != BROADCAST_PREFIX[i] )
167 - {
139 + for ( int i = 0; i < zEncodedStringStartsAt; i++ ) {
140 + if ( zData[i] != BROADCAST_PREFIX[i] ) {
168 141 return null;
169 142 }
170 143 }
171 144 String zMessage = null;
172 - try
173 - {
145 + try {
174 146 zMessage = new String( zData, zEncodedStringStartsAt, zDataLength - zEncodedStringStartsAt, FileUtils.UTF_8 );
175 147 }
176 - catch ( UnsupportedEncodingException e )
177 - {
148 + catch ( UnsupportedEncodingException e ) {
178 149 LOGGER.error.log( e ); // shouldn't happen
179 150 }
180 - catch ( RuntimeException e )
181 - {
151 + catch ( RuntimeException e ) {
182 152 // Not Properly Encoded?
183 153 LOGGER.error.log( e );
184 154 }
  @@ -187,98 +157,79 @@
187 157
188 158 private OurServer mOurServer = null;
189 159
190 - private class OurServer extends Thread
191 - {
160 + private class OurServer extends Thread {
192 161 private PeerConnectionHandlerFactory mConnectionHandlerFactory;
193 162 private DatagramSocket mSocket = null;
194 163
195 - public OurServer( String pName, PeerConnectionHandlerFactory pConnectionHandlerFactory )
196 - {
164 + public OurServer( String pName, PeerConnectionHandlerFactory pConnectionHandlerFactory ) {
197 165 super( pName );
198 166 mConnectionHandlerFactory = pConnectionHandlerFactory;
199 167 setDaemon( true );
200 168 }
201 169
202 170 public synchronized void startListening()
203 - throws SocketException
204 - {
171 + throws SocketException {
205 172 mSocket = new DatagramSocket( null );
206 173 mSocket.setReuseAddress( true );
207 174 mSocket.bind( new InetSocketAddress( (InetAddress) null, DISCOVERY_PORT ) );
208 175 start();
209 176 }
210 177
211 - public synchronized boolean stopListening()
212 - {
178 + public synchronized boolean stopListening() {
213 179 DatagramSocket zSocket = mSocket;
214 180 mSocket = null;
215 - if ( zSocket == null )
216 - {
181 + if ( zSocket == null ) {
217 182 return false;
218 183 }
219 184 zSocket.close();
220 185 return true;
221 186 }
222 187
223 - private synchronized DatagramSocket stillListening()
224 - {
188 + private synchronized DatagramSocket stillListening() {
225 189 return mSocket;
226 190 }
227 191
228 192 @Override
229 - public void run()
230 - {
231 - for ( DatagramSocket zSocket; null != (zSocket = stillListening()); )
232 - {
193 + public void run() {
194 + for ( DatagramSocket zSocket; null != (zSocket = stillListening()); ) {
233 195 DatagramPacket packet = new DatagramPacket( new byte[256], 256 );
234 - try
235 - {
196 + try {
236 197 // receive request
237 198 zSocket.receive( packet );
238 199 }
239 - catch ( IOException e )
240 - {
241 - if ( stopListening() )
242 - {
200 + catch ( IOException e ) {
201 + if ( stopListening() ) {
243 202 LOGGER.error.log( e );
244 203 }
245 204 return;
246 205 }
247 - try
248 - {
206 + try {
249 207 String zMessage = parseAnnouceMessage( packet );
250 - if ( zMessage != null )
251 - {
208 + if ( zMessage != null ) {
252 209 processAnnounceMessage( zMessage, packet.getAddress(), mConnectionHandlerFactory );
253 210 }
254 211 }
255 - catch ( RuntimeException e )
256 - {
212 + catch ( RuntimeException e ) {
257 213 LOGGER.error.log( e );
258 214 }
259 215 }
260 216 }
261 217 }
262 218
263 - private static String assertNotEmptyAndNoCommas( String pErrorMessage, String pStringToAssert )
264 - {
219 + private static String assertNotEmptyAndNoCommas( String pErrorMessage, String pStringToAssert ) {
265 220 pStringToAssert = Strings.assertNotNullNotEmpty( pErrorMessage, pStringToAssert );
266 - if ( pStringToAssert.contains( "," ) )
267 - {
221 + if ( pStringToAssert.contains( "," ) ) {
268 222 Strings.error( "String", pErrorMessage, " is not allowed to contain any Commas (',')" );
269 223 }
270 224
271 225 return pStringToAssert;
272 226 }
273 227
274 - private static int parseServerPort( String pThierServerPort )
275 - {
276 - try
277 - {
228 + private static int parseServerPort( String pThierServerPort ) {
229 + try {
278 230 return P2Putils.validatePort( Integer.parseInt( pThierServerPort ) );
279 231 }
280 - catch ( RuntimeException e )
281 - {
232 + catch ( RuntimeException e ) {
282 233 return 0;
283 234 }
284 235 }