diff src/goodjava/rpc/RpcServer.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/rpc/RpcServer.java@e8fc6712b468
children aaac1d29edea
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/rpc/RpcServer.java	Tue Sep 17 01:35:01 2019 -0400
@@ -0,0 +1,55 @@
+package goodjava.rpc;
+
+import java.io.EOFException;
+import java.net.Socket;
+import java.util.List;
+import java.util.ArrayList;
+
+
+public class RpcServer extends RpcCon {
+
+	public RpcServer(Socket socket)
+		throws RpcError
+	{
+		super(socket);
+	}
+
+	public RpcCall read()
+		throws RpcError
+	{
+		try {
+			List list = readJson();
+			String cmd = (String)list.remove(0);
+			Object[] args = list.toArray();
+			return new RpcCall(inBinary,lenBinary,cmd,args);
+		} catch(RpcError e) {
+			if( !readSome && e.getCause() instanceof EOFException )
+				return null;
+			throw e;
+		}
+	}
+
+	public void write(RpcResult result)
+		throws RpcError
+	{
+		List list = new ArrayList();
+		list.add(true);
+		for( Object val : result.returnValues ) {
+			list.add(val);
+		}
+		write(result.in,result.lenIn,list);
+	}
+
+	public void write(RpcException ex)
+		throws RpcError
+	{
+		List list = new ArrayList();
+		list.add(false);
+		list.add(ex.getMessage());
+		for( Object val : ex.values ) {
+			list.add(val);
+		}
+		write(ex.in,ex.lenIn,list);
+	}
+
+}