package org.ggp.base.player.proxy;

import java.io.BufferedReader;
import java.io.PrintStream;
import java.io.Serializable;
import java.net.SocketException;

import org.ggp.base.util.logging.GamerLogger;


public class ProxyMessage implements Serializable {
    private static final long serialVersionUID = 1237859L;
    
    public final long messageCode;
    public final long receptionTime;
    public final String theMessage;
    
    public ProxyMessage(String theMessage, long messageCode, long receptionTime) {
        this.theMessage = theMessage;
        this.messageCode = messageCode;
        this.receptionTime = receptionTime;
    }
    
    public String toString() {
        return "ProxyMessage<" + messageCode + ", " + receptionTime + ">[\"" + theMessage + "\"]"; 
    }
    
    public static ProxyMessage readFrom(BufferedReader theInput) throws SocketException {        
        try {            
            long messageCode = Long.parseLong(theInput.readLine());
            long receptionTime = Long.parseLong(theInput.readLine());
            String theMessage = theInput.readLine();
            return new ProxyMessage(theMessage, messageCode, receptionTime);
        } catch(SocketException se) {
            GamerLogger.log("Proxy", "[ProxyMessage Reader] Socket closed: stopping read operation.");
            throw se;
        } catch(Exception e) {
            GamerLogger.logStackTrace("Proxy", e);
            GamerLogger.logError("Proxy", "[ProxyMessage Reader] Could not digest message. Emptying stream.");
            try {
                // TODO: Fix this, I suspect it may be buggy.
                theInput.skip(Long.MAX_VALUE);
            } catch(SocketException se) {
                GamerLogger.log("Proxy", "[ProxyMessage Reader] Socket closed: stopping read operation.");
                throw se;                
            } catch(Exception ie) {
                GamerLogger.logStackTrace("Proxy", ie);
            }
            return null;
        }
    }
    
    public void writeTo(PrintStream theOutput) {
        theOutput.println(messageCode);
        theOutput.println(receptionTime);
        theOutput.println(theMessage);
    }
}
@@ -925,28 +925,16 @@
    try %7B
-            
 %0D%0A      
@@ -2094,28 +2094,59 @@
 put) %7B%0D%0A    
-    
+%09synchronized (theOutput) %7B%0D%0A    %09%09
 theOutput.pr
@@ -2162,36 +2162,34 @@
 sageCode);%0D%0A    
-    
+%09%09
 theOutput.printl
@@ -2211,20 +2211,18 @@
 );%0D%0A    
-    
+%09%09
 theOutpu
@@ -2241,20 +2241,28 @@
 eMessage);%0D%0A
+    %09%7D%0D%0A
     %7D%0D%0A%7D

Synchronize writing to the proxy channel to avoid interleaving messages.