1010 * @author thsc
1111 */
1212abstract class ASP3ChunkSerialization {
13- static void sendChunk (ASP3Chunk2Send chunk , DataOutputStream dos )
13+ static void sendChunk (ASP3Chunk chunk , DataOutputStream dos )
1414 throws IOException {
1515
1616 //<<<<<<<<<<<<<<<<<<debug
1717 StringBuilder b = new StringBuilder ();
18- b .append ("send chunk url: " );
18+ b .append ("send chunk url / #messages : " );
1919 b .append (chunk .getUri ());
20+ b .append (" / " );
21+ b .append (chunk .getNumberMessage ());
2022 System .out .println (b .toString ());
2123 //>>>>>>>>>>>>>>>>>>>debug
2224
2325 // send url
2426 dos .writeUTF (chunk .getUri ());
27+
28+ // send #messages
29+ dos .writeInt (chunk .getNumberMessage ());
2530
2631 Iterator <CharSequence > messages = chunk .getMessages ();
2732 //<<<<<<<<<<<<<<<<<<debug
@@ -38,6 +43,7 @@ static void sendChunk(ASP3Chunk2Send chunk, DataOutputStream dos)
3843 System .out .println (b .toString ());
3944 //>>>>>>>>>>>>>>>>>>>debug
4045
46+ // send message
4147 dos .writeUTF ((String ) message );
4248
4349 //<<<<<<<<<<<<<<<<<<debug
@@ -54,20 +60,60 @@ static void sendChunk(ASP3Chunk2Send chunk, DataOutputStream dos)
5460 System .out .println (b .toString ());
5561 }
5662
57- static void readChunk (ASP3ChunkReceived chunk , DataInputStream dis )
58- throws IOException {
63+ static void readChunk (ASP3ChunkStorage chunkStorage , ASP3Storage storage ,
64+ DataInputStream dis ) throws IOException {
5965
60- for (;;) {
61- // escapes with IOException
62- String message = dis .readUTF ();
66+ try {
67+ // read URI
68+ String uri = dis .readUTF ();
69+
70+ // read number of messages
71+ int number = dis .readInt ();
72+
6373 //<<<<<<<<<<<<<<<<<<debug
6474 StringBuilder b = new StringBuilder ();
65- b .append ("chunk deserialisation read message: " );
66- b .append (message );
75+ b .append ("read chunkURI / #messages " );
76+ b .append (uri );
77+ b .append (" / " );
78+ b .append (number );
6779 System .out .println (b .toString ());
6880 //>>>>>>>>>>>>>>>>>>>debug
81+ ASP3Chunk chunk =
82+ storage .getChunk (uri , chunkStorage .getEra ());
83+
84+ if (chunk != null ) {
85+ //<<<<<<<<<<<<<<<<<<debug
86+ b = new StringBuilder ();
87+ b .append ("got chunk: " );
88+ b .append (uri );
89+ System .out .println (b .toString ());
90+ //>>>>>>>>>>>>>>>>>>>debug
91+ } else {
92+ //<<<<<<<<<<<<<<<<<<debug
93+ b = new StringBuilder ();
94+ b .append ("ERROR: no chunk found for sender/uri: " );
95+ b .append (" / " );
96+ b .append (uri );
97+ System .err .println (b .toString ());
98+ //>>>>>>>>>>>>>>>>>>>debug
99+ throw new IOException ("couldn't create local chunk storage - give up" );
100+ }
69101
70- chunk .addReceivedMessage (message );
102+ for (;number > 0 ; number --) {
103+ // escapes with IOException
104+ String message = dis .readUTF ();
105+ //<<<<<<<<<<<<<<<<<<debug
106+ b = new StringBuilder ();
107+ b .append ("chunk deserialisation read message: " );
108+ b .append (message );
109+ System .out .println (b .toString ());
110+ //>>>>>>>>>>>>>>>>>>>debug
111+
112+ chunk .add (message );
113+ }
114+
115+ } catch (IOException ex ) {
116+ // done
71117 }
72118 }
73119}
0 commit comments