Archived
This forum has been archived. Please start a new discussion on GitHub.
Quention with icestorm about many topic subscriber for c++ !

in Help Center
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?
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?
0
Comments
-
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,
Bernard0 -
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 !0 -
The new problem is quite obvious: you are dereferencing a null pointer (adapter).
Cheers,
Bernard0 -
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 !0 -
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,
Bernard0 -
thank you very much for your reply !
I will modify my code by your prompt.
thank you very much.:)0