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 = (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 );
137 const std::string&
id = m_clientbase->
getID();
140 m_clientbase->
send( iq,
this, IBBOpen );
171 if( i->type() == IBBOpen )
173 returnResult( iq.
from(), iq.
id() );
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() );
204 m_lastChunkReceived++;
220 if( m_lastChunkReceived != i->seq() )
226 if( i->data().empty() )
233 m_lastChunkReceived++;
236 void InBandBytestream::returnResult(
const JID& to,
const std::string&
id )
239 m_clientbase->
send( iq );
245 iq.addExtension(
new Error( type, error ) );
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();
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()
290 const std::string&
id = m_clientbase->
getID();
293 m_clientbase->
send( iq,
this, IBBClose );
virtual ~InBandBytestream()
const std::string & sid() const
void removeIDHandler(IqHandler *ih)
An abstraction of a message session between any two entities.
An abstraction of an IQ stanza.
void removeIqHandler(IqHandler *ih, int exttype)
void addExtension(const StanzaExtension *se)
BytestreamDataHandler * m_handler
const std::string XMLNS_IBB
virtual void handleIqID(const IQ &iq, int context)
virtual void handleBytestreamData(Bytestream *bs, const std::string &data)=0
const std::string decode64(const std::string &input)
bool send(const std::string &data)
An abstraction of a message stanza.
void removeMessageHandler(MessageHandler *mh)
The namespace for the gloox library.
const std::string encode64(const std::string &input)
const std::string getID()
virtual void handleMessage(const Message &msg, MessageSession *session=0)
virtual void handleBytestreamClose(Bytestream *bs)=0
virtual void handleBytestreamOpen(Bytestream *bs)=0
virtual bool handleIq(const IQ &iq)
const std::string & id() const
const StanzaExtension * findExtension(int type) const