1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
10 * =============================================================================
12 * For the license of bayes/sort.h and bayes/sort.c, please see the header
15 * ------------------------------------------------------------------------
17 * For the license of kmeans, please see kmeans/LICENSE.kmeans
19 * ------------------------------------------------------------------------
21 * For the license of ssca2, please see ssca2/COPYRIGHT
23 * ------------------------------------------------------------------------
25 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26 * header of the files.
28 * ------------------------------------------------------------------------
30 * For the license of lib/rbtree.h and lib/rbtree.c, please see
31 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33 * ------------------------------------------------------------------------
35 * Unless otherwise noted, the following license applies to STAMP files:
37 * Copyright (c) 2007, Stanford University
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions are
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
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
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.
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.
68 * =============================================================================
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)
79 public class Decoder {
81 MAP_T fragmentedMapPtr; /* contains list of packet_t* */
82 Queue_t decodedQueuePtr; /* contains decoded_t* */
86 /* =============================================================================
88 * =============================================================================
89 decoder_t* decoder_alloc ();
91 public static Decoder alloc() {
94 decoderPtr = new Decoder();
95 if(decoderPtr != null) {
96 decoderPtr.fragmentedMapPtr = MAP_ALLOC(0,0);
98 if(decoderPtr.fragmentedMapPtr == null)
100 System.out.println("Assert in Decoder.alloc");
104 decoderPtr.decodedQueuePtr = Queue_t.queue_alloc(1024);
105 if(decoderPtr.decodedQueuePtr == null)
107 System.out.println("Assert in Decoder.alloc");
117 /* =============================================================================
119 * =============================================================================
120 void decoder_free (decoder_t* decoderPtr);
124 /* =============================================================================
126 * =============================================================================
127 er_t decoder_process (decoder_t* decoderPtr, char* bytes, long numByte);
129 public int process(Packet bytes,int numByte)
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;
153 if ((fragmentId < 0) || (fragmentId >= numFragment)) {
154 return er.FRAGMENTID;
163 * Add to fragmented map for reassembling
166 if (numFragment > 1) {
168 List_t fragmentListPtr = (List_t)MAP_FIND(fragmentedMapPtr,flowId);
170 if (fragmentListPtr == null) {
172 fragmentListPtr = List_t.alloc(1); // packet_compareFragmentId
173 if(fragmentListPtr == null) {
174 System.out.println("Assertion in process");
178 status = MAP_INSERT(fragmentedMapPtr,flowId,fragmentListPtr);
180 System.out.println("Assertion in process");
185 it.reset(fragmentListPtr);
187 if(it.hasNext(fragmentListPtr)) {
188 System.out.println("Assertion in process");
192 Packet firstFragmentPtr = (Packet)it.next(fragmentListPtr);
193 int expectedNumFragment = firstFragmentPtr.numFragment;
195 if (numFragment != expectedNumFragment) {
196 status = MAP_REMOVE(fragmentedMapPtr,flowId);
198 System.out.println("Assertion in process");
202 status = fragmentListPtr.insert(packetPtr);
204 System.out.println("Assertion in process");
209 * If we have all thefragments we can reassemble them
212 if(fragmentListPtr.getSize() == numFragment) {
217 it.reset(fragmentListPtr);
219 while (it.hasNext(fragmentListPtr)) {
220 Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
222 if(fragmentPtr.fragmentId != i) {
223 status = MAP_REMOVE(fragmentedMapPtr,flowId);
226 System.out.println("Assertion in process");
229 return er.INCOMPLETE; /* should be sequential */
231 numBytes += fragmentPtr.length;
235 char[] data = new char[numBytes+1];
237 System.out.println("Assertion in process");
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);
247 for(i=0;i<fragmentPtr.length;i++) {
248 data[dst++] = fragmentPtr.data.charAt(i);
252 if(dst != (numBytes)) {
253 System.out.println("Assertion in process");
257 Decoded decodedPtr = new Decoded();
258 if(decodedPtr == null) {
259 System.out.println("Assertion in process");
263 decodedPtr.flowId = flowId;
264 decodedPtr.data = new String(data);
266 status = decodedQueuePtr.queue_push(decodedPtr);
269 System.out.println("Assertion in process");
273 status = MAP_REMOVE(fragmentedMapPtr,flowId);
276 System.out.println("Assertion in process");
286 * This is the only fragment, so it is ready
289 if (fragmentId != 0) {
290 return er.FRAGMENTID;
293 String data = new String();
295 System.out.println("Assertion in process");
299 data.concat(packetPtr.data);
301 Decoded decodedPtr = new Decoded();
303 if(decodedPtr == null) {
304 System.out.println("Assertion in process");
308 decodedPtr.flowId = flowId;
309 decodedPtr.data = data;
311 status = decodedQueuePtr.queue_push(decodedPtr);
314 System.out.println("Assertion in process");
327 /* =============================================================================
329 * =============================================================================
330 er_t TMdecoder_process (TM_ARGDECL decoder_t* decoderPtr, char* bytes, long numByte);
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) {
341 Decoded decodedPtr = (Decoded)decodedQueuePtr.queue_pop();
343 if(decodedPtr != null) {
344 decodedFlowId[0] = decodedPtr.flowId;
345 data = decodedPtr.data;
348 decodedFlowId[0] = -1;
357 /* =============================================================================
358 * TMdecoder_getComplete
359 * -- If none, returns NULL
360 * =============================================================================
361 char* TMdecoder_getComplete (TM_ARGDECL decoder_t* decoderPtr, long* decodedFlowIdPtr);
365 /* =============================================================================
367 * End of decoder.java
369 * =============================================================================