4733da878e5bb967f450ff78f5fbb78706751ce2
[IRC.git] / Robust / src / Benchmarks / SingleTM / Intruder / Decoder.java
1 /* =============================================================================
2  *
3  * decoder.java
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70
71
72 #define MAP_T                       RBTree
73 #define MAP_ALLOC(hash, cmp)        RBTree.alloc(cmp)
74 #define MAP_INSERT(map, key, data)  map.insert(key, data)
75 #define MAP_CONTAINS(map, key)      map.contains(key)
76 #define MAP_FIND(map,key)           map.get(key)
77 #define MAP_REMOVE(map,key)         map.deleteNode(key)
78
79 public class Decoder {
80
81     MAP_T fragmentedMapPtr;     /* contains list of packet_t* */
82     Queue_t decodedQueuePtr;    /* contains decoded_t*  */
83
84     public Decoder() {}
85
86 /* =============================================================================
87  * decoder_alloc
88  * =============================================================================
89  decoder_t* decoder_alloc ();
90   */
91     public static Decoder alloc() {
92         Decoder decoderPtr;
93
94         decoderPtr = new Decoder();
95         if(decoderPtr != null) {
96             decoderPtr.fragmentedMapPtr = MAP_ALLOC(0,0);
97
98             if(decoderPtr.fragmentedMapPtr == null)
99             {
100                 System.out.println("Assert in Decoder.alloc");
101                 System.exit(1);
102             }
103             
104             decoderPtr.decodedQueuePtr = Queue_t.queue_alloc(1024);
105             if(decoderPtr.decodedQueuePtr == null)
106             {
107                 System.out.println("Assert in Decoder.alloc");
108                 System.exit(1);
109             }
110
111         }
112
113         return decoderPtr;
114     }
115
116
117 /* =============================================================================
118  * decoder_free
119  * =============================================================================
120  void decoder_free (decoder_t* decoderPtr);
121  */
122
123
124 /* =============================================================================
125  * decoder_process
126  * =============================================================================
127  er_t decoder_process (decoder_t* decoderPtr, char* bytes, long numByte);
128  */
129     public int process(Packet bytes,int numByte)
130     {
131         boolean status;
132         ERROR er;
133
134         /*
135          * Basic er checing
136          */
137
138         if (numByte < 0) {
139             return er.SHORT;
140         }
141
142         /* need to look into it later */
143         Packet packetPtr = (Packet)bytes;
144         int flowId = packetPtr.flowId;
145         int fragmentId = packetPtr.fragmentId;
146         int numFragment = packetPtr.numFragment;
147         int length = packetPtr.length;
148
149         if (flowId < 0) {
150             return er.FLOWID;
151         }
152
153         if ((fragmentId < 0) || (fragmentId >= numFragment)) {
154             return er.FRAGMENTID;
155         }
156
157         if (length < 0) {
158             return er.LENGTH;
159         }
160
161     
162         /*
163          * Add to fragmented map for reassembling
164          */
165
166         if (numFragment > 1) {
167
168             List_t fragmentListPtr = (List_t)MAP_FIND(fragmentedMapPtr,flowId);
169
170             if (fragmentListPtr == null) {
171
172                 fragmentListPtr = List_t.alloc(1);      // packet_compareFragmentId
173                 if(fragmentListPtr == null) {
174                     System.out.println("Assertion in process");
175                     System.exit(1);
176                 }
177
178                 status = MAP_INSERT(fragmentedMapPtr,flowId,fragmentListPtr);
179                 if(status) {
180                     System.out.println("Assertion in process");
181                     System.exit(1);                                
182                 }
183             } else {
184                 List_Iter it;
185                 it.reset(fragmentListPtr);
186                 
187                 if(it.hasNext(fragmentListPtr)) {
188                     System.out.println("Assertion in process");
189                     System.exit(1);
190                 }
191
192                 Packet firstFragmentPtr = (Packet)it.next(fragmentListPtr);
193                 int expectedNumFragment = firstFragmentPtr.numFragment;
194
195                 if (numFragment != expectedNumFragment) {
196                     status = MAP_REMOVE(fragmentedMapPtr,flowId);
197                     if(status) {
198                         System.out.println("Assertion in process");
199                         System.exit(1);
200                     }
201
202                     status = fragmentListPtr.insert(packetPtr);
203                     if(status) {
204                         System.out.println("Assertion in process");
205                         System.exit(1);
206                     }
207
208                     /*
209                      * If we have all thefragments we can reassemble them
210                      */
211
212                     if(fragmentListPtr.getSize() ==  numFragment) {
213
214
215                         int numBytes = 0;
216                         int i = 0;
217                         it.reset(fragmentListPtr);
218
219                         while (it.hasNext(fragmentListPtr)) {
220                             Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
221
222                             if(fragmentPtr.fragmentId != i) {
223                                 status = MAP_REMOVE(fragmentedMapPtr,flowId);
224                                 
225                                 if(status) {
226                                     System.out.println("Assertion in process");
227                                     System.exit(1);
228                                 }
229                                return er.INCOMPLETE; /* should be sequential */
230                             }
231                                 numBytes += fragmentPtr.length;
232                                 i++;
233                         }
234
235                         char[] data = new char[numBytes+1];
236                         if(data == null) {
237                              System.out.println("Assertion in process");
238                              System.exit(1);
239                         }
240
241                         data[numBytes] = '\0';
242                         int dst = 0;            // index of data
243                         it.reset(fragmentListPtr);
244                         while(it.hasNext(fragmentListPtr)) {
245                             Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
246
247                             for(i=0;i<fragmentPtr.length;i++) {                      
248                                 data[dst++] = fragmentPtr.data.charAt(i);
249                             }
250                         }
251                         
252                         if(dst != (numBytes)) {
253                             System.out.println("Assertion in process");                         
254                             System.exit(1);
255                         }
256
257                         Decoded decodedPtr = new Decoded();
258                         if(decodedPtr == null) {
259                              System.out.println("Assertion in process");                    
260                              System.exit(1);
261                         }
262
263                         decodedPtr.flowId = flowId;
264                         decodedPtr.data = new String(data);
265
266                         status = decodedQueuePtr.queue_push(decodedPtr);
267
268                         if(status) {
269                             System.out.println("Assertion in process");  
270                             System.exit(1);
271                         }
272
273                         status = MAP_REMOVE(fragmentedMapPtr,flowId);
274
275                         if(status) {
276                             System.out.println("Assertion in process");                         
277                             System.exit(1);
278                         }
279                     }                
280                 } 
281             }
282
283         }else {
284
285                 /*
286                  * This is the only fragment, so it is ready
287                  */
288
289                 if (fragmentId != 0) {
290                     return er.FRAGMENTID;
291                 }
292
293                 String data = new String();
294                 if(data == null) {
295                     System.out.println("Assertion in process");                           
296                     System.exit(1);
297                 }
298
299                 data.concat(packetPtr.data);
300
301                Decoded decodedPtr = new Decoded();
302
303                 if(decodedPtr == null) {
304                     System.out.println("Assertion in process");                           
305                     System.exit(1);
306                 }
307
308                 decodedPtr.flowId = flowId;
309                 decodedPtr.data = data;
310
311                 status = decodedQueuePtr.queue_push(decodedPtr);
312                 
313                 if(status) {
314                     System.out.println("Assertion in process");                           
315                     System.exit(1);
316                 }
317             }
318
319     
320         return er.NONE;
321
322             
323         
324     }
325
326
327 /* =============================================================================
328  * TMdecoder_process
329  * =============================================================================
330  er_t TMdecoder_process (TM_ARGDECL  decoder_t* decoderPtr, char* bytes, long numByte);
331  */
332
333
334 /* =============================================================================
335  * decoder_getComplete
336  * -- If none, returns NULL
337  * =============================================================================
338  char* decoder_getComplete (decoder_t* decoderPtr, long* decodedFlowIdPtr); */
339     public String getComplete(int[] decodedFlowId) {
340         String data;
341         Decoded decodedPtr = (Decoded)decodedQueuePtr.queue_pop();
342
343         if(decodedPtr != null) {
344             decodedFlowId[0] = decodedPtr.flowId;
345             data = decodedPtr.data;
346             decodedPtr = null;
347         } else {
348             decodedFlowId[0] = -1;
349             data= null;
350         }
351
352         return data;
353     }
354
355 }
356
357 /* =============================================================================
358  * TMdecoder_getComplete
359  * -- If none, returns NULL
360  * =============================================================================
361  char* TMdecoder_getComplete (TM_ARGDECL  decoder_t* decoderPtr, long* decodedFlowIdPtr);
362  */
363
364
365 /* =============================================================================
366  *
367  * End of decoder.java
368  *
369  * =============================================================================
370  */