annotate src/goodjava/rpc/RpcCon.java @ 1471:f5b8c7e1ac82

xml
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2020 20:20:45 -0600
parents 59fd2e8b1b9d
children aaac1d29edea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
1 package goodjava.rpc;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.InputStream;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.OutputStream;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.EOFException;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.net.Socket;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.nio.charset.StandardCharsets;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.List;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
10 import goodjava.parser.ParseException;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
11 import goodjava.json.JsonParser;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
12 import goodjava.json.JsonToString;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public class RpcCon {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 final Socket socket;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 final InputStream in;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 final OutputStream out;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 InputStream inBinary = null;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 long lenBinary = -1;
1120
e8fc6712b468 luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents: 1119
diff changeset
21 boolean readSome = false;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
23 RpcCon(Socket socket)
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
24 throws RpcError
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
25 {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
26 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
27 this.socket = socket;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
28 this.in = socket.getInputStream();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
29 this.out = socket.getOutputStream();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
30 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
31 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
32 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
33 }
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 public void close()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
37 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
39 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
40 socket.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
41 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
42 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
43 }
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 public boolean isClosed() {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return socket.isClosed();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 void write(InputStream in,long lenIn,List list)
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
51 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if( in != null )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 list.add(0,lenIn);
1419
59fd2e8b1b9d stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
55 String json = new JsonToString().toString(list);
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 byte[] aJson = json.getBytes(StandardCharsets.UTF_8);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 int len = aJson.length;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 byte[] a = new byte[4+len];
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 a[0] = (byte)(len >>> 24);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 a[1] = (byte)(len >>> 16);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 a[2] = (byte)(len >>> 8);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 a[3] = (byte)(len >>> 0);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 System.arraycopy(aJson,0,a,4,len);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
64 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
65 out.write(a);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
66 if( in != null ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
67 a = new byte[8192];
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
68 long total = 0;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
69 int n;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
70 while( (n=in.read(a)) != -1 ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
71 out.write(a,0,n);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
72 total += n;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
73 }
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
74 if( total != lenIn ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
75 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
76 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
77 }
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
79 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
80 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
81 throw new RpcError(e);
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 List readJson()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
86 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
88 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
89 if( inBinary != null ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
90 inBinary.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
91 inBinary = null;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
92 lenBinary = -1;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
93 }
1120
e8fc6712b468 luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents: 1119
diff changeset
94 readSome = false;
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
95 byte[] a = new byte[4];
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
96 readAll(a);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
97 int len = 0;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
98 for( byte b : a ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
99 len <<= 8;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
100 len |= b&0xFF;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
101 }
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
102 a = new byte[len];
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
103 readAll(a);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
104 String json = new String(a,StandardCharsets.UTF_8);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
105 List list = (List)JsonParser.parse(json);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
106 if( list.get(0) instanceof Long ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
107 lenBinary = (Long)list.remove(0);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
108 inBinary = new FixedLengthInputStream(in,lenBinary);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
109 }
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
110 return list;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
111 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
112 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
113 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
114 } catch(ParseException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
115 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
116 throw new RpcError(e);
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 private void readAll(final byte[] a) throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 int total = 0;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 int n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 while( total < a.length ){
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 n = in.read( a, total, a.length-total );
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 if( n == -1 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 throw new EOFException();
1120
e8fc6712b468 luan Rpc uses luan.lib.rpc
Franklin Schmidt <fschmidt@gmail.com>
parents: 1119
diff changeset
127 readSome = true;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 total += n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 }