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

📄 chat.cpp

📁 pRTI chatroom源代码及程序打包
💻 CPP
字号:
// Chat.cpp : Defines the entry point for the console application.//#include "StdAfx.h"#include <iostream>#include <sstream>#ifdef _WIN32#include <io.h>#include <fcntl.h>#endif#include "BaseFederateAmbassador.h"#include "LogicalTimeDouble.h"using namespace std;using namespace RTI;class ChatCCFederate : public BaseFederateAmbassador {    private:        //wstring host;        //wstring port;        auto_ptr< RTIambassador > _rtiAmbassador;        InteractionClassHandle _communicationId;        ParameterHandle _parameterIdMessage;        ParameterHandle _parameterIdSender;        ObjectClassHandle _participantId;        AttributeHandle _attributeIdName;        LogicalTimeDoubleFactory _myTimeFactory;        LogicalTimeIntervalFactoryDouble _myIntervalFactory;                public:            ChatCCFederate(wstring host, wstring port) {                RTIambassadorFactory* rtiAmbassadorFactory = new RTIambassadorFactory();                vector< wstring > args;                                args.push_back(wstring(L"crcHost = ").append(host));                args.push_back(wstring(L"crcPort = ").append(port));                _rtiAmbassador = rtiAmbassadorFactory->createRTIambassador(args);            }                        ~ChatCCFederate() throw() {            }                        void run() {                try {                    try {                        _rtiAmbassador->createFederationExecution(L"ChatRoom", L"Chat.xml");                    } catch (FederationExecutionAlreadyExists&) {                    }                                        FederateHandle federateHandle = _rtiAmbassador->joinFederationExecution(                    L"Chat",                    L"ChatRoom",                    *this,                    _myTimeFactory,                    _myIntervalFactory);                                        // Publish subscribe to interaction classes                    _communicationId = _rtiAmbassador->getInteractionClassHandle(L"Communication");                    _parameterIdMessage = _rtiAmbassador->getParameterHandle(_communicationId, L"Message");                    _parameterIdSender = _rtiAmbassador->getParameterHandle(_communicationId, L"Sender");                                        _rtiAmbassador->subscribeInteractionClass(_communicationId);                    _rtiAmbassador->publishInteractionClass(_communicationId);                                        // Publish subscribe to object classes                    _participantId = _rtiAmbassador->getObjectClassHandle(L"Participant");                    _attributeIdName = _rtiAmbassador->getAttributeHandle(_participantId, L"Name");                                        AttributeHandleSet _theAttributes;                    _theAttributes.insert(_attributeIdName);                                        _rtiAmbassador->subscribeObjectClassAttributes(_participantId, _theAttributes);                    _rtiAmbassador->publishObjectClassAttributes(_participantId, _theAttributes);                                        #ifdef _WIN32         // Make sure that stdin is in text mode. Initialization of the         // Java Virtual Machine (done by pRTI) has a side-effect which         // is to set stdin to binary mode.         _setmode(_fileno(stdin), _O_TEXT);#endifchar _username[32];cout << "Enter your name: ";cout.flush();cin.getline(_username, sizeof(_username), '\n');if(cin.fail()) {    cin.clear();    cin.ignore(128, '\n');}cout << "Type messages you want to send. To exit, type . <ENTER>" << endl;while (true) {    cout << "> ";    cout.flush();    char _message[256];    cin.getline(_message, sizeof(_message)); // '\n');        if (cin.fail()) {        cout << "Too long message, maximum 255 characters.\n";        cin.clear();        cin.ignore(512, '\n'); // Empty the input stream of old garbage.        continue;    }        if (!strcmp(_message, ".")) {        break;    }        ParameterHandleValueMap parameters;    parameters[_parameterIdMessage] = ParameterValue(_message, strlen(_message) + 1);    parameters[_parameterIdSender] = ParameterValue(_username, strlen(_username) + 1);        _rtiAmbassador->sendInteraction(_communicationId, parameters, UserSuppliedTag(0, 0));}_rtiAmbassador->resignFederationExecution(CANCEL_THEN_DELETE_THEN_DIVEST );try {    _rtiAmbassador->destroyFederationExecution(L"ChatRoom");} catch (FederatesCurrentlyJoined) {}                } catch (RTI::exception& e) {                    wcout << e.what();                }            }                        void receiveInteraction(            InteractionClassHandle const & theInteraction,            std::auto_ptr< ParameterHandleValueMap > theParameterValues,            UserSuppliedTag const & theUserSuppliedTag,            OrderType const & sentOrder,            TransportationType const & theType)            throw (            InteractionClassNotRecognized,            InteractionParameterNotRecognized,            InteractionClassNotSubscribed,            FederateInternalError) {                if (theInteraction == _communicationId) {                    string message, sender;                    for (ParameterHandleValueMap::const_iterator i = theParameterValues->begin(); i != theParameterValues->end(); ++i) {                        ParameterHandle const & handle = i->first;                        ParameterValue const & value = i->second;                        if (handle == _parameterIdMessage) {                            message = string(static_cast< const char* >(value.data()), value.size());                        }                        else if(handle == _parameterIdSender) {                            sender = string(static_cast< const char* >(value.data()), value.size());                        }                    }                    cout << sender << ": " << message << endl;                                        cout << "> ";                    cout.flush();                }            }};void string2wstring(wstring &dest, const string &src) {    dest.resize(src.size());    for (string::size_type i = 0; i < src.size(); i++)        dest[i] = static_cast<unsigned char>(src[i]);}int main(int argc, char* argv[]) {    wstring host, port;    try {                if (argc > 1) {            string2wstring(host, argv[1]);        } else {            host = L"localhost";        }                port = L"8989";                ChatCCFederate chatCCFederate(host, port);                chatCCFederate.run();            } catch (RTI::RTIinternalError& e) {        wcout << endl << L"Unable to connect to CRC on " << host << L":" << port << endl;    }        return 0;}

⌨️ 快捷键说明

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