Results 1 to 7 of 7

Thread: Quention with icestorm about many topic subscriber for c++ !

  1. #1
    ateng's Avatar
    ateng is offline Registered User
    Name: wang teng
    Organization: Nanjing University of Science and Technology
    Project: intelligent information distribution system
    Join Date
    Sep 2011
    Location
    nanjing qq:1510914311
    Posts
    37

    Question with icestorm about many topic subscriber for c++ !

    Hi,

    I'm using Ice 3.4.1 on Windows xp c++.
    I have a quention about icestorm that how can I subscribe different topics at the same time?
    the code as follow:

    int SubscriberFun(const string topicName, const Ice::CommunicatorPtr communicator)
    {
    enum Option { None, Datagram, Twoway, Oneway, Ordered};
    Option option = None;
    string id;
    option = Oneway;

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

    if(!manager)
    {
    //cerr << appName() << ": invalid proxy" << endl;
    printf("invalid proxy !\n");
    return EXIT_FAILURE;
    }

    IceStorm::TopicPrx topic;
    try
    {
    topic = manager->retrieve(topicName);
    }
    catch(const IceStorm::NoSuchTopic&)
    {
    try
    {
    topic = manager->create(topicName);
    }
    catch(const IceStorm::TopicExists&)
    {
    printf("temporary failure. try again.\n");
    return EXIT_FAILURE;
    }
    }
    Ice::ObjectAdapterPtr adapter;
    adapter = communicator->createObjectAdapter("Transporter.Subscriber");
    Ice::Identity subId;
    subId.name = id;
    if(subId.name.empty())
    {
    subId.name = IceUtil::generateUUID();
    }
    Ice::ObjectPrx subscriber = adapter->add(new TransporterI, subId);
    Ice::ObjectPrx subscriber;
    subscriber = subscriber->ice_oneway();
    IceStorm::QoS qos;
    qos["reliability"] = "";

    try
    {
    topic->subscribeAndGetPublisher(qos, subscriber);
    }
    catch(const IceStorm::AlreadySubscribed&)
    {
    //cout << "reactivating persistent subscriber" << endl;
    printf("reactivating persistent subscriber\n");
    }
    adapter->activate();
    cout<<"订阅主题为"<<topicName<<endl;

    shutdownOnInterrupt();
    communicator->waitForShutdown();
    //topic->unsubscribe(subscriber);
    cout<<"等待结束"<<endl;
    return EXIT_SUCCESS;
    }

    int main(int argc, char** argv)
    {
    communicator=initializeSub( argc, argv );//初始化
    SubscriberFun("Weather",communicator);
    SubscriberFun("wonderful",communicator);//订阅
    Sleep(30000);
    }

    what should I add in the SubscriberFun?
    Last edited by ateng; 09-08-2011 at 11:29 PM. Reason: more Detailed

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Hi Wang Ten,

    Welcome to our forums!

    Your SubscriberFun function, which retrieves a topic, create a subscriber and register this subscriber with the retrieved topic, should _not_:
    - create a new object adapter each time
    - active this object adapter
    - call communicator->waitForShutdown

    All this is usually done only once for your application.

    Best regards,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  3. #3
    ateng's Avatar
    ateng is offline Registered User
    Name: wang teng
    Organization: Nanjing University of Science and Technology
    Project: intelligent information distribution system
    Join Date
    Sep 2011
    Location
    nanjing qq:1510914311
    Posts
    37

    thank you very much for your reply !

    HI,bernard,

    I qucily changed my code when receive you reply as your prompt.
    I use a static variable time to record number of times that create a new object adapter to advoid repeating creating the adapter ,the code as follow:
    ......
    static int times=0;

    int SubscriberFun(const string topicName, const Ice::CommunicatorPtr communicator)
    {
    ++times;
    .....
    Ice::ObjectAdapterPtr adapter;
    if(times==1)
    {
    adapter = communicator->createObjectAdapter("Transporter.Subscriber");
    }
    Ice::Identity subId;
    subId.name = id;
    if(subId.name.empty())
    {
    subId.name = IceUtil::generateUUID();
    }
    Ice::ObjectPrx subscriber;
    subscriber = adapter->add(new TransporterI, subId);//error(when run to aother topic "") :sub.exe The unhandled exception at 0x004152e0: 0xC0000005: read access violation occurs when the location 0x00000000.
    subscriber = subscriber->ice_oneway();

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

    try
    {
    topic->subscribeAndGetPublisher(qos, subscriber);
    }
    catch(const IceStorm::AlreadySubscribed&)
    {
    //cout << "reactivating persistent subscriber" << endl;
    printf("reactivating persistent subscriber\n");
    }
    if (times==1)
    {
    adapter->activate();
    }
    cout<<"订阅主题为"<<topicName<<endl;

    cout<<"等待结束"<<endl;
    return EXIT_SUCCESS;}

    //error(when run to aother topic "") :sub.exe The unhandled exception at 0x004152e0: 0xC0000005: read access violation occurs when the location 0x00000000.
    Could you tell me how can I modified the code to complete the task that subscriber different topics with the same interface ?
    thank you very much !

  4. #4
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    The new problem is quite obvious: you are dereferencing a null pointer (adapter).

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  5. #5
    ateng's Avatar
    ateng is offline Registered User
    Name: wang teng
    Organization: Nanjing University of Science and Technology
    Project: intelligent information distribution system
    Join Date
    Sep 2011
    Location
    nanjing qq:1510914311
    Posts
    37

    Smile thank you very much for your reply !

    I know the reason for the new problem from the adapter .
    The true key to a puzzle is that how can I modified the code to complete the task that subscribe different topics with the same interface ?

    I puzzled with this problem many days.

    thank you very much !
    Last edited by ateng; 09-09-2011 at 11:20 AM. Reason: subscriber to subscribe

  6. #6
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    So far your code was attempting to register a new subscriber for each topic.

    If you want to register the same subscriber with different topics, there is no particular difficulty. You can simply:
    - create your subscriber
    - retrieve or create topic1, topic2, topic3 (etc.)
    - register your subscriber with each topic

    Cheers,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  7. #7
    ateng's Avatar
    ateng is offline Registered User
    Name: wang teng
    Organization: Nanjing University of Science and Technology
    Project: intelligent information distribution system
    Join Date
    Sep 2011
    Location
    nanjing qq:1510914311
    Posts
    37

    thank you very much for your reply !

    I will modify my code by your prompt.

    thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. IceStorm Subscriber callback
    By mwtupper in forum Help Center
    Replies: 5
    Last Post: 06-30-2010, 04:07 PM
  2. Topic federation across multiple IceStorm
    By danleepw in forum Help Center
    Replies: 1
    Last Post: 05-20-2010, 09:02 AM
  3. Losing Subscription to IceStorm topic
    By pmills in forum Help Center
    Replies: 1
    Last Post: 03-21-2008, 03:41 AM
  4. IceStorm - Topic creation
    By ritesh in forum Help Center
    Replies: 15
    Last Post: 02-14-2007, 11:31 PM
  5. Replies: 4
    Last Post: 08-24-2006, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •