@@ -60,60 +60,90 @@ static void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
6060 System .out .println (b .toString ());
6161 }
6262
63- static void readChunk (ASP3ChunkStorage chunkStorage , ASP3Storage storage ,
64- DataInputStream dis ) throws IOException {
63+ /**
64+ * This methods reads until an IOException notifies closed connection.
65+ * It expects serialized chunks (plural!) which are written to received
66+ * message storage.
67+ *
68+ * @param chunkStorage where the received chunks are stored
69+ * @param storage that looks like a design glitch TODO
70+ * @param dis input stream to read serialized chunks
71+ * @param listener to be notified about a successfully deserialized chunk
72+ * @throws IOException
73+ */
74+ static void readChunks (String sender , ASP3ChunkStorage chunkStorage ,
75+ ASP3Storage storage , DataInputStream dis ,
76+ ASP3ReceivedChunkListener listener ) throws IOException {
6577
6678 try {
67- // read URI
68- String uri = dis .readUTF ();
69-
70- // read number of messages
71- int number = dis .readInt ();
72-
73- //<<<<<<<<<<<<<<<<<<debug
74- StringBuilder b = new StringBuilder ();
75- b .append ("read chunkURI / #messages " );
76- b .append (uri );
77- b .append (" / " );
78- b .append (number );
79- System .out .println (b .toString ());
80- //>>>>>>>>>>>>>>>>>>>debug
81- ASP3Chunk chunk =
82- storage .getChunk (uri , chunkStorage .getEra ());
83-
84- if (chunk != null ) {
79+ while (true ) { // until IOException informs end of communication
80+ // read URI
81+ String uri = dis .readUTF ();
82+
83+ // read number of messages
84+ int number = dis .readInt ();
85+
8586 //<<<<<<<<<<<<<<<<<<debug
86- b = new StringBuilder ();
87- b .append ("got chunk: " );
87+ StringBuilder b = new StringBuilder ();
88+ b .append ("ASPChunkDeserialization: " );
89+ b .append ("read chunkURI / #messages / sender" );
8890 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: " );
9591 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- }
101-
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 );
92+ b .append (number );
93+ b .append (" / " );
94+ b .append (sender );
10995 System .out .println (b .toString ());
11096 //>>>>>>>>>>>>>>>>>>>debug
97+ ASP3Chunk chunk =
98+ storage .getChunk (uri , chunkStorage .getEra ());
99+
100+ if (chunk != null ) {
101+ //<<<<<<<<<<<<<<<<<<debug
102+ b = new StringBuilder ();
103+ b .append ("ASPChunkDeserialization: " );
104+ b .append ("got chunk: " );
105+ b .append (uri );
106+ System .out .println (b .toString ());
107+ //>>>>>>>>>>>>>>>>>>>debug
108+ } else {
109+ //<<<<<<<<<<<<<<<<<<debug
110+ b = new StringBuilder ();
111+ b .append ("ASPChunkDeserialization: " );
112+ b .append ("ERROR: no chunk found for sender/uri: " );
113+ b .append (" / " );
114+ b .append (uri );
115+ System .err .println (b .toString ());
116+ //>>>>>>>>>>>>>>>>>>>debug
117+ throw new IOException ("couldn't create local chunk storage - give up" );
118+ }
111119
112- chunk .add (message );
120+ for (;number > 0 ; number --) {
121+ // escapes with IOException
122+ String message = dis .readUTF ();
123+ //<<<<<<<<<<<<<<<<<<debug
124+ b = new StringBuilder ();
125+ b .append ("ASPChunkDeserialization: " );
126+ b .append ("read message: " );
127+ b .append (message );
128+ System .out .println (b .toString ());
129+ //>>>>>>>>>>>>>>>>>>>debug
130+
131+ chunk .add (message );
132+ }
133+
134+ // read all messages
135+ listener .chunkReceived (sender , uri , chunkStorage .getEra ());
113136 }
114137
115138 } catch (IOException ex ) {
116- // done
139+ // done - connection closed
140+
141+ //<<<<<<<<<<<<<<<<<<debug
142+ StringBuilder b = new StringBuilder ();
143+ b .append ("ASPChunkDeserialization: " );
144+ b .append ("connection close - done" );
145+ System .out .println (b .toString ());
146+ //>>>>>>>>>>>>>>>>>>>debug
117147 }
118148 }
119149}
0 commit comments