annotate src/goodjava/rpc/RpcCon.java @ 1493:471ef3e6a84e

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 00:12:15 -0600
parents aaac1d29edea
children 91c167099462
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;
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
4 import java.io.BufferedOutputStream;
1118
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.net.Socket;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.List;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
8 import goodjava.parser.ParseException;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
9 import goodjava.json.JsonParser;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1120
diff changeset
10 import goodjava.json.JsonToString;
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
11 import goodjava.io.BufferedInputStream;
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
12 import goodjava.io.DataInputStream;
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
13 import goodjava.io.DataOutputStream;
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1492
diff changeset
14 import goodjava.io.IoUtils;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 public class RpcCon {
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
18 private final Socket socket;
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
19 private final DataInputStream in;
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
20 private final DataOutputStream out;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 InputStream inBinary = null;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 long lenBinary = -1;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
24 RpcCon(Socket socket)
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
25 throws RpcError
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
26 {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
27 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
28 this.socket = socket;
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
29 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
30 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
31 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
32 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
33 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
34 }
1118
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 public void close()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
38 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
40 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
41 socket.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
42 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
43 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
44 }
1118
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 public boolean isClosed() {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return socket.isClosed();
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 void write(InputStream in,long lenIn,List list)
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
52 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 if( in != null )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 list.add(0,lenIn);
1419
59fd2e8b1b9d stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
56 String json = new JsonToString().toString(list);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
57 try {
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
58 out.writeString(json);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
59 if( in != null ) {
1493
471ef3e6a84e more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1492
diff changeset
60 long total = IoUtils.copyAll(in,out);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
61 if( total != lenIn ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
62 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
63 throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
64 }
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
66 out.flush();
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
67 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
68 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
69 throw new RpcError(e);
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 List readJson()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
74 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
76 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
77 if( inBinary != null ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
78 inBinary.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
79 inBinary = null;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
80 lenBinary = -1;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
81 }
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
82 String json = in.readString();
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
83 List list = (List)JsonParser.parse(json);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
84 if( list.get(0) instanceof Long ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
85 lenBinary = (Long)list.remove(0);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
86 inBinary = new FixedLengthInputStream(in,lenBinary);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
87 }
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
88 return list;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
89 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
90 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
91 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
92 } catch(ParseException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
93 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
94 throw new RpcError(e);
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 }