• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eXile 的專欄

            用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室

                聊天室是一種典型的網(wǎng)絡(luò)應(yīng)用,這個(gè)程序演示了ICE框架的基本結(jié)構(gòu)。

            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);
            };

            };
            定義兩個(gè)接口,Room用于服務(wù)器端實(shí)現(xiàn),MessageReceiver用于客戶端實(shí)現(xiàn)。
            使用slice2cpp生成C++文件: chat.h, chat.cpp.

            2)服務(wù)器端實(shí)現(xiàn)。
            #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);
            }




            3)客戶端實(shí)現(xiàn)。
            #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);
            }



            posted on 2009-01-04 01:32 eXile 閱讀(5151) 評(píng)論(7)  編輯 收藏 引用 所屬分類: C/C++代碼片段網(wǎng)絡(luò)開發(fā)ICE

            評(píng)論

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2009-01-04 12:17 cppfan

            ICE就是屏蔽了底層細(xì)節(jié),開發(fā)者著力于功能和邏輯上,對(duì)嗎?  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2009-01-04 14:21 eXile

            @cppfan
            是的,你說的太對(duì)了.  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2009-01-04 14:48 haskell

            這東西還活著嗎?  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2009-01-05 09:55 elan.gch

            @haskell 活著, 活的好好的  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2009-01-06 08:35 guest

            @haskell
            ICE一直在改進(jìn),現(xiàn)在挺不錯(cuò)的。不過這個(gè)庫缺少大公司去支持,宣傳~~是個(gè)遺憾。  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2011-07-12 14:23 hanfei_007

            不知樓主是否有此程序服務(wù)器端的Java源代碼~~~Java入手ICE,有點(diǎn)暈呀~~  回復(fù)  更多評(píng)論   

            # re: 用ICE實(shí)現(xiàn)一個(gè)簡(jiǎn)單的聊天室 2016-01-20 22:55 sy

            find() end()等函數(shù),是在哪里寫的呢,功能是什么  回復(fù)  更多評(píng)論   

            導(dǎo)航

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            統(tǒng)計(jì)

            常用鏈接

            留言簿(18)

            隨筆分類

            隨筆檔案

            服務(wù)器編程

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            99久久99久久精品国产片果冻| 精品精品国产自在久久高清| 亚洲国产精品综合久久一线| 久久精品国产亚洲AV不卡| 精品国产乱码久久久久久郑州公司 | 亚洲人成无码www久久久| 狠狠色婷婷久久综合频道日韩| 99久久国语露脸精品国产| 久久久久久国产精品无码下载| 区久久AAA片69亚洲| 久久无码av三级| 影音先锋女人AV鲁色资源网久久| 久久综合中文字幕| 久久精品人人做人人爽电影蜜月| 国产福利电影一区二区三区久久老子无码午夜伦不 | 亚洲精品高清一二区久久| 人妻精品久久无码区| 久久一区二区三区免费| 久久99精品国产麻豆宅宅| 欧美黑人激情性久久| 久久一本综合| 精品免费久久久久国产一区| 久久久久人妻一区精品性色av| 伊人久久无码精品中文字幕| 国产精品gz久久久| 国产精品久久波多野结衣| 久久久久亚洲精品日久生情| 天天综合久久一二三区| 久久久久一本毛久久久| 99热精品久久只有精品| 久久99精品国产99久久6男男| 无码人妻精品一区二区三区久久久| 欧美色综合久久久久久| 久久久久久毛片免费看| 国产ww久久久久久久久久| 99国产欧美精品久久久蜜芽| 久久夜色精品国产噜噜噜亚洲AV| 亚洲国产婷婷香蕉久久久久久| 狠狠精品久久久无码中文字幕| 国产精品熟女福利久久AV| 99久久伊人精品综合观看|