IBR-DTNSuite  0.8
ibrdtn/ibrdtn/streams/StreamDataSegment.cpp
Go to the documentation of this file.
00001 /*
00002  * StreamDataSegment.cpp
00003  *
00004  *  Created on: 30.06.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/streams/StreamDataSegment.h"
00009 #include "ibrdtn/data/SDNV.h"
00010 
00011 using namespace std;
00012 
00013 namespace dtn
00014 {
00015         namespace streams
00016         {
00017                 StreamDataSegment::StreamDataSegment(SegmentType type, size_t size)
00018                  : _value(size), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
00019                 {
00020                 }
00021 
00022                 StreamDataSegment::StreamDataSegment(SegmentType type)
00023                 : _value(0), _type(type), _reason(MSG_SHUTDOWN_IDLE_TIMEOUT), _flags(0)
00024                 {
00025                 }
00026 
00027                 StreamDataSegment::StreamDataSegment(ShutdownReason reason, size_t reconnect)
00028                 : _value(reconnect), _type(MSG_SHUTDOWN), _reason(reason), _flags(3)
00029                 {
00030                 }
00031 
00032                 StreamDataSegment::~StreamDataSegment()
00033                 {
00034                 }
00035 
00036                 std::ostream &operator<<(std::ostream &stream, const StreamDataSegment &seg)
00037                 {
00038                         char header = seg._flags;
00039                         header += ((seg._type & 0x0F) << 4);
00040 
00041                         // write the header (1-byte)
00042                         stream.put(header);
00043 
00044                         switch (seg._type)
00045                         {
00046                         case StreamDataSegment::MSG_DATA_SEGMENT:
00047                                 // write the length + data
00048                                 stream << dtn::data::SDNV(seg._value);
00049                                 break;
00050 
00051                         case StreamDataSegment::MSG_ACK_SEGMENT:
00052                                 // write the acknowledged length
00053                                 stream << dtn::data::SDNV(seg._value);
00054                                 break;
00055 
00056                         case StreamDataSegment::MSG_REFUSE_BUNDLE:
00057                                 break;
00058 
00059                         case StreamDataSegment::MSG_KEEPALIVE:
00060                                 break;
00061 
00062                         case StreamDataSegment::MSG_SHUTDOWN:
00063                                 // write the reason (char) + reconnect time (SDNV)
00064                                 stream.put((char)seg._reason);
00065                                 stream << dtn::data::SDNV(seg._value);
00066                                 break;
00067                         }
00068 
00069                         return stream;
00070                 }
00071 
00072                 std::istream &operator>>(std::istream &stream, StreamDataSegment &seg)
00073                 {
00074                         dtn::data::SDNV value;
00075 
00076                         char header = 0;
00077                         stream.get(header);
00078 
00079                         seg._type = StreamDataSegment::SegmentType( (header & 0xF0) >> 4 );
00080                         seg._flags = (header & 0x0F);
00081 
00082                         switch (seg._type)
00083                         {
00084                         case StreamDataSegment::MSG_DATA_SEGMENT:
00085                                 // read the length
00086                                 stream >> value;
00087                                 seg._value = value.getValue();
00088                                 break;
00089 
00090                         case StreamDataSegment::MSG_ACK_SEGMENT:
00091                                 // read the acknowledged length
00092                                 stream >> value;
00093                                 seg._value = value.getValue();
00094                                 break;
00095 
00096                         case StreamDataSegment::MSG_REFUSE_BUNDLE:
00097                                 break;
00098 
00099                         case StreamDataSegment::MSG_KEEPALIVE:
00100                                 break;
00101 
00102                         case StreamDataSegment::MSG_SHUTDOWN:
00103                                 // read the reason (char) + reconnect time (SDNV)
00104                                 char reason;
00105                                 stream.get(reason);
00106                                 seg._reason = StreamDataSegment::ShutdownReason(reason);
00107 
00108                                 stream >> value;
00109                                 seg._value = value.getValue();
00110                                 break;
00111                         }
00112 
00113                         return stream;
00114                 }
00115         }
00116 }