首页 > 技术文章 > ICE提纲之demo/IceStorm/clock(发布者/订阅者)

leaf-w 2014-06-13 18:35 原文

ICE发布者/订阅者的一个最简单例子

IceStorm服务

启动命令

$ /Library/Developer/Ice-3.5.1/bin/icebox --Ice.Config=config.icebox

config.icebox

#
# The IceBox server endpoint configuration. This endpoint is only used
# to communicate with the IceBox ServiceManager object (such as when
# using iceboxadmin to shutdown the server).
#
# The IceStorm service has its own endpoints (see config.service).
#
IceBox.ServiceManager.Endpoints=tcp -h localhost -p 9998

#
# The IceStorm service. The service is configured using a separate
# configuration file (see config.service).
#
IceBox.Service.IceStorm=IceStormService,35:createIceStorm --Ice.Config=config.service

config.service

#
# The IceStorm service instance name.
#
IceStorm.InstanceName=DemoIceStorm

#
# This property defines the endpoints on which the IceStorm
# TopicManager listens.
#
IceStorm.TopicManager.Endpoints=default -h localhost -p 10000

#
# This property defines the endpoints on which the topic
# publisher objects listen. If you want to federate
# IceStorm instances this must run on a fixed port (or use
# IceGrid).
#
IceStorm.Publish.Endpoints=tcp -h localhost -p 10001:udp -h localhost -p 10001

Slice

Clock.ice

module Demo
{

interface Clock
{
    void tick(string time);
};

};

发布者

config.pub

#
# This property is used by the clients to connect to IceStorm.
#
TopicManager.Proxy=DemoIceStorm/TopicManager:default -h localhost -p 10000

Publisher.cpp

int
Publisher::run(int argc, char* argv[])
{
    IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast(
        communicator()->propertyToProxy("TopicManager.Proxy"));

    //
    // Retrieve the topic.
    //
    string topicName = "time";
    IceStorm::TopicPrx topic;
    topic = manager->create(topicName);
    // or
    topic = manager->retrieve(topicName);

    //
    // Get the topic's publisher object, and create a Clock proxy
    //
    Ice::ObjectPrx publisher = topic->getPublisher();
    
    ClockPrx clock = ClockPrx::uncheckedCast(publisher);

    while(true)
    {
        clock->tick(IceUtil::Time::now().toDateTime());
        IceUtil::ThreadControl::sleep(IceUtil::Time::seconds(1));
    }
}

订阅者

config.sub

#
# This property is used to configure the endpoints of the clock
# subscriber adapter. These endpoints are where the client receives
# topic messages from IceStorm.
#
Clock.Subscriber.Endpoints=tcp:udp

#
# This property is used by the clients to connect to IceStorm.
#
TopicManager.Proxy=DemoIceStorm/TopicManager:default -h localhost -p 10000

Subscriber.cpp

class ClockI : public Clock
{
public:

    virtual void
    tick(const string& time, const Ice::Current&)
    {
        cout << time << endl;
    }
};

int
Subscriber::run(int argc, char* argv[])
{
    string topicName = "time";

    IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast(
        communicator()->propertyToProxy("TopicManager.Proxy"));

    IceStorm::TopicPrx topic;
    topic = manager->retrieve(topicName);
    // or
    topic = manager->create(topicName);

    Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Clock.Subscriber");

    //
    // Add a servant for the Ice object. If --id is used the identity
    // comes from the command line, otherwise a UUID is used.
    //
    // id is not directly altered since it is used below to detect
    // whether subscribeAndGetPublisher can raise AlreadySubscribed.
    //
    Ice::Identity subId;
    Ice::ObjectPrx subscriber = adapter->add(new ClockI, subId);

    //
    // Activate the object adapter before subscribing.
    //
    adapter->activate();

    IceStorm::QoS qos;
    qos["retryCount"] = retryCount;
    qos["reliability"] = "ordered";

    topic->subscribeAndGetPublisher(qos, subscriber);

    shutdownOnInterrupt();
    communicator()->waitForShutdown();

    topic->unsubscribe(subscriber);
}

 

 

 

 

推荐阅读