annotate src/goodjava/rpc/RpcCon.java @ 1808:69623c62aa34

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 May 2024 15:58:30 -0600
parents 973d3039c421
children
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;
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
15 import goodjava.io.CountingInputStream;
1118
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public class RpcCon {
1690
973d3039c421 backup server checks client domain
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
19 public final Socket socket;
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
20 private final DataInputStream in;
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
21 private final DataOutputStream out;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 InputStream inBinary = null;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 long lenBinary = -1;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
25 RpcCon(Socket socket)
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
26 throws RpcError
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
27 {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
28 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
29 this.socket = socket;
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
30 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
31 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
32 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
33 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
34 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
35 }
1118
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 public void close()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
39 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
41 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
42 socket.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
43 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
44 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
45 }
1118
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 public boolean isClosed() {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 return socket.isClosed();
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
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 void write(InputStream in,long lenIn,List list)
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
53 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 if( in != null )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 list.add(0,lenIn);
1419
59fd2e8b1b9d stringify and json_string
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
57 String json = new JsonToString().toString(list);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
58 try {
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
59 out.writeString(json);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
60 if( in != null ) {
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
61 CountingInputStream countIn = new CountingInputStream(in);
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
62 IoUtils.copyAll(countIn,out);
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1493
diff changeset
63 if( countIn.count() != lenIn ) {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
64 close();
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1494
diff changeset
65 throw new RpcError("InputStream wrong length "+countIn.count()+" when should be "+lenIn+" - list = "+list);
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
66 }
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
68 out.flush();
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
69 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
70 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
71 throw new RpcError(e);
1118
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 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 List readJson()
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
76 throws RpcError
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 {
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
78 try {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
79 if( inBinary != null ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
80 inBinary.close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
81 inBinary = null;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
82 lenBinary = -1;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
83 }
1492
aaac1d29edea better io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1419
diff changeset
84 String json = in.readString();
1119
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
85 List list = (List)JsonParser.parse(json);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
86 if( list.get(0) instanceof Long ) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
87 lenBinary = (Long)list.remove(0);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
88 inBinary = new FixedLengthInputStream(in,lenBinary);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
89 }
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
90 return list;
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
91 } catch(IOException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
92 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
93 throw new RpcError(e);
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
94 } catch(ParseException e) {
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
95 close();
87c674f3f6b7 add RpcError
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
96 throw new RpcError(e);
1118
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 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 }