Jelajahi Sumber

Added a bunch of lower level logging that can be
used to either watch traffic or debug serialization
issues, etc..
Went ahead and instrumented the service manager while
I was at it... and fixed a potential NPE in the AbstractService's
toString() method.
Fixed a bug in the DefaultClient where it couldn't be shutdown
if attempted before the services had been started.

Paul Speed 9 tahun lalu
induk
melakukan
388a8a8bd7

+ 12 - 3
jme3-networking/src/main/java/com/jme3/network/base/DefaultClient.java

@@ -100,6 +100,7 @@ public class DefaultClient implements Client
     }
 
     protected void addStandardServices() {
+        log.fine("Adding standard services...");
         services.addService(new ClientSerializerRegistrationsService());
     }
 
@@ -222,6 +223,9 @@ public class DefaultClient implements Client
    
     public void send( Message message )
     {
+        if( log.isLoggable(Level.FINER) ) {
+            log.log(Level.FINER, "send({0})", message);
+        }
         if( message.isReliable() || channels.get(CH_UNRELIABLE) == null ) {
             send(CH_RELIABLE, message, true);
         } else {
@@ -231,6 +235,9 @@ public class DefaultClient implements Client
  
     public void send( int channel, Message message )
     {
+        if( log.isLoggable(Level.FINER) ) {
+            log.log(Level.FINER, "send({0}, {1})", new Object[]{channel, message});
+        }
         if( channel >= 0 ) {
             // Make sure we aren't still connecting.  Channels
             // won't be valid until we are fully connected since
@@ -287,9 +294,11 @@ public class DefaultClient implements Client
         if( !isRunning )
             return;
 
-        // Let the services get a chance to stop before we
-        // kill the connection.
-        services.stop();
+        if( services.isStarted() ) {
+            // Let the services get a chance to stop before we
+            // kill the connection.
+            services.stop();
+        }
         
         // Send a close message
     

+ 15 - 0
jme3-networking/src/main/java/com/jme3/network/base/DefaultServer.java

@@ -108,6 +108,7 @@ public class DefaultServer implements Server
     }   
 
     protected void addStandardServices() {
+        log.fine("Adding standard services...");
         services.addService(new ServerSerializerRegistrationsService());
     }
 
@@ -221,6 +222,10 @@ public class DefaultServer implements Server
 
     public void broadcast( Filter<? super HostedConnection> filter, Message message )
     {
+        if( log.isLoggable(Level.FINER) ) {
+            log.log(Level.FINER, "broadcast({0}, {1})", new Object[]{filter, message});
+        }
+        
         if( connections.isEmpty() )
             return;
  
@@ -239,6 +244,10 @@ public class DefaultServer implements Server
 
     public void broadcast( int channel, Filter<? super HostedConnection> filter, Message message )
     {
+        if( log.isLoggable(Level.FINER) ) {
+            log.log(Level.FINER, "broadcast({0}, {1}. {2})", new Object[]{channel, filter, message});
+        }
+        
         if( connections.isEmpty() )
             return;
 
@@ -540,6 +549,9 @@ public class DefaultServer implements Server
        
         public void send( Message message )
         {
+            if( log.isLoggable(Level.FINER) ) {
+                log.log(Level.FINER, "send({0})", message);
+            }
             ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null);
             if( message.isReliable() || channels[CH_UNRELIABLE] == null ) {
                 channels[CH_RELIABLE].send( buffer );
@@ -550,6 +562,9 @@ public class DefaultServer implements Server
 
         public void send( int channel, Message message )
         {
+            if( log.isLoggable(Level.FINER) ) {
+                log.log(Level.FINER, "send({0}, {1})", new Object[]{channel, message});
+            }
             checkChannel(channel);
             ByteBuffer buffer = MessageProtocol.messageToBuffer(message, null);
             channels[channel+CH_FIRST].send(buffer);

+ 5 - 0
jme3-networking/src/main/java/com/jme3/network/serializing/Serializer.java

@@ -45,6 +45,7 @@ import java.nio.ByteBuffer;
 import java.util.*;
 import java.util.jar.Attributes;
 import java.util.logging.Level;
+import java.util.logging.LogManager;
 import java.util.logging.Logger;
 
 /**
@@ -403,6 +404,10 @@ public abstract class Serializer {
         if (reg == null) {
             throw new SerializerException( "Class not registered:" + type );
         }
+        
+        if( log.isLoggable(Level.FINER) ) {
+            log.log(Level.FINER, "writing class:{0} with ID:{1}", new Object[]{type, reg.getId()});
+        }
         buffer.putShort(reg.getId());
         return reg;
     }

+ 1 - 1
jme3-networking/src/main/java/com/jme3/network/service/AbstractService.java

@@ -106,6 +106,6 @@ public abstract class AbstractService<S extends ServiceManager> implements Servi
     
     @Override
     public String toString() {
-        return getClass().getName() + "[serviceManager.class=" + serviceManager.getClass() + "]";
+        return getClass().getName() + "[serviceManager.class=" + (serviceManager != null ? serviceManager.getClass() : "") + "]";
     }
 }

+ 28 - 0
jme3-networking/src/main/java/com/jme3/network/service/ServiceManager.java

@@ -34,6 +34,8 @@ package com.jme3.network.service;
 
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  *  The base service manager class from which the HostedServiceManager
@@ -44,6 +46,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
  */
 public abstract class ServiceManager<T> {
 
+    static final Logger log = Logger.getLogger(ServiceManager.class.getName());
+    
     private List<Service<T>> services = new CopyOnWriteArrayList<Service<T>>();
     private volatile boolean started = false;
     
@@ -76,6 +80,9 @@ public abstract class ServiceManager<T> {
             return;
         }
         for( Service<T> s : services ) {
+            if( log.isLoggable(Level.FINE) ) {
+                log.log(Level.FINE, "Starting service:{0}", s);
+            }
             s.start();
         }
         started = true;
@@ -96,6 +103,9 @@ public abstract class ServiceManager<T> {
             throw new IllegalStateException(getClass().getSimpleName() + " not started.");
         }
         for( Service<T> s : services ) {
+            if( log.isLoggable(Level.FINE) ) {
+                log.log(Level.FINE, "Stopping service:{0}", s);
+            }
             s.stop();
         }
         started = false;
@@ -106,9 +116,18 @@ public abstract class ServiceManager<T> {
      *  has already been started then the service will also be started.
      */   
     public <S extends Service<T>> void addService( S s ) {
+        if( log.isLoggable(Level.FINE) ) {
+            log.log(Level.FINE, "addService({0})", s);
+        }
         services.add(s);
+        if( log.isLoggable(Level.FINE) ) {
+            log.log(Level.FINE, "Initializing service:{0}", s);
+        }
         s.initialize(getParent());
         if( started ) {
+            if( log.isLoggable(Level.FINE) ) {
+                log.log(Level.FINE, "Starting service:{0}", s);
+            }
             s.start();
         }
     }
@@ -120,10 +139,19 @@ public abstract class ServiceManager<T> {
      *  the service will be terminated.
      */
     public <S extends Service<T>> void removeService( S s ) {
+        if( log.isLoggable(Level.FINE) ) {
+            log.log(Level.FINE, "removeService({0})", s);
+        }
         if( started ) {
+            if( log.isLoggable(Level.FINE) ) {
+                log.log(Level.FINE, "Stopping service:{0}", s);
+            }
             s.stop();
         }
         services.remove(s);
+        if( log.isLoggable(Level.FINE) ) {
+            log.log(Level.FINE, "Terminating service:{0}", s);
+        }
         s.terminate(getParent());        
     }