Add stream type prefix to stream name. Added account to client streams. A client stream can now be retrieved by account from the jabber client engine.

git-svn-id: http://yate.null.ro/svn/yate/trunk@2976 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2009-11-27 12:17:17 +00:00
parent 9c55bbd7fd
commit fa5280c020
5 changed files with 89 additions and 43 deletions

View File

@ -1371,8 +1371,36 @@ void JBClientEngine::cleanup(bool final, bool waitTerminate)
TelEngine::destruct(m_process);
}
// Find a stream by account
JBClientStream* JBClientEngine::findAccount(const String& account)
{
if (!account)
return 0;
RefPointer<JBStreamSetList> list;
getStreamList(list,JBStream::c2s);
if (!list)
return 0;
JBClientStream* found = 0;
list->lock();
for (ObjList* o = list->sets().skipNull(); !found && o; o = o->skipNext()) {
JBStreamSet* set = static_cast<JBStreamSet*>(o->get());
for (ObjList* s = set->clients().skipNull(); s; s = s->skipNext()) {
found = static_cast<JBClientStream*>(s->get());
if (account == found->account())
break;
found = 0;
}
}
if (found && !found->ref())
found = 0;
list->unlock();
list = 0;
return found;
}
// Build an outgoing client stream
JBClientStream* JBClientEngine::create(const String& account, const NamedList& params)
JBClientStream* JBClientEngine::create(const String& account, const NamedList& params,
const String& name)
{
if (!account)
return 0;
@ -1385,9 +1413,9 @@ JBClientStream* JBClientEngine::create(const String& account, const NamedList& p
return 0;
}
Lock lock(this);
JBClientStream* stream = static_cast<JBClientStream*>(findStream(account));
JBClientStream* stream = static_cast<JBClientStream*>(findAccount(account));
if (!stream) {
stream = new JBClientStream(this,jid,account,params);
stream = new JBClientStream(this,jid,account,params,name);
stream->ref();
addStream(stream);
}

View File

@ -144,7 +144,7 @@ JBStream::JBStream(JBEngine* engine, Socket* socket, Type t, bool ssl)
{
if (ssl)
setFlags(StreamSecured | StreamTls);
m_engine->buildStreamName(m_name);
m_engine->buildStreamName(m_name,this);
debugName(m_name);
debugChain(m_engine);
Debug(this,DebugAll,"JBStream::JBStream(%p,%p,%s,%s) incoming [%p]",
@ -173,7 +173,7 @@ JBStream::JBStream(JBEngine* engine, Type t, const JabberID& local, const Jabber
m_xmlDom(0), m_socket(0), m_socketFlags(0), m_connectPort(0)
{
if (!m_name)
m_engine->buildStreamName(m_name);
m_engine->buildStreamName(m_name,this);
debugName(m_name);
debugChain(m_engine);
if (params) {
@ -1990,9 +1990,9 @@ JBClientStream::JBClientStream(JBEngine* engine, Socket* socket, bool ssl)
}
JBClientStream::JBClientStream(JBEngine* engine, const JabberID& jid, const String& account,
const NamedList& params)
: JBStream(engine,c2s,jid,jid.domain(),account,&params),
m_userData(0), m_registerReq(0)
const NamedList& params, const char* name)
: JBStream(engine,c2s,jid,jid.domain(),TelEngine::null(name) ? account.c_str() : name,&params),
m_account(account), m_userData(0), m_registerReq(0)
{
m_password = params.getValue("password");
}

View File

@ -803,14 +803,14 @@ public:
* Get the name of a stream state
* @return The name of the stream state
*/
inline const char* stateName()
inline const char* stateName() const
{ return lookup(state(),s_stateName); }
/**
* Get the name of a stream type
* @return The name of the stream type
*/
inline const char* typeName()
inline const char* typeName() const
{ return lookup(type(),s_typeName); }
/**
@ -1222,9 +1222,17 @@ public:
* @param jid User jid
* @param account Account (stream) name
* @param params Stream parameters
* @param name Optional stream name
*/
JBClientStream(JBEngine* engine, const JabberID& jid, const String& account,
const NamedList& params);
const NamedList& params, const char* name = 0);
/**
* Retrieve stream's account
* @return Stream account
*/
inline const String& account() const
{ return m_account; }
/**
* Retrieve stream's user data
@ -1340,6 +1348,7 @@ private:
return id && id->length() == 1 && (*id)[0] == m_registerReq;
}
String m_account; // Stream account
GenObject* m_userData; // User (upper layer) data
String m_password; // The password
String m_newPassword; // New password
@ -1728,8 +1737,9 @@ public:
/**
* Build an internal stream name
* @param name Destination buffer
* @param stream Stream requesting it
*/
virtual void buildStreamName(String& name)
virtual void buildStreamName(String& name, const JBStream* stream)
{}
/**
@ -1906,8 +1916,9 @@ public:
/**
* Build an internal stream name
* @param name Destination buffer
* @param stream Stream requesting it
*/
virtual void buildStreamName(String& name)
virtual void buildStreamName(String& name, const JBStream* stream)
{ name << "stream/" << getStreamIndex(); }
/**
@ -2017,13 +2028,22 @@ public:
*/
virtual void cleanup(bool final = false, bool waitTerminate = true);
/**
* Find a stream by account
* @param account Account name
* @return Referenced JBClientStream pointer or 0
*/
JBClientStream* findAccount(const String& account);
/**
* Build an outgoing client stream
* @param account Account (stream) name
* @param account Account name
* @param params Stream parameters
* @param name Optional stream name
* @return Referenced JBClientStream pointer or 0 if a stream already exists
*/
JBClientStream* create(const String& account, const NamedList& params);
JBClientStream* create(const String& account, const NamedList& params,
const String& name = String::empty());
protected:
/**

View File

@ -216,11 +216,6 @@ class YJBEngine : public JBClientEngine
public:
YJBEngine();
~YJBEngine();
// Find a c2s stream by account
inline JBClientStream* find(const String& name) {
JBStream* s = findStream(name);
return s ? s->clientStream() : 0;
}
// Retrieve stream data from a stream
inline StreamData* streamData(JBClientStream* s)
{ return s ? static_cast<StreamData*>(s->userData()) : 0; }
@ -320,18 +315,18 @@ public:
return module && *module == name();
}
// Build a Message. Complete module, protocol and line parameters
inline Message* message(const char* msg, JBStream* stream = 0) {
inline Message* message(const char* msg, JBClientStream* stream = 0) {
Message* m = new Message(msg);
complete(*m,stream);
return m;
}
// Complete module, protocol and line parameters
inline void complete(Message& m, JBStream* stream = 0) {
inline void complete(Message& m, JBClientStream* stream = 0) {
m.addParam("module",name());
m.addParam("protocol","jabber");
if (stream) {
m.addParam("account",stream->name());
m.addParam("line",stream->name());
m.addParam("account",stream->account());
m.addParam("line",stream->account());
}
}
// Retrieve the line (account) from a message
@ -542,7 +537,7 @@ void YJBEngine::processEvent(JBEvent* ev)
switch (ev->type()) {
case JBEvent::Message:
if (ev->element()) {
Message* m = __plugin.message("msg.execute",ev->stream());
Message* m = __plugin.message("msg.execute",ev->clientStream());
m->addParam("type",ev->stanzaType());
m->addParam("caller",ev->from().bare());
addValidParam(*m,"caller_instance",ev->from().resource());
@ -630,7 +625,7 @@ bool YJBEngine::handleUserRoster(Message& msg, const String& line)
DDebug(this,DebugStub,"handleUserRoster() oper=%s not implemented!",oper->c_str());
return false;
}
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
JabberID contact(msg.getValue("contact"));
@ -685,7 +680,7 @@ bool YJBEngine::handleUserUpdate(Message& msg, const String& line)
String* oper = msg.getParam("operation");
if (TelEngine::null(oper))
return false;
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
bool ok = false;
@ -704,7 +699,7 @@ bool YJBEngine::handleUserUpdate(Message& msg, const String& line)
// Process 'jabber.iq' messages
bool YJBEngine::handleJabberIq(Message& msg, const String& line)
{
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
XmlElement* xml = XMPPUtils::getXml(msg);
@ -716,7 +711,7 @@ bool YJBEngine::handleJabberIq(Message& msg, const String& line)
// Process 'jabber.account' messages
bool YJBEngine::handleJabberAccount(Message& msg, const String& line)
{
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
// Use a while to break to the end
@ -775,7 +770,7 @@ bool YJBEngine::handleResSubscribe(Message& msg, const String& line)
return false;
DDebug(this,DebugAll,"handleResSubscribe() line=%s oper=%s to=%s",
line.c_str(),oper->c_str(),to.c_str());
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
XmlElement* p = XMPPUtils::createPresence(0,to.bare(),
@ -792,7 +787,7 @@ bool YJBEngine::handleResNotify(Message& msg, const String& line)
if (TelEngine::null(oper))
return false;
DDebug(this,DebugAll,"handleResNotify() line=%s oper=%s",line.c_str(),oper->c_str());
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
// Use a while to break to the end
@ -834,7 +829,7 @@ bool YJBEngine::handleResNotify(Message& msg, const String& line)
bool YJBEngine::handleMsgExecute(Message& msg, const String& line)
{
DDebug(this,DebugAll,"handleMsgExecute() line=%s",line.c_str());
JBClientStream* s = find(line);
JBClientStream* s = findAccount(line);
if (!s)
return false;
XmlElement* xml = XMPPUtils::getChatXml(msg);
@ -875,11 +870,12 @@ bool YJBEngine::handleUserLogin(Message& msg, const String& line)
Debug(&__plugin,DebugAll,"handleUserLogin(%s) account=%s",
String::boolText(login),line.c_str());
JBClientStream* stream = s_jabber->find(line);
JBClientStream* stream = s_jabber->findAccount(line);
bool ok = false;
if (login) {
if (!stream) {
stream = s_jabber->create(line,msg);
stream = s_jabber->create(line,msg,
String(::lookup(JBStream::c2s,JBStream::s_typeName)) + "/" + line);
if (stream) {
// Build user data and set it
Lock lock(stream);
@ -939,7 +935,7 @@ void YJBEngine::processPresenceStanza(JBEvent* ev)
sdata->removeResource(ev->from().bare(),ev->from().resource());
lock.drop();
// Notify
Message* m = __plugin.message("resource.notify",ev->stream());
Message* m = __plugin.message("resource.notify",ev->clientStream());
m->addParam("operation",online ? "online" : "offline");
m->addParam("contact",ev->from().bare());
if (ev->from().resource())
@ -965,7 +961,7 @@ void YJBEngine::processPresenceStanza(JBEvent* ev)
}
bool subReq = (pres == XMPPUtils::Subscribe);
if (subReq || pres == XMPPUtils::Unsubscribe) {
Message* m = __plugin.message("resource.subscribe",ev->stream());
Message* m = __plugin.message("resource.subscribe",ev->clientStream());
m->addParam("operation",ev->stanzaType());
m->addParam("subscriber",ev->from().bare());
Engine::enqueue(m);
@ -1024,7 +1020,7 @@ void YJBEngine::processIqStanza(JBEvent* ev)
}
// Route the iq
Message m("jabber.iq");
__plugin.complete(m,ev->stream());
__plugin.complete(m,ev->clientStream());
m.addParam("from",ev->from().bare());
m.addParam("from_instance",ev->from().resource());
m.addParam("to",ev->to().bare());
@ -1076,7 +1072,7 @@ void YJBEngine::processStreamEvent(JBEvent* ev, bool ok)
sendPresence(ev->stream(),true,pres);
}
Message* m = __plugin.message("user.notify",ev->stream());
Message* m = __plugin.message("user.notify",ev->clientStream());
m->addParam("username",ev->stream()->local().node());
m->addParam("server",ev->stream()->local().domain());
m->addParam("jid",ev->stream()->local());
@ -1170,7 +1166,7 @@ void YJBEngine::processRoster(JBEvent* ev, XmlElement* service, int tag, int iqT
String* jid = x->getAttribute("jid");
if (TelEngine::null(jid))
return;
Message* m = __plugin.message("user.roster",ev->stream());
Message* m = __plugin.message("user.roster",ev->clientStream());
String* sub = x->getAttribute("subscription");
bool upd = !sub || *sub != "remove";
ev->stream()->lock();
@ -1198,7 +1194,7 @@ void YJBEngine::processRoster(JBEvent* ev, XmlElement* service, int tag, int iqT
// Handle 'query' responses
if (!service || tag != XmlTag::Query || ev->id() != s_rosterQueryId)
return;
Message* m = __plugin.message("user.roster",ev->stream());
Message* m = __plugin.message("user.roster",ev->clientStream());
m->addParam("operation","update");
NamedString* count = new NamedString("contact.count");
m->addParam(count);

View File

@ -163,7 +163,7 @@ public:
// Process events
virtual void processEvent(JBEvent* ev);
// Build an internal stream name from node name and stream index
virtual void buildStreamName(String& name);
virtual void buildStreamName(String& name, const JBStream* stream);
// Start stream TLS
virtual void encryptStream(JBStream* stream);
// Connect an outgoing stream
@ -949,10 +949,12 @@ void YJBEngine::processEvent(JBEvent* ev)
}
// Build an internal stream name from node name and stream index
void YJBEngine::buildStreamName(String& name)
void YJBEngine::buildStreamName(String& name, const JBStream* stream)
{
// name << Engine::nodeName() << "/";
JBServerEngine::buildStreamName(name);
JBServerEngine::buildStreamName(name,stream);
if (stream)
name = String(stream->typeName()) + "/" + name;
}
// Start stream TLS