示例#1
0
bool
ChatServer::start(int, char*[], int& status)
{
    _timer = new IceUtil::Timer();
    // Timeout for reaping polling sessions.
    int timeout = communicator()->getProperties()->getPropertyAsIntWithDefault("ReaperTimeout", 10);
    bool traceEnabled = communicator()->getProperties()->getPropertyAsIntWithDefault("Server.Trace", 0) != 0;
    Ice::LoggerPtr logger = communicator()->getLogger();
    ReaperTaskPtr reaper = new ReaperTask(timeout, traceEnabled, logger);
    _timer->scheduleRepeated(reaper, IceUtil::Time::seconds(timeout));
    try
    {
        _adapter = communicator()->createObjectAdapter("ChatServer");

        ChatRoomPtr chatRoom = new ChatRoom(traceEnabled, logger);
        if(traceEnabled)
        {
            ostringstream os;
            os << "Chat room created ok.";
            logger->trace("info", os.str());
        }
        _adapter->add(new ChatSessionManagerI(chatRoom, traceEnabled, logger),
                     communicator()->stringToIdentity("ChatSessionManager"));

        if(traceEnabled)
        {
            ostringstream os;
            os << "Chat session manager created ok.";
            logger->trace("info", os.str());
        }
        _adapter->add(new PollingChatSessionFactoryI(chatRoom, reaper, traceEnabled, logger),
                     communicator()->stringToIdentity("PollingChatSessionFactory"));

        if(traceEnabled)
        {
            ostringstream os;
            os << "Polling chat session factory created ok.";
            logger->trace("info", os.str());
        }

        _adapter->activate();
        if(traceEnabled)
        {
            ostringstream os;
            os << "Chat server started ok.";
            logger->trace("info", os.str());
        }
    }
    catch(const Ice::LocalException&)
    {
        status = EXIT_FAILURE;
        _timer->destroy();
        throw;
    }
    status = EXIT_SUCCESS;
    return true;
}
示例#2
0
int
SessionServer::run(int argc, char*[])
{
    if(argc > 1)
    {
        cerr << appName() << ": too many arguments" << endl;
        return EXIT_FAILURE;
    }

    Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("SessionFactory");

    IceUtil::TimerPtr timer = new IceUtil::Timer();
    ReapTaskPtr reapTask = new ReapTask;
    timer->scheduleRepeated(reapTask, IceUtil::Time::seconds(1));

    SessionFactoryPtr factory = new SessionFactoryI(reapTask);
    adapter->add(factory, Ice::stringToIdentity("SessionFactory"));
    adapter->activate();
    communicator()->waitForShutdown();

    timer->destroy();

    return EXIT_SUCCESS;
}
示例#3
0
int
SessionClient::run(int argc, char* argv[])
{
    if(argc > 1)
    {
        cerr << appName() << ": too many arguments" << endl;
        return EXIT_FAILURE;
    }

    string name;
    cout << "Please enter your name ==> ";
    cin >> name;
    if(!cin.good())
    {
        return EXIT_FAILURE;
    }

    Ice::ObjectPrx base = communicator()->propertyToProxy("SessionFactory.Proxy");
    SessionFactoryPrx factory = SessionFactoryPrx::checkedCast(base);
    if(!factory)
    {
        cerr << argv[0] << ": invalid proxy" << endl;
        return EXIT_FAILURE;
    }

    {
        IceUtil::Mutex::Lock sync(_mutex);
        _session = factory->create(name);
        
        _timer = new IceUtil::Timer();
        _timer->scheduleRepeated(new RefreshTask(communicator()->getLogger(), _session), IceUtil::Time::seconds(5));
    }

    vector<HelloPrx> hellos;

    menu();

    try
    {
        bool destroy = true;
        bool shutdown = false;
        while(true)
        {
            cout << "==> ";
            char c;
            cin >> c;
            if(!cin.good())
            {
                break;
            }
            if(isdigit(c))
            {
                string s;
                s += c;
                vector<HelloPrx>::size_type index = atoi(s.c_str());
                if(index < hellos.size())
                {
                    hellos[index]->sayHello();
                }
                else
                {
                    cout << "Index is too high. " << hellos.size() << " hello objects exist so far.\n"
                         << "Use `c' to create a new hello object." << endl;
                }
            }
            else if(c == 'c')
            {
                hellos.push_back(_session->createHello());
                cout << "Created hello object " << hellos.size() - 1 << endl;
            }
            else if(c == 's')
            {
                destroy = false;
                shutdown = true;
                break;
            }
            else if(c == 'x')
            {
                break;
            }
            else if(c == 't')
            {
                destroy = false;
                break;
            }
            else if(c == '?')
            {
                menu();
            }
            else
            {
                cout << "Unknown command `" << c << "'." << endl;
                menu();
            }
        }

        cleanup(destroy);
        if(shutdown)
        {
            factory->shutdown();
        }
    }
    catch(...)
    {
        try
        {
            cleanup(true);
        }
        catch(...)
        {
        }
        throw;
    }

    return EXIT_SUCCESS;
}