Server.cpp 1.43 KB
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/Ice.h>
#include <Printer.h>
#include "EvictorBase.h"

using namespace std;
using namespace Demo;

class PrinterI : public Printer
{
    std::string name;
public:
    PrinterI(const std::string& a) : name(a) {};
    virtual void printString(string s, const Ice::Current&) override;
    void shutdown(const Ice::Current& c) override
    {
      cout << "Shutting down..." << endl;
      c.adapter->getCommunicator()->shutdown();
    }
};

void
PrinterI::printString(string s, const Ice::Current&)
{
    cout << name << ":" << s << endl;
}


class myEvictor: public EvictorBase
{
    virtual std::shared_ptr<Ice::Object> add(const Ice::Current& c, std::shared_ptr<void>&) override
    {
        cout << "Adding new servant" << endl;
        return make_shared<PrinterI>(c.id.name);
    }

    virtual void evict(const std::shared_ptr<Ice::Object>&, const std::shared_ptr<void>&) override
    {
	
    }

};


int
main(int argc, char* argv[])
{
    try
    {
        Ice::CommunicatorHolder ich(argc, argv);
        auto adapter = ich->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
        auto evictor = make_shared<myEvictor>();
        adapter->addServantLocator(evictor,"");
        adapter->activate();

        ich->waitForShutdown();
    }
    catch(const std::exception& e)
    {
        cerr << e.what() << endl;
        return 1;
    }
    return 0;
}