changeset 1119:87c674f3f6b7

add RpcError
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Aug 2017 12:35:45 -0600
parents e4710ddfd287
children e8fc6712b468
files src/luan/lib/rpc/RpcClient.java src/luan/lib/rpc/RpcCon.java src/luan/lib/rpc/RpcError.java src/luan/lib/rpc/RpcServer.java
diffstat 4 files changed, 89 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/rpc/RpcClient.java	Sun Aug 06 20:11:11 2017 -0600
+++ b/src/luan/lib/rpc/RpcClient.java	Mon Aug 07 12:35:45 2017 -0600
@@ -1,6 +1,5 @@
 package luan.lib.rpc;
 
-import java.io.IOException;
 import java.net.Socket;
 import java.util.List;
 import java.util.ArrayList;
@@ -8,12 +7,14 @@
 
 public class RpcClient extends RpcCon {
 
-	public RpcClient(Socket socket) throws IOException {
+	public RpcClient(Socket socket)
+		throws RpcError
+	{
 		super(socket);
 	}
 
 	public void write(RpcCall call)
-		throws IOException
+		throws RpcError
 	{
 		List list = new ArrayList();
 		list.add(call.cmd);
@@ -24,7 +25,7 @@
 	}
 
 	public RpcResult read()
-		throws IOException, RpcException
+		throws RpcError, RpcException
 	{
 		List list = readJson();
 		boolean ok = (Boolean)list.remove(0);
--- a/src/luan/lib/rpc/RpcCon.java	Sun Aug 06 20:11:11 2017 -0600
+++ b/src/luan/lib/rpc/RpcCon.java	Mon Aug 07 12:35:45 2017 -0600
@@ -19,16 +19,27 @@
 	InputStream inBinary = null;
 	long lenBinary = -1;
 
-	RpcCon(Socket socket) throws IOException {
-		this.socket = socket;
-		this.in = socket.getInputStream();
-		this.out = socket.getOutputStream();
+	RpcCon(Socket socket)
+		throws RpcError
+	{
+		try {
+			this.socket = socket;
+			this.in = socket.getInputStream();
+			this.out = socket.getOutputStream();
+		} catch(IOException e) {
+			close();
+			throw new RpcError(e);
+		}
 	}
 
 	public void close()
-		throws IOException
+		throws RpcError
 	{
-		socket.close();
+		try {
+			socket.close();
+		} catch(IOException e) {
+			throw new RpcError(e);
+		}
 	}
 
 	public boolean isClosed() {
@@ -36,7 +47,7 @@
 	}
 
 	void write(InputStream in,long lenIn,List list)
-		throws IOException
+		throws RpcError
 	{
 		if( in != null )
 			list.add(0,lenIn);
@@ -49,49 +60,59 @@
         a[2] = (byte)(len >>>  8);
         a[3] = (byte)(len >>>  0);
 		System.arraycopy(aJson,0,a,4,len);
-		out.write(a);
-		if( in != null ) {
-			a = new byte[8192];
-			long total = 0;
-			int n;
-			while( (n=in.read(a)) != -1 ) {
-				out.write(a,0,n);
-				total += n;
+		try {
+			out.write(a);
+			if( in != null ) {
+				a = new byte[8192];
+				long total = 0;
+				int n;
+				while( (n=in.read(a)) != -1 ) {
+					out.write(a,0,n);
+					total += n;
+				}
+				if( total != lenIn ) {
+					close();
+					throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
+				}
 			}
-			if( total != lenIn )
-				throw new IOException("InputStream wrong length "+total+" when should be "+lenIn);
+		} catch(IOException e) {
+			close();
+			throw new RpcError(e);
 		}
 	}
 
 	List readJson()
-		throws IOException
+		throws RpcError
 	{
-		if( inBinary != null ) {
-			inBinary.close();
-			inBinary = null;
-			lenBinary = -1;
-		}
-		byte[] a = new byte[4];
-		readAll(a);
-		int len = 0;
-		for( byte b : a ) {
-			len <<= 8;
-			len |= b&0xFF;
+		try {
+			if( inBinary != null ) {
+				inBinary.close();
+				inBinary = null;
+				lenBinary = -1;
+			}
+			byte[] a = new byte[4];
+			readAll(a);
+			int len = 0;
+			for( byte b : a ) {
+				len <<= 8;
+				len |= b&0xFF;
+			}
+			a = new byte[len];
+			readAll(a);
+			String json = new String(a,StandardCharsets.UTF_8);
+			List list = (List)JsonParser.parse(json);
+			if( list.get(0) instanceof Long ) {
+				lenBinary = (Long)list.remove(0);
+				inBinary = new FixedLengthInputStream(in,lenBinary);
+			}
+			return list;
+		} catch(IOException e) {
+			close();
+			throw new RpcError(e);
+		} catch(ParseException e) {
+			close();
+			throw new RpcError(e);
 		}
-		a = new byte[len];
-		readAll(a);
-		String json = new String(a,StandardCharsets.UTF_8);
-		List list;
-		try {
-			list = (List)JsonParser.parse(json);
-		} catch(ParseException e) {
-			throw new IOException(e);
-		}
-		if( list.get(0) instanceof Long ) {
-			lenBinary = (Long)list.remove(0);
-			inBinary = new FixedLengthInputStream(in,lenBinary);
-		}
-		return list;
 	}
 
 	private void readAll(final byte[] a) throws IOException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/rpc/RpcError.java	Mon Aug 07 12:35:45 2017 -0600
@@ -0,0 +1,14 @@
+package luan.lib.rpc;
+
+
+public class RpcError extends RuntimeException {
+
+	public RpcError(String msg) {
+		super(msg);
+	}
+
+	public RpcError(Exception e) {
+		super(e);
+	}
+
+}
--- a/src/luan/lib/rpc/RpcServer.java	Sun Aug 06 20:11:11 2017 -0600
+++ b/src/luan/lib/rpc/RpcServer.java	Mon Aug 07 12:35:45 2017 -0600
@@ -1,6 +1,5 @@
 package luan.lib.rpc;
 
-import java.io.IOException;
 import java.net.Socket;
 import java.util.List;
 import java.util.ArrayList;
@@ -8,12 +7,14 @@
 
 public class RpcServer extends RpcCon {
 
-	public RpcServer(Socket socket) throws IOException {
+	public RpcServer(Socket socket)
+		throws RpcError
+	{
 		super(socket);
 	}
 
 	public RpcCall read()
-		throws IOException
+		throws RpcError
 	{
 		List list = readJson();
 		String cmd = (String)list.remove(0);
@@ -22,7 +23,7 @@
 	}
 
 	public void write(RpcResult result)
-		throws IOException
+		throws RpcError
 	{
 		List list = new ArrayList();
 		list.add(true);
@@ -33,7 +34,7 @@
 	}
 
 	public void write(RpcException ex)
-		throws IOException
+		throws RpcError
 	{
 		List list = new ArrayList();
 		list.add(false);