⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 一个icestorm的例子.ice是一个开源的中间件,性能好,易使用
💻 CPP
字号:
// **********************************************************************//// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.//// This copy of Ice is licensed to you under the terms described in the// ICE_LICENSE file included in this distribution.//// **********************************************************************#include <Ice/Application.h>#include <IceUtil/Thread.h>#include <Glacier2/Router.h>#include <Chat.h>using namespace std;using namespace Ice;using namespace Chat;class SessionPingThread : public IceUtil::Thread, public IceUtil::Monitor<IceUtil::Mutex>{public:    SessionPingThread(const Glacier2::SessionPrx& session) :	_session(session),	_timeout(IceUtil::Time::seconds(20)),	_destroy(false)    {    }    virtual void    run()    {	Lock sync(*this);	while(!_destroy)	{	    timedWait(_timeout);	    if(_destroy)	    {		break;	    }	    try	    {		_session->ice_ping();	    }	    catch(const Ice::Exception&)	    {		break;	    }	}    }    void    destroy()    {	Lock sync(*this);	_destroy = true;	notify();    }private:    const Glacier2::SessionPrx _session;    const IceUtil::Time _timeout;    bool _destroy;};typedef IceUtil::Handle<SessionPingThread> SessionPingThreadPtr;class ChatRoomObserverI : public ChatRoomObserver{public:    ChatRoomObserverI(const string& room) :	_room(room)    {    }    virtual void    message(const string& data, const Current&)    {	cout << _room << ": " << data << endl;    }private:    const string _room;};class ChatRoomManager{public:    ChatRoomManager(const ObjectAdapterPtr& adapter, const ChatSessionPrx& session, const string& category) :	_adapter(adapter),	_session(session),	_category(category)    {    }    ChatRoomParticipantPrx    find(const string& room) const    {	map<string, ChatRoomParticipantPrx>::const_iterator p = _rooms.find(room);	ChatRoomParticipantPrx r;	if(p != _rooms.end())	{	    r = p->second;	}	return r;    }    void    join(const string& roomName)    {	Identity id;	id.name = "observer." + roomName;	id.category = _category;	ChatRoomObserverPrx observer = ChatRoomObserverPrx::uncheckedCast(	    _adapter->add(new ChatRoomObserverI(roomName), id));	ChatRoomParticipantPrx room = _session->join(roomName, observer);	_rooms.insert(make_pair(roomName, room));    }private:    const ObjectAdapterPtr _adapter;    const ChatSessionPrx _session;    map<string, ChatRoomParticipantPrx> _rooms;    const string _category;};class ChatClient : public Application{public:    virtual int    run(int argc, char* argv[])    {	RouterPrx defaultRouter = communicator()->getDefaultRouter();	if(!defaultRouter)	{	    cerr << argv[0] << ": no default router set" << endl;	    return EXIT_FAILURE;	}	Glacier2::RouterPrx router = Glacier2::RouterPrx::checkedCast(defaultRouter);	if(!router)	{	    cerr << argv[0] << ": configured router is not a Glacier2 router" << endl;	    return EXIT_FAILURE;	}	ChatSessionPrx session;	while(true)	{	    cout << "This demo accepts any user-id / password combination.\n";	    string id;	    cout << "user id: " << flush;	    std::getline(cin, id);	    id = trim(id);	    string pw;	    cout << "password: " << flush;	    std::getline(cin, pw);	    pw = trim(pw);	    try	    {		session = ChatSessionPrx::uncheckedCast(router->createSession(id, pw));		break;	    }	    catch(const Glacier2::PermissionDeniedException& ex)	    {		cout << "permission denied:\n" << ex.reason << endl;	    }	}    	SessionPingThreadPtr ping = new SessionPingThread(session);	ping->start();	ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Chat.Client");	adapter->activate();	ChatRoomManager manager(adapter, session, router->getServerProxy()->ice_getIdentity().category);	manager.join("global");	menu();	try	{	    do	    {		string s;		cout << "==> ";		getline(cin, s);		s = trim(s);		if(s.empty())		{		    continue;		}		if(s[0] == '/')		{		    vector<string> tok;		    stringstream ss(s);		    string buf;		    while(ss >> buf)		    {			tok.push_back(buf);		    }		    assert(tok.size() > 0);		    if(tok[0] == "/quit")		    {			break;		    }		    else if(tok[0] == "/join")		    {			if(tok.size() != 2)			{ 			    menu();			    continue;			}			string roomName = tok[1];			if(manager.find(roomName))			{			    cerr << "Room already exists." << endl;			    continue;			}			manager.join(roomName);		    }		    else		    {			ChatRoomParticipantPrx room = manager.find(tok[0].substr(1));			if(room)			{			    room->say(s);			}			else			{			    menu();			}		    }		}		else		{		    ChatRoomParticipantPrx room = manager.find("global");		    assert(room);		    room->say(s);		}	    }	    while(cin.good());	    router->destroySession();	}	catch(const Exception& ex)	{	    cerr << ex << endl;	    ping->destroy();	    ping->getThreadControl().join();	    return EXIT_FAILURE;	}	ping->destroy();	ping->getThreadControl().join();	return EXIT_SUCCESS;    }private:    void    menu()    {	cout << "commands\n"	     << "/quit		    Exit\n"	     << "/join <room>	    Join room\n"	     << "/<room>	    Talk in room\n"	     << "<text>		    Talk in global"	     << endl;    }    string    trim(const string& s)    {	static const string delims = "\t\r\n ";	string::size_type last = s.find_last_not_of(delims);	if(last != string::npos)	{	    return s.substr(s.find_first_not_of(delims), last+1);	}	return s;    }};intmain(int argc, char* argv[]){    ChatClient app;    return app.main(argc, argv, "config");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -