int main(int argc, char** argv){ int status; Ice::CommunicatorPtr ic; try{ ic = Ice::initialize(argc,argv); std::string topicName = ic->getProperties()->getProperty("Cameraview_icestorm.Camera.TopicName"); std::cout << "Trying to conect to: " << topicName << std::endl; Ice::ObjectPrx obj=ic->propertyToProxy("Cameraview_icestorm.Camera.TopicManager"); IceStorm::TopicManagerPrx topicManager=IceStorm::TopicManagerPrx::checkedCast(obj); std::string objAdapterEndpoint = ic->getProperties()->getProperty("Cameraview_icestorm.Camera.ObjectAdapter"); Ice::ObjectAdapterPtr adapter=ic->createObjectAdapterWithEndpoints("CameraAdapter",objAdapterEndpoint); ImageConsumerI* imageConsumer = new ImageConsumerI; Ice::ObjectPrx proxy = adapter->addWithUUID(imageConsumer)->ice_oneway(); IceStorm::TopicPrx topic; try { topic = topicManager->retrieve(topicName); IceStorm::QoS qos; topic->subscribeAndGetPublisher(qos, proxy); } catch (const IceStorm::NoSuchTopic& ex) { std::cerr << ex << std::endl; } adapter->activate(); ic->waitForShutdown(); topic->unsubscribe(proxy); if (ic) ic->destroy(); return status; }catch (const Ice::Exception& ex) { std::cerr << ex << std::endl; status = 1; } catch (const char* msg) { std::cerr << msg << std::endl; status = 1; } }
void main() { Ice::InitializationData initData; initData.properties = Ice::createProperties(); initData.properties->setProperty("Ice.MessageSizeMax", "102400" );//默认是1024,单位KB initData.properties->setProperty("Ice.ThreadPool.Server.Size", "1"); initData.properties->setProperty("Ice.ThreadPool.Server.SizeMax", "1000" ); initData.properties->setProperty("Ice.ThreadPool.Server.SizeWarn", "1024"); Ice::CommunicatorPtr communicatorPtr = Ice::initialize(initData); char szStormHost[100]={0}; char szStromPort[10]={0}; char szDir[MAX_PATH] = {0}; GetModuleFileName(NULL,szDir,MAX_PATH); string strIniFile = szDir; strIniFile = strIniFile.substr(0,strIniFile.length()-3) + "ini"; GetPrivateProfileString("NewsPub","StormHost","localhost",szStormHost,100,strIniFile.c_str()); WritePrivateProfileString("NewsPub","StormHost",szStormHost,strIniFile.c_str()); GetPrivateProfileString("NewsPub","StromPort","10000",szStromPort,100,strIniFile.c_str()); WritePrivateProfileString("NewsPub","StromPort",szStromPort,strIniFile.c_str()); char szStr[1000]={0}; sprintf_s(szStr,"StormNewsDemo/TopicManager:tcp -h %s -p %s",szStormHost,szStromPort); // icestorm的地址"StormNewsDemo/TopicManager:tcp -h xiangzhenwei.peraportal.com -p 10000" IceStorm::TopicManagerPrx manager = NULL; try { manager = IceStorm::TopicManagerPrx::checkedCast(communicatorPtr->stringToProxy(szStr)); } catch (const Ice::Exception &e) { cerr << e.what(); return; } if(!manager) { cerr << "NewsSub.exe" << ": invalid proxy" << endl; return; } IceStorm::TopicPrx topic; try { topic = manager->retrieve("news"); } catch(const IceStorm::NoSuchTopic&) { try { topic = manager->create("news"); } catch(const IceStorm::TopicExists&) { cerr << "NewsSub.exe" << ": temporary failure. try again." << endl; return; } } // 接收端监听消息的地址"tcp -h 0.0.0.0:udp -h 0.0.0.0" string strEndPoint = "tcp -h 0.0.0.0:udp -h 0.0.0.0"; Ice::ObjectAdapterPtr adapter = communicatorPtr->createObjectAdapterWithEndpoints("News.Subscriber", strEndPoint ); // // 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; subId.name = IceUtil::generateUUID(); Ice::ObjectPrx subscriber = adapter->add(new NewsI, subId); g_strClientId = subId.name; Ice::CommunicatorPtr communicatorPtr2 = InitCommunicator(); char szEndPoints[1000]={0}; sprintf_s(szEndPoints,"Pera601DemoServerService:tcp -h %s -p %s -t 5000", szStormHost, "20131"); try { PcIdToWsServerPrx m_pPrx = PcIdToWsServerPrx::checkedCast(communicatorPtr2->stringToProxy(szEndPoints)); m_pPrx = m_pPrx->ice_twoway()->ice_timeout(20000)->ice_secure(false); m_pPrx->TellClientId(subId.name); } catch(const Ice::Exception& ex) { printf("远程调用服务端失败,ICE异常:%s", ex.ice_name().c_str()); return; } // // Activate the object adapter before subscribing. // adapter->activate(); subscriber = subscriber->ice_oneway(); IceStorm::QoS qos; qos["retryCount"] = 3; try { topic->subscribeAndGetPublisher(qos, subscriber); } catch(const IceStorm::AlreadySubscribed&) { // If we're manually setting the subscriber id ignore. cout << "reactivating persistent subscriber" << endl; } communicatorPtr->waitForShutdown(); topic->unsubscribe(subscriber); }
int Subscriber::run(int argc, char* argv[]) { Ice::StringSeq args = Ice::argsToStringSeq(argc, argv); args = communicator()->getProperties()->parseCommandLineOptions("Clock", args); Ice::stringSeqToArgs(args, argc, argv); bool batch = false; enum Option { None, Datagram, Twoway, Oneway, Ordered}; Option option = None; string topicName = "time"; string id; string retryCount; int i; for(i = 1; i < argc; ++i) { string optionString = argv[i]; Option oldoption = option; if(optionString == "--datagram") { option = Datagram; } else if(optionString == "--twoway") { option = Twoway; } else if(optionString == "--oneway") { option = Oneway; } else if(optionString == "--ordered") { option = Ordered; } else if(optionString == "--batch") { batch = true; } else if(optionString == "--id") { ++i; if(i >= argc) { usage(argv[0]); return EXIT_FAILURE; } id = argv[i]; } else if(optionString == "--retryCount") { ++i; if(i >= argc) { usage(argv[0]); return EXIT_FAILURE; } retryCount = argv[i]; } else if(optionString.substr(0, 2) == "--") { usage(argv[0]); return EXIT_FAILURE; } else { topicName = argv[i++]; break; } if(oldoption != option && oldoption != None) { usage(argv[0]); return EXIT_FAILURE; } } if(i != argc) { usage(argv[0]); return EXIT_FAILURE; } if(!retryCount.empty()) { if(option == None) { option = Twoway; } else if(option != Twoway && option != Ordered) { cerr << argv[0] << ": retryCount requires a twoway proxy" << endl; return EXIT_FAILURE; } } if(batch && (option == Twoway || option == Ordered)) { cerr << argv[0] << ": batch can only be set with oneway or datagram" << endl; return EXIT_FAILURE; } IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast( communicator()->propertyToProxy("TopicManager.Proxy")); if(!manager) { cerr << appName() << ": invalid proxy" << endl; return EXIT_FAILURE; } IceStorm::TopicPrx topic; try { topic = manager->retrieve(topicName); } catch(const IceStorm::NoSuchTopic&) { try { topic = manager->create(topicName); } catch(const IceStorm::TopicExists&) { cerr << appName() << ": temporary failure. try again." << endl; return EXIT_FAILURE; } } 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; subId.name = id; if(subId.name.empty()) { subId.name = IceUtil::generateUUID(); } Ice::ObjectPrx subscriber = adapter->add(new ClockI, subId); // // Activate the object adapter before subscribing. // adapter->activate(); IceStorm::QoS qos; if(!retryCount.empty()) { qos["retryCount"] = retryCount; } // // Set up the proxy. // if(option == Datagram) { if(batch) { subscriber = subscriber->ice_batchDatagram(); } else { subscriber = subscriber->ice_datagram(); } } else if(option == Twoway) { // Do nothing to the subscriber proxy. Its already twoway. } else if(option == Ordered) { // Do nothing to the subscriber proxy. Its already twoway. qos["reliability"] = "ordered"; } else if(option == Oneway || option == None) { if(batch) { subscriber = subscriber->ice_batchOneway(); } else { subscriber = subscriber->ice_oneway(); } } try { topic->subscribeAndGetPublisher(qos, subscriber); } catch(const IceStorm::AlreadySubscribed&) { // If we're manually setting the subscriber id ignore. if(id.empty()) { throw; } cout << "reactivating persistent subscriber" << endl; } shutdownOnInterrupt(); communicator()->waitForShutdown(); topic->unsubscribe(subscriber); return EXIT_SUCCESS; }
void allTests(const Ice::CommunicatorPtr& communicator) { { cout << "Testing Glacier2 stub... " << flush; char** argv = 0; int argc = 0; SessionHelperClient client; client.run(argc, argv); cout << "ok" << endl; } { cout << "Testing IceStorm stub... " << flush; IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::uncheckedCast(communicator->stringToProxy("test:default -p 12010")); IceStorm::QoS qos; IceStorm::TopicPrx topic; string topicName = "time"; try { topic = manager->retrieve(topicName); test(false); } catch(const IceStorm::NoSuchTopic&) { test(false); } catch(const Ice::LocalException&) { } Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapterWithEndpoints("subscriber" ,"tcp"); Ice::ObjectPrx subscriber = adapter->addWithUUID(new ClockI); adapter->activate(); try { topic->subscribeAndGetPublisher(qos, subscriber); test(false); } catch(const IceStorm::AlreadySubscribed&) { test(false); } catch(const IceUtil::NullHandleException&) { } cout << "ok" << endl; } { cout << "Testing IceGrid stub... " << flush; Ice::ObjectPrx base = communicator->stringToProxy("test:default -p 12010"); IceGrid::RegistryPrx registry = IceGrid::RegistryPrx::uncheckedCast(base); IceGrid::AdminSessionPrx session; IceGrid::AdminPrx admin; try { session = registry->createAdminSession("username", "password"); test(false); } catch(const IceGrid::PermissionDeniedException&) { test(false); } catch(const Ice::LocalException&) { } try { admin = session->getAdmin(); test(false); } catch(const IceUtil::NullHandleException&) { } cout << "ok" << endl; } }