Mercurial Hosting > luan
annotate src/goodjava/rpc/RpcCon.java @ 1495:2e8a5df45d56
better xml
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 May 2020 11:03:48 -0600 |
parents | 91c167099462 |
children | 22e15cf73040 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1120
diff
changeset
|
1 package goodjava.rpc; |
1118 | 2 |
3 import java.io.InputStream; | |
1492 | 4 import java.io.BufferedOutputStream; |
1118 | 5 import java.io.IOException; |
6 import java.net.Socket; | |
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 | 11 import goodjava.io.BufferedInputStream; |
12 import goodjava.io.DataInputStream; | |
13 import goodjava.io.DataOutputStream; | |
1493 | 14 import goodjava.io.IoUtils; |
1494 | 15 import goodjava.io.CountingInputStream; |
1118 | 16 |
17 | |
18 public class RpcCon { | |
1492 | 19 private final Socket socket; |
20 private final DataInputStream in; | |
21 private final DataOutputStream out; | |
1118 | 22 InputStream inBinary = null; |
23 long lenBinary = -1; | |
24 | |
1119 | 25 RpcCon(Socket socket) |
26 throws RpcError | |
27 { | |
28 try { | |
29 this.socket = socket; | |
1492 | 30 this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
31 this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); | |
1119 | 32 } catch(IOException e) { |
33 close(); | |
34 throw new RpcError(e); | |
35 } | |
1118 | 36 } |
37 | |
38 public void close() | |
1119 | 39 throws RpcError |
1118 | 40 { |
1119 | 41 try { |
42 socket.close(); | |
43 } catch(IOException e) { | |
44 throw new RpcError(e); | |
45 } | |
1118 | 46 } |
47 | |
48 public boolean isClosed() { | |
49 return socket.isClosed(); | |
50 } | |
51 | |
52 void write(InputStream in,long lenIn,List list) | |
1119 | 53 throws RpcError |
1118 | 54 { |
55 if( in != null ) | |
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 | 58 try { |
1492 | 59 out.writeString(json); |
1119 | 60 if( in != null ) { |
1494 | 61 CountingInputStream countIn = new CountingInputStream(in); |
62 IoUtils.copyAll(countIn,out); | |
63 countIn.close(); | |
64 if( countIn.count() != lenIn ) { | |
1119 | 65 close(); |
1494 | 66 throw new RpcError("InputStream wrong length "+countIn.count()+" when should be "+lenIn); |
1119 | 67 } |
1118 | 68 } |
1492 | 69 out.flush(); |
1119 | 70 } catch(IOException e) { |
71 close(); | |
72 throw new RpcError(e); | |
1118 | 73 } |
74 } | |
75 | |
76 List readJson() | |
1119 | 77 throws RpcError |
1118 | 78 { |
1119 | 79 try { |
80 if( inBinary != null ) { | |
81 inBinary.close(); | |
82 inBinary = null; | |
83 lenBinary = -1; | |
84 } | |
1492 | 85 String json = in.readString(); |
1119 | 86 List list = (List)JsonParser.parse(json); |
87 if( list.get(0) instanceof Long ) { | |
88 lenBinary = (Long)list.remove(0); | |
89 inBinary = new FixedLengthInputStream(in,lenBinary); | |
90 } | |
91 return list; | |
92 } catch(IOException e) { | |
93 close(); | |
94 throw new RpcError(e); | |
95 } catch(ParseException e) { | |
96 close(); | |
97 throw new RpcError(e); | |
1118 | 98 } |
99 } | |
100 | |
101 } |