14 #include "inbandbytestream.h"
16 #include "bytestreamdatahandler.h"
18 #include "clientbase.h"
29 static const char* typeValues[] =
31 "open",
"data",
"close"
34 InBandBytestream::IBB::IBB(
const std::string& sid,
int blocksize )
35 : StanzaExtension(
ExtIBB ), m_sid ( sid ), m_seq( 0 ), m_blockSize( blocksize ),
40 InBandBytestream::IBB::IBB(
const std::string& sid,
int seq,
const std::string& data )
41 : StanzaExtension(
ExtIBB ), m_sid ( sid ), m_seq( seq ), m_blockSize( 0 ),
42 m_data( data ), m_type( IBBData )
46 InBandBytestream::IBB::IBB(
const std::string& sid )
47 : StanzaExtension(
ExtIBB ), m_sid ( sid ), m_seq( 0 ), m_blockSize( 0 ),
52 InBandBytestream::IBB::IBB(
const Tag* tag )
53 : StanzaExtension(
ExtIBB ), m_type( IBBInvalid )
58 m_type =
static_cast<IBBType
>( util::lookup(
tag->
name(), typeValues ) );
65 InBandBytestream::IBB::~IBB()
69 const std::string& InBandBytestream::IBB::filterString()
const
71 static const std::string filter =
"/iq/open[@xmlns='" +
XMLNS_IBB +
"']"
73 "|/message/data[@xmlns='" +
XMLNS_IBB +
"']"
78 Tag* InBandBytestream::IBB::tag()
const
80 if( m_type == IBBInvalid )
83 Tag* t =
new Tag( util::lookup( m_type, typeValues ) );
85 t->addAttribute(
"sid", m_sid );
86 if( m_type == IBBData )
89 t->addAttribute(
"seq", m_seq );
91 else if( m_type == IBBOpen )
92 t->addAttribute(
"block-size", m_blockSize );
99 InBandBytestream::InBandBytestream( ClientBase* clientbase, LogSink& logInstance,
const JID& initiator,
100 const JID& target,
const std::string& sid )
101 : Bytestream( Bytestream::IBB, logInstance, initiator, target, sid ),
102 m_clientbase( clientbase ), m_blockSize( 4096 ), m_sequence( -1 ), m_lastChunkReceived( -1 )
106 m_clientbase->registerStanzaExtension(
new IBB() );
107 m_clientbase->registerIqHandler(
this,
ExtIBB );
108 m_clientbase->registerMessageHandler(
this );
137 const std::string&
id = m_clientbase->
getID();
140 m_clientbase->
send( iq,
this, IBBOpen );
154 else if( context == IBBData &&
m_handler )
175 if( i->type() == IBBOpen )
177 returnResult( iq.
from(), iq.
id() );
185 if( i->type() == IBBClose )
187 returnResult( iq.
from(), iq.
id() );
192 if( ++m_lastChunkReceived != i->seq() )
199 if( m_lastChunkReceived == 65535 )
200 m_lastChunkReceived = -1;
202 if( i->data().empty() )
209 returnResult( iq.
from(), iq.
id() );
227 if( m_lastChunkReceived != i->seq() )
233 if( i->data().empty() )
240 m_lastChunkReceived++;
243 void InBandBytestream::returnResult(
const JID& to,
const std::string&
id )
246 m_clientbase->
send( iq );
252 iq.addExtension(
new Error(
type, error ) );
253 m_clientbase->
send( iq );
258 if( !
m_open || !m_clientbase )
262 size_t len = data.length();
265 const std::string&
id = m_clientbase->
getID();
268 m_clientbase->
send( iq,
this, IBBData );
271 if( m_sequence == 65535 )
279 void InBandBytestream::closed()
297 const std::string&
id = m_clientbase->
getID();
300 m_clientbase->
send( iq,
this, IBBClose );