Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include "BinaryXMLElementReader.h" |
| 7 | |
| 8 | ndn_Error ndn_BinaryXMLElementReader_onReceivedData |
| 9 | (struct ndn_BinaryXMLElementReader *self, unsigned char *data, unsigned int dataLength) |
| 10 | { |
| 11 | // Process multiple objects in the data. |
| 12 | while(1) { |
| 13 | // Scan the input to check if a whole binary XML object has been read. |
| 14 | ndn_BinaryXMLStructureDecoder_seek(&self->structureDecoder, 0); |
| 15 | |
| 16 | ndn_Error error; |
| 17 | if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength)) |
| 18 | return error; |
| 19 | if (self->structureDecoder.gotElementEnd) { |
| 20 | // Got the remainder of an element. Report to the caller. |
| 21 | #if 0 // TODO: implement saving data parts. |
| 22 | this.dataParts.push(data.subarray(0, this.structureDecoder.offset)); |
| 23 | var element = DataUtils.concatArrays(this.dataParts); |
| 24 | this.dataParts = []; |
| 25 | #endif |
| 26 | (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset); |
| 27 | |
| 28 | // Need to read a new object. |
| 29 | data += self->structureDecoder.offset; |
| 30 | dataLength -= self->structureDecoder.offset; |
| 31 | ndn_BinaryXMLStructureDecoder_init(&self->structureDecoder); |
| 32 | if (dataLength == 0) |
| 33 | // No more data in the packet. |
| 34 | return 0; |
| 35 | |
| 36 | // else loop back to decode. |
| 37 | } |
| 38 | else { |
| 39 | #if 0 // TODO: implement saving data parts. |
| 40 | // Save for a later call to concatArrays so that we only copy data once. |
| 41 | this.dataParts.push(data); |
| 42 | #else |
| 43 | return -1; // TODO: implement saving data parts. |
| 44 | #endif |
| 45 | return 0; |
| 46 | } |
| 47 | } |
| 48 | } |