码迷,mamicode.com
首页 > 其他好文 > 详细

ICE提纲之demo/IceStorm/clock

时间:2014-06-14 21:38:09      阅读:535      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   com   

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

IceStorm服务

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

 

 

 

 

ICE提纲之demo/IceStorm/clock,布布扣,bubuko.com

ICE提纲之demo/IceStorm/clock

标签:style   class   blog   code   color   com   

原文地址:http://www.cnblogs.com/leaf-w/p/3786387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!