summaryrefslogtreecommitdiff
path: root/cpp/demo/Glacier2/chat/ChatSessionI.cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2005-04-06 04:41:19 +0000
committerMatthew Newhook <matthew@zeroc.com>2005-04-06 04:41:19 +0000
commitb53c71eeee1b25030a1a0dd45c9d1ebcfec5b2e0 (patch)
treee3c4647c0a469a6a3330ce9cac5dc62b189529a5 /cpp/demo/Glacier2/chat/ChatSessionI.cpp
parentfix for bug 204 (diff)
downloadice-b53c71eeee1b25030a1a0dd45c9d1ebcfec5b2e0.tar.bz2
ice-b53c71eeee1b25030a1a0dd45c9d1ebcfec5b2e0.tar.xz
ice-b53c71eeee1b25030a1a0dd45c9d1ebcfec5b2e0.zip
merge with 2.1 branch.
Diffstat (limited to 'cpp/demo/Glacier2/chat/ChatSessionI.cpp')
-rwxr-xr-xcpp/demo/Glacier2/chat/ChatSessionI.cpp101
1 files changed, 63 insertions, 38 deletions
diff --git a/cpp/demo/Glacier2/chat/ChatSessionI.cpp b/cpp/demo/Glacier2/chat/ChatSessionI.cpp
index 8c76e6e77cc..22e703885c4 100755
--- a/cpp/demo/Glacier2/chat/ChatSessionI.cpp
+++ b/cpp/demo/Glacier2/chat/ChatSessionI.cpp
@@ -10,97 +10,122 @@
#include <Ice/Ice.h>
#include <ChatSessionI.h>
+#include <list>
+
using namespace std;
+using namespace Ice;
using namespace Demo;
-ChatRoomMembers::ChatRoomMembers()
+class ChatRoom;
+typedef IceUtil::Handle<ChatRoom> ChatRoomPtr;
+
+class ChatRoom : public IceUtil::Mutex, public IceUtil::Shared
+{
+public:
+
+ static ChatRoomPtr& instance();
+
+ void enter(const Demo::ChatCallbackPrx&);
+ void leave(const Demo::ChatCallbackPrx&);
+ void message(const string&) const;
+
+private:
+
+ list<Demo::ChatCallbackPrx> _members;
+
+ static ChatRoomPtr _instance;
+ static IceUtil::StaticMutex _instanceMutex;
+};
+
+ChatRoomPtr ChatRoom::_instance;
+IceUtil::StaticMutex ChatRoom::_instanceMutex = ICE_STATIC_MUTEX_INITIALIZER;
+
+ChatRoomPtr&
+ChatRoom::instance()
{
+ IceUtil::StaticMutex::Lock sync(_instanceMutex);
+ if(!_instance)
+ {
+ _instance = new ChatRoom;
+ }
+
+ return _instance;
}
void
-ChatRoomMembers::add(const ChatCallbackPrx& callback)
+ChatRoom::enter(const ChatCallbackPrx& callback)
{
IceUtil::Mutex::Lock sync(*this);
- _members.push_back(callback);
+ _members.push_back(ChatCallbackPrx::uncheckedCast(callback->ice_oneway()));
}
void
-ChatRoomMembers::remove(const ChatCallbackPrx& callback)
+ChatRoom::leave(const ChatCallbackPrx& callback)
{
IceUtil::Mutex::Lock sync(*this);
- list<ChatCallbackPrx>::iterator p = _members.begin();
- while(p != _members.end())
+ list<ChatCallbackPrx>::iterator p;
+ for(p = _members.begin(); p != _members.end(); ++p)
{
- if(Ice::proxyIdentityEqual(callback, *p))
+ if(proxyIdentityEqual(callback, *p))
{
break;
}
- ++p;
- }
- if(p != _members.end())
- {
- _members.erase(p);
}
+
+ assert(p != _members.end());
+ _members.erase(p);
}
void
-ChatRoomMembers::message(const string& data)
+ChatRoom::message(const string& data) const
{
IceUtil::Mutex::Lock sync(*this);
- list<ChatCallbackPrx>::iterator p = _members.begin();
- while(p != _members.end())
+ for(list<ChatCallbackPrx>::const_iterator p = _members.begin(); p != _members.end(); ++p)
{
try
{
(*p)->message(data);
}
- catch(const Ice::LocalException&)
+ catch(const LocalException&)
{
- p = _members.erase(p);
- continue;
}
- ++p;
}
}
-ChatSessionI::ChatSessionI(const ChatRoomMembersPtr& members, const string& userId) :
- _members(members),
- _userId(userId),
- _destroy(false)
+ChatSessionI::ChatSessionI(const string& userId) :
+ _userId(userId)
{
}
void
-ChatSessionI::setCallback(const ChatCallbackPrx& callback, const Ice::Current& current)
+ChatSessionI::setCallback(const ChatCallbackPrx& callback, const Current& current)
{
IceUtil::Mutex::Lock sync(*this);
if(!_callback)
{
_callback = callback;
- _members->message(_userId + " has entered the chat room.");
- _members->add(callback);
+ ChatRoomPtr chatRoom = ChatRoom::instance();
+ chatRoom->message(_userId + " has entered the chat room.");
+ chatRoom->enter(callback);
}
}
void
-ChatSessionI::say(const string& data, const Ice::Current&)
+ChatSessionI::say(const string& data, const Current&)
{
- _members->message(_userId + " says: " + data);
+ ChatRoom::instance()->message(_userId + " says: " + data);
}
void
-ChatSessionI::destroy(const Ice::Current& current)
+ChatSessionI::destroy(const Current& current)
{
IceUtil::Mutex::Lock sync(*this);
- if(!_destroy)
+ if(_callback)
{
- _destroy = true;
- if(_callback)
- {
- _members->remove(_callback);
- _callback = 0;
- _members->message(_userId + " has left the chat room.");
- }
- current.adapter->remove(current.id);
+ ChatRoomPtr chatRoom = ChatRoom::instance();
+ chatRoom->leave(_callback);
+ _callback = 0;
+ chatRoom->message(_userId + " has left the chat room.");
}
+ current.adapter->remove(current.id);
}