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 )
42 m_data( data ), m_type( IBBData )
46 InBandBytestream::IBB::IBB(
const std::string& sid )
52 InBandBytestream::IBB::IBB(
const Tag*
tag )
58 m_type = (IBBType)util::lookup( tag->name(), typeValues );
59 m_blockSize = atoi( tag->findAttribute(
"block-size" ).c_str() );
60 m_seq = atoi( tag->findAttribute(
"seq" ).c_str() );
61 m_sid = tag->findAttribute(
"sid" );
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 );
123 m_clientbase->removeMessageHandler(
this );
124 m_clientbase->removeIqHandler(
this,
ExtIBB );
125 m_clientbase->removeIDHandler(
this );
134 if( m_target == m_clientbase->jid() )
137 const std::string&
id = m_clientbase->getID();
140 m_clientbase->send( iq,
this, IBBOpen );
149 if( context == IBBOpen && m_handler )
151 m_handler->handleBytestreamOpen(
this );
166 if( !i || !m_handler || iq.
subtype() !=
IQ::Set || i->sid() != this->sid() )
171 if( i->type() == IBBOpen )
173 returnResult( iq.
from(), iq.
id() );
175 m_handler->handleBytestreamOpen(
this );
181 if( i->type() == IBBClose )
183 returnResult( iq.
from(), iq.
id() );
188 if( ( m_lastChunkReceived + 1 ) != i->seq() )
195 if( i->data().empty() )
202 returnResult( iq.
from(), iq.
id() );
203 m_handler->handleBytestreamData(
this, i->data() );
204 m_lastChunkReceived++;
210 if( msg.
from() != m_target || !m_handler )
220 if( m_lastChunkReceived != i->seq() )
226 if( i->data().empty() )
232 m_handler->handleBytestreamData(
this, i->data() );
233 m_lastChunkReceived++;
236 void InBandBytestream::returnResult(
const JID& to,
const std::string&
id )
239 m_clientbase->send( iq );
246 m_clientbase->send( iq );
251 if( !m_open || !m_clientbase )
255 size_t len = data.length();
258 const std::string&
id = m_clientbase->getID();
259 IQ iq(
IQ::Set, m_clientbase->jid() == m_target ? m_initiator : m_target, id );
260 iq.
addExtension(
new IBB( m_sid, ++m_sequence, data.substr( pos, m_blockSize ) ) );
261 m_clientbase->send( iq,
this, IBBData );
264 if( m_sequence == 65535 )
272 void InBandBytestream::closed()
280 m_handler->handleBytestreamClose(
this );
290 const std::string&
id = m_clientbase->getID();
293 m_clientbase->send( iq,
this, IBBClose );
296 m_handler->handleBytestreamClose(
this );
virtual ~InBandBytestream()
const StanzaExtension * findExtension(int type) const
An abstraction of a message session between any two entities.
An abstraction of an IQ stanza.
void addExtension(const StanzaExtension *se)
const std::string XMLNS_IBB
virtual void handleIqID(const IQ &iq, int context)
A stanza error abstraction implemented as a StanzaExtension.
const std::string decode64(const std::string &input)
bool send(const std::string &data)
An abstraction of a message stanza.
The namespace for the gloox library.
const std::string encode64(const std::string &input)
StanzaExtension(int type)
virtual void handleMessage(const Message &msg, MessageSession *session=0)
const std::string & id() const
virtual bool handleIq(const IQ &iq)