Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
closes #25
  • Loading branch information
pmuellr committed Aug 13, 2011
1 parent 4f268d2 commit a36a38da8f3ad1ae2693a611915d8e25f8e2f571
Showing 4 changed files with 88 additions and 2 deletions.
@@ -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 Channel(String pathPrefix, String name, String id, String remoteHost, Str
this.connector = null;
this.serviceMap = new HashMap<String,Object>();
this.lastRead = System.currentTimeMillis();
this.messageLog = Main.getSettings().getMessageLog();
}

//---------------------------------------------------------------
@@ -186,13 +189,32 @@ public void postRequest(String json) {
if (isClosed()) return;

requestQueue.add(json);
log(json);
}

//---------------------------------------------------------------
public void postResponse(String json) {
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(",");
}

//---------------------------------------------------------------
@@ -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 Main(String[] args) {
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);

@@ -70,6 +77,19 @@ public void run() throws Throwable, Exception {
exit();
}

//---------------------------------------------------------------
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) {
@@ -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 @@

//-------------------------------------------------------------------
public class MessageHandler {
private PrintWriter messageLog;

//---------------------------------------------------------------
static public void start() {
@@ -43,6 +45,7 @@ public void run() {

//---------------------------------------------------------------
private MessageHandler() {
messageLog = Main.getSettings().getMessageLog();
}

//---------------------------------------------------------------
@@ -70,6 +73,13 @@ private void handleMessages() throws InterruptedException {
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 + "'");
@@ -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 @@ private Options getOptions() {
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 @@ private ServerSettings parse(String[] commandLineArgs) {
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 @@ private boolean getBooleanFromOption(CommandLine commandLine, String name, boole

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 boolean useProxy() {
public boolean reuseAddr() {
return reuseAddr;
}


//---------------------------------------------------------------
public PrintWriter getMessageLog() {
return messageLog;
}

//---------------------------------------------------------------
public String getNiceHostName() {
String hostName = getBoundHostValue();

0 comments on commit a36a38d

Please sign in to comment.