incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmue...@apache.org
Subject [26/27] git commit: closes #25
Date Tue, 03 Jan 2012 18:39:32 GMT
closes #25


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/commit/a36a38da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/tree/a36a38da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/diff/a36a38da

Branch: refs/heads/master
Commit: a36a38da8f3ad1ae2693a611915d8e25f8e2f571
Parents: 4f268d2
Author: Patrick Mueller <pmuellr@yahoo.com>
Authored: Sat Aug 13 18:41:48 2011 -0400
Committer: Patrick Mueller <pmuellr@yahoo.com>
Committed: Sat Aug 13 18:41:48 2011 -0400

----------------------------------------------------------------------
 weinre.server/src/weinre/server/Channel.java       |   22 ++++++++
 weinre.server/src/weinre/server/Main.java          |   20 ++++++++
 .../src/weinre/server/MessageHandler.java          |   10 ++++
 .../src/weinre/server/ServerSettings.java          |   38 ++++++++++++++-
 4 files changed, 88 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/blob/a36a38da/weinre.server/src/weinre/server/Channel.java
----------------------------------------------------------------------
diff --git a/weinre.server/src/weinre/server/Channel.java b/weinre.server/src/weinre/server/Channel.java
index 71294d2..11889b2 100644
--- a/weinre.server/src/weinre/server/Channel.java
+++ b/weinre.server/src/weinre/server/Channel.java
@@ -8,6 +8,7 @@
 package weinre.server;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -35,6 +36,7 @@ public class Channel {
     private Map<String,Object>    serviceMap;
     private String                remoteHost;
     private String                remoteAddress;
+    private PrintWriter           messageLog;
     
     //---------------------------------------------------------------
     public Channel(String pathPrefix, String name, String id, String remoteHost, String remoteAddress)
{
@@ -49,6 +51,7 @@ public class Channel {
         this.connector          = null;
         this.serviceMap         = new HashMap<String,Object>();
         this.lastRead           = System.currentTimeMillis();
+        this.messageLog         = Main.getSettings().getMessageLog();
     }
 
     //---------------------------------------------------------------
@@ -186,6 +189,7 @@ public class Channel {
         if (isClosed()) return;
         
         requestQueue.add(json);
+        log(json);
     }
 
     //---------------------------------------------------------------
@@ -193,6 +197,24 @@ public class Channel {
         if (isClosed()) return;
         
         responseQueue.add(json);
+        log(json);
+    }
+
+    //---------------------------------------------------------------
+    private void log(String json) {
+        if (null == messageLog) return;
+        
+        JSONObject jObject;
+        try {
+            jObject = new JSONObject(json);
+            jObject.put("_to", getName() + "#" + getId());
+        }
+        catch (JSONException e) {
+            return;
+        }
+        
+        messageLog.print(jObject.toString(true));
+        messageLog.println(",");
     }
 
     //---------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/blob/a36a38da/weinre.server/src/weinre/server/Main.java
----------------------------------------------------------------------
diff --git a/weinre.server/src/weinre/server/Main.java b/weinre.server/src/weinre/server/Main.java
index 1d8ae96..4968fee 100644
--- a/weinre.server/src/weinre/server/Main.java
+++ b/weinre.server/src/weinre/server/Main.java
@@ -8,6 +8,7 @@
 package weinre.server;
 
 import java.io.PrintStream;
+import java.io.PrintWriter;
 
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.util.log.Log;
@@ -56,6 +57,12 @@ public class Main {
         Settings = ServerSettings.getOptions(args);
         if (null == Settings) System.exit(0);
         
+        if (null != Settings.getMessageLog()) {
+            Settings.getMessageLog().println("[");
+        }
+        
+        Runtime.getRuntime().addShutdownHook(onShutdownThread());
+        
         consoleStdout = ConsoleOutputStream.newPrintStream(this, System.out, true);
         consoleStderr = ConsoleOutputStream.newPrintStream(this, System.err, false);
 
@@ -71,6 +78,19 @@ public class Main {
     }
     
     //---------------------------------------------------------------
+    private Thread onShutdownThread() {
+        return new Thread(new Runnable() {
+            public void run() {
+                PrintWriter messageLog = Settings.getMessageLog();
+                if (null == messageLog) return;
+                
+                messageLog.println("null ]");
+                messageLog.close();
+            }
+        });
+    }
+    
+    //---------------------------------------------------------------
     public int exit() {
         if (null != server) {
             try {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/blob/a36a38da/weinre.server/src/weinre/server/MessageHandler.java
----------------------------------------------------------------------
diff --git a/weinre.server/src/weinre/server/MessageHandler.java b/weinre.server/src/weinre/server/MessageHandler.java
index 29942b2..bbddc9f 100644
--- a/weinre.server/src/weinre/server/MessageHandler.java
+++ b/weinre.server/src/weinre/server/MessageHandler.java
@@ -7,6 +7,7 @@
 
 package weinre.server;
 
+import java.io.PrintWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -18,6 +19,7 @@ import org.apache.wink.json4j.JSONObject;
 
 //-------------------------------------------------------------------
 public class MessageHandler {
+    private PrintWriter messageLog;
 
     //---------------------------------------------------------------
     static public void start() {
@@ -43,6 +45,7 @@ public class MessageHandler {
     
     //---------------------------------------------------------------
     private MessageHandler() {
+        messageLog = Main.getSettings().getMessageLog();
     }
     
     //---------------------------------------------------------------
@@ -70,6 +73,13 @@ public class MessageHandler {
                     try {
                         request = acc.getString(i);
                         accRequest = new JSONObject(request);
+                        accRequest.put("_from", channel.getName() + "#" + channel.getId());
+                        
+                        if (null != messageLog) {
+                            messageLog.print(accRequest.toString(true));
+                            messageLog.println(",");
+                        }
+                        
                     }
                     catch (JSONException e) {
                         Main.warn("error parsing request: " + e + ": '" + request + "'");

http://git-wip-us.apache.org/repos/asf/incubator-cordova-weinre/blob/a36a38da/weinre.server/src/weinre/server/ServerSettings.java
----------------------------------------------------------------------
diff --git a/weinre.server/src/weinre/server/ServerSettings.java b/weinre.server/src/weinre/server/ServerSettings.java
index b7a63b5..a2c73c7 100644
--- a/weinre.server/src/weinre/server/ServerSettings.java
+++ b/weinre.server/src/weinre/server/ServerSettings.java
@@ -7,6 +7,10 @@
 
 package weinre.server;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
@@ -38,6 +42,7 @@ public class ServerSettings {
     private int         readTimeoutSeconds = 5;
     private int         deathTimeoutSeconds;
     private Properties  fileProperties;
+    private PrintWriter messageLog;
     
     //---------------------------------------------------------------
     static public ServerSettings getOptions(String[] commandLine) {
@@ -65,6 +70,7 @@ public class ServerSettings {
         options.addOption("reuseAddr",    true,  "force bind the port if already bound [true|false]");
         options.addOption("readTimeout",  true,  "seconds before timing out HTTP GETs");
         options.addOption("deathTimeout", true,  "seconds before considering connector dead");
+        options.addOption("messageLog",   true,  "file to log messages to");
        
         return options;
     }
@@ -126,6 +132,7 @@ public class ServerSettings {
         reuseAddr           = getBooleanFromOption(commandLine, "reuseAddr",    reuseAddr);
         readTimeoutSeconds  = getIntFromOption(commandLine,     "readTimeout",  readTimeoutSeconds,
  0, 0x00FFFFFF);
         deathTimeoutSeconds = getIntFromOption(commandLine,     "deathTimeout", readTimeoutSeconds*3,
0, 0x00FFFF);
+        messageLog          = getPrintWriterFromOption(commandLine, "messageLog");
 
         // handle verbose logging
         if (commandLine.hasOption("verbose")) {
@@ -188,7 +195,29 @@ public class ServerSettings {
 
         return result;
     }
-    
+
+    //---------------------------------------------------------------
+    private PrintWriter getPrintWriterFromOption(CommandLine commandLine, String name) {
+        
+        String fileName = commandLine.getOptionValue(name);
+        if (null == fileName) {
+            fileName = fileProperties.getProperty(name);
+        }
+
+        if (null == fileName) return null;
+        
+        File file = new File(fileName);
+        
+        try {
+            FileWriter fileWriter = new FileWriter(file);
+            return new PrintWriter(fileWriter);
+        } 
+        catch (IOException e) {
+            error(name + " parameter file name '" + fileName + "' cannot be opened for writing.");
+            return null;
+        }
+    }
+
     //---------------------------------------------------------------
     private void error(String message) {
         System.out.println("error with command-line option: " + message);
@@ -266,7 +295,12 @@ public class ServerSettings {
     public boolean reuseAddr() {
         return reuseAddr;
     }
-    
+
+    //---------------------------------------------------------------
+    public PrintWriter getMessageLog() {
+        return messageLog;
+    }
+
     //---------------------------------------------------------------
     public String getNiceHostName() {
         String hostName = getBoundHostValue();


Mime
View raw message