聊天室是一种典型的网络应用,这个程序演示了ICE框架的基本结构。
来源:互联网 发布:唱歌学英语软件 编辑:程序博客网 时间:2024/05/17 23:50
启动Server命令行
chatserver --Ice.Config=config.chatserver
带路由的client启动命令如下:
glacier2router --Ice.Config=/QTOA/ChatDemo-1.4.0/config/nfig.glacier2router
./chatgl2client --Ice.Config=/QTOA/ChatDemo-1.4.0/config/config.chatgl2client
不带路由的Poll方式启动client命令如下:
./chatpollclient --Ice.Config=/QTOA/ChatDemo-1.4.0/config/config.chatpollclient
1)定义SLICE接口。
module Chat {
interface MessageReceiver {
void onMessage(string sender, string msg);
};
dictionary<string, MessageReceiver*> UserList;
interface Room {
bool login(string user, MessageReceiver* receiver);
void logout(string user);
void sendMessage(string user, string message);
};
};
定义两个接口,Room用于服务器端实现,MessageReceiver用于客户端实现。interface MessageReceiver {
void onMessage(string sender, string msg);
};
dictionary<string, MessageReceiver*> UserList;
interface Room {
bool login(string user, MessageReceiver* receiver);
void logout(string user);
void sendMessage(string user, string message);
};
};
使用slice2cpp生成C++文件: chat.h, chat.cpp.
2)服务器端实现。
#include <iostream>
#include <Ice/Ice.h>
#include "chat.h"
using std::string;
class ChatRoomI : public Chat::Room {
public:
virtual bool login(const string&, const Chat::MessageReceiverPrx&, const Ice::Current&);
virtual void logout(const string&, const Ice::Current&);
virtual void sendMessage(const string&, const string&, const Ice::Current&);
private:
void broadcast(const string& user, const string& message);
bool notify(const Chat::MessageReceiverPrx& receiver, const string& sender, const string& message);
Chat::UserList users_;
};
bool ChatRoomI::login(const string& user, const Chat::MessageReceiverPrx& receiver, const Ice::Current&) {
if (users_.find(user) != users_.end()) {
return false;
}
users_.insert(Chat::UserList::value_type(user, receiver));
broadcast(user, "---login---");
return true;
}
void ChatRoomI::logout(const string& user, const Ice::Current&) {
users_.erase(user);
broadcast(user, "===logout===");
}
void ChatRoomI::sendMessage(const string& user, const string& message, const Ice::Current&) {
broadcast(user, message);
}
void ChatRoomI::broadcast(const string& user, const string& message) {
Chat::UserList::iterator it = users_.begin(), end = users_.end();
while (it != end) {
if (user != it->first && !notify(it->second, it->first, message))
users_.erase(it++);
else
++it;
}
}
bool ChatRoomI::notify(const Chat::MessageReceiverPrx& receiver, const string& sender, const string& message) {
bool ret = true;
try {
receiver->onMessage(sender, message);
} catch(const std::exception& ex) {
ret = false;
}
return ret;
}
class Server : public Ice::Application {
public:
virtual int run(int argc, char* argv[]) {
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
"Chat.RoomAdapter", "default -p 10000");
Chat::RoomPtr room = new ChatRoomI;
adapter->add(room, communicator()->stringToIdentity("Chat.Room"));
adapter->activate();
communicator()->waitForShutdown();
return 0;
}
};
int main(int argc, char* argv[]) {
Server app;
return app.main(argc, argv);
}
#include <Ice/Ice.h>
#include "chat.h"
using std::string;
class ChatRoomI : public Chat::Room {
public:
virtual bool login(const string&, const Chat::MessageReceiverPrx&, const Ice::Current&);
virtual void logout(const string&, const Ice::Current&);
virtual void sendMessage(const string&, const string&, const Ice::Current&);
private:
void broadcast(const string& user, const string& message);
bool notify(const Chat::MessageReceiverPrx& receiver, const string& sender, const string& message);
Chat::UserList users_;
};
bool ChatRoomI::login(const string& user, const Chat::MessageReceiverPrx& receiver, const Ice::Current&) {
if (users_.find(user) != users_.end()) {
return false;
}
users_.insert(Chat::UserList::value_type(user, receiver));
broadcast(user, "---login---");
return true;
}
void ChatRoomI::logout(const string& user, const Ice::Current&) {
users_.erase(user);
broadcast(user, "===logout===");
}
void ChatRoomI::sendMessage(const string& user, const string& message, const Ice::Current&) {
broadcast(user, message);
}
void ChatRoomI::broadcast(const string& user, const string& message) {
Chat::UserList::iterator it = users_.begin(), end = users_.end();
while (it != end) {
if (user != it->first && !notify(it->second, it->first, message))
users_.erase(it++);
else
++it;
}
}
bool ChatRoomI::notify(const Chat::MessageReceiverPrx& receiver, const string& sender, const string& message) {
bool ret = true;
try {
receiver->onMessage(sender, message);
} catch(const std::exception& ex) {
ret = false;
}
return ret;
}
class Server : public Ice::Application {
public:
virtual int run(int argc, char* argv[]) {
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
"Chat.RoomAdapter", "default -p 10000");
Chat::RoomPtr room = new ChatRoomI;
adapter->add(room, communicator()->stringToIdentity("Chat.Room"));
adapter->activate();
communicator()->waitForShutdown();
return 0;
}
};
int main(int argc, char* argv[]) {
Server app;
return app.main(argc, argv);
}
3)客户端实现。
#include <iostream>
#include <Ice/Ice.h>
#include "chat.h"
using std::string;
class ChatUserI : public Chat::MessageReceiver {
public:
virtual void onMessage(const string& user, const string& message, const Ice::Current&) {
std::cout << user << " : " << message << std::endl;
}
};
class Client : public Ice::Application {
public:
virtual int run(int argc, char* argv[]) {
Chat::RoomPrx chatRoom = Chat::RoomPrx::checkedCast(
communicator()->stringToProxy("Chat.Room:default -p 10000"));
if (!chatRoom) {
std::cout << "Proxy not existed!/n";
return -1;
}
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
"Chat.UserAdapter", "default");
Chat::MessageReceiverPtr user = new ChatUserI;
adapter->add(user, communicator()->stringToIdentity("Chat.User"));
adapter->activate();
Chat::MessageReceiverPrx userPrx = Chat::MessageReceiverPrx::uncheckedCast(
adapter->createProxy(communicator()->stringToIdentity("Chat.User")));
string name;
std::cout << "Please input user name: ";
std::cin >> name;
if (!chatRoom->login(name, userPrx)) {
std::cout << "login error: User Name already exist!/n";
return 0;
}
std::cout << "login OK!" << std::endl;
string message;
while (std::getline(std::cin, message) && message != "quit") {
chatRoom->sendMessage(name, message);
}
chatRoom->logout(name);
return 0;
}
};
int main(int argc, char* argv[]) {
Client app;
return app.main(argc, argv);
}
#include <Ice/Ice.h>
#include "chat.h"
using std::string;
class ChatUserI : public Chat::MessageReceiver {
public:
virtual void onMessage(const string& user, const string& message, const Ice::Current&) {
std::cout << user << " : " << message << std::endl;
}
};
class Client : public Ice::Application {
public:
virtual int run(int argc, char* argv[]) {
Chat::RoomPrx chatRoom = Chat::RoomPrx::checkedCast(
communicator()->stringToProxy("Chat.Room:default -p 10000"));
if (!chatRoom) {
std::cout << "Proxy not existed!/n";
return -1;
}
Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints(
"Chat.UserAdapter", "default");
Chat::MessageReceiverPtr user = new ChatUserI;
adapter->add(user, communicator()->stringToIdentity("Chat.User"));
adapter->activate();
Chat::MessageReceiverPrx userPrx = Chat::MessageReceiverPrx::uncheckedCast(
adapter->createProxy(communicator()->stringToIdentity("Chat.User")));
string name;
std::cout << "Please input user name: ";
std::cin >> name;
if (!chatRoom->login(name, userPrx)) {
std::cout << "login error: User Name already exist!/n";
return 0;
}
std::cout << "login OK!" << std::endl;
string message;
while (std::getline(std::cin, message) && message != "quit") {
chatRoom->sendMessage(name, message);
}
chatRoom->logout(name);
return 0;
}
};
int main(int argc, char* argv[]) {
Client app;
return app.main(argc, argv);
}
- 聊天室是一种典型的网络应用,这个程序演示了ICE框架的基本结构。
- ICE:实现简单的聊天室
- Structs是典型的MVC框架实现
- 用ICE实现一个简单的聊天室
- 用ICE实现一个简单的聊天室
- Qt状态机框架的一个典型应用
- 进行命令处理的典型程序框架
- 一个简单的聊天室的实现----ICE应用系列文章之四
- 程序的基本结构
- 程序的基本结构
- 程序的基本结构
- linux网络编程十六:I/O复用的应用-poll简单实现聊天室程序
- ICE的应用
- 一种新网络的崛起:应用网络
- [分享]面向中大型应用的PHPWeb开发框架Ice
- 典型的三层结构
- 简单的网络聊天室
- 简单的网络聊天室
- HDU 1007 Quoit Design(计算几何 平面最近点对)
- 用windows远程连接linux桌面(使用tightvnc或者tigervnc)
- 万用表测量二极管
- 面试题:求二叉树中节点的最大距离
- POJ 1046 Color Me Less(浅水)
- 聊天室是一种典型的网络应用,这个程序演示了ICE框架的基本结构。
- 求序列逆序数
- Linux 防火墙
- 利用reloadRowsAtIndexPaths:withRowAnimation:来动态改变cell的高度
- 迭代式MapReduce框架介绍
- ssh前台同时迭代两个list
- Excel2010创建包含数据有效性的xls文件,再打开时有效性变无效
- <<How to setup an iSCSI target using tgtadm>>
- Class(java.lang.Class)类中成员函数的区别