1 // 2 // Copyright (c) ZeroC, Inc. All rights reserved. 3 // 4 5 #include <Ice/Ice.h> 6 #include <Ice/Locator.h> 7 #include <TestI.h> 8 #include <TestHelper.h> 9 10 using namespace Test; 11 12 ServerManagerI::ServerManagerI(const ServerLocatorRegistryPtr& registry, 13 const Ice::InitializationData& initData) : 14 _registry(registry), 15 _initData(initData), 16 _nextPort(1) 17 { 18 _initData.properties->setProperty("TestAdapter.AdapterId", "TestAdapter"); 19 _initData.properties->setProperty("TestAdapter.ReplicaGroupId", "ReplicatedAdapter"); 20 _initData.properties->setProperty("TestAdapter2.AdapterId", "TestAdapter2"); 21 _initData.properties->setProperty("Ice.PrintAdapterReady", "0"); 22 } 23 24 void 25 ServerManagerI::startServer(const Ice::Current&) 26 { 27 for(::std::vector<Ice::CommunicatorPtr>::const_iterator i = _communicators.begin(); i != _communicators.end(); ++i) 28 { 29 (*i)->waitForShutdown(); 30 (*i)->destroy(); 31 } 32 _communicators.clear(); 33 34 // 35 // Simulate a server: create a new communicator and object 36 // adapter. The object adapter is started on a system allocated 37 // port. The configuration used here contains the Ice.Locator 38 // configuration variable. The new object adapter will register 39 // its endpoints with the locator and create references containing 40 // the adapter id instead of the endpoints. 41 // 42 Ice::CommunicatorPtr serverCommunicator = Ice::initialize(_initData); 43 _communicators.push_back(serverCommunicator); 44 45 // 46 // Use fixed port to ensure that OA re-activation doesn't re-use previous port from 47 // another OA (e.g.: TestAdapter2 is re-activated using port of TestAdapter). 48 // 49 int nRetry = 10; 50 while(--nRetry > 0) 51 { 52 Ice::ObjectAdapterPtr adapter; 53 Ice::ObjectAdapterPtr adapter2; 54 try 55 { 56 Ice::PropertiesPtr props = _initData.properties; 57 serverCommunicator->getProperties()->setProperty("TestAdapter.Endpoints", 58 TestHelper::getTestEndpoint(props, _nextPort++)); 59 serverCommunicator->getProperties()->setProperty("TestAdapter2.Endpoints", 60 TestHelper::getTestEndpoint(props, _nextPort++)); 61 62 adapter = serverCommunicator->createObjectAdapter("TestAdapter"); 63 adapter2 = serverCommunicator->createObjectAdapter("TestAdapter2"); 64 65 Ice::ObjectPrxPtr locator = 66 serverCommunicator->stringToProxy("locator:" + TestHelper::getTestEndpoint(props)); 67 adapter->setLocator(ICE_UNCHECKED_CAST(Ice::LocatorPrx, locator)); 68 adapter2->setLocator(ICE_UNCHECKED_CAST(Ice::LocatorPrx, locator)); 69 70 Ice::ObjectPtr object = ICE_MAKE_SHARED(TestI, adapter, adapter2, _registry); 71 _registry->addObject(adapter->add(object, Ice::stringToIdentity("test"))); 72 _registry->addObject(adapter->add(object, Ice::stringToIdentity("test2"))); 73 adapter->add(object, Ice::stringToIdentity("test3")); 74 75 adapter->activate(); 76 adapter2->activate(); 77 break; 78 } 79 catch(const Ice::SocketException&) 80 { 81 if(nRetry == 0) 82 { 83 throw; 84 } 85 86 // Retry, if OA creation fails with EADDRINUSE (this can occur when running with JS web 87 // browser clients if the driver uses ports in the same range as this test, ICE-8148) 88 if(adapter) 89 { 90 adapter->destroy(); 91 } 92 if(adapter2) 93 { 94 adapter2->destroy(); 95 } 96 } 97 } 98 } 99 100 void 101 ServerManagerI::shutdown(const Ice::Current& current) 102 { 103 for(::std::vector<Ice::CommunicatorPtr>::const_iterator i = _communicators.begin(); i != _communicators.end(); ++i) 104 { 105 (*i)->destroy(); 106 } 107 current.adapter->getCommunicator()->shutdown(); 108 } 109 110 TestI::TestI(const Ice::ObjectAdapterPtr& adapter, 111 const Ice::ObjectAdapterPtr& adapter2, 112 const ServerLocatorRegistryPtr& registry) : 113 _adapter1(adapter), _adapter2(adapter2), _registry(registry) 114 { 115 _registry->addObject(_adapter1->add(ICE_MAKE_SHARED(HelloI), Ice::stringToIdentity("hello"))); 116 } 117 118 void 119 TestI::shutdown(const Ice::Current&) 120 { 121 _adapter1->getCommunicator()->shutdown(); 122 } 123 124 HelloPrxPtr 125 TestI::getHello(const Ice::Current&) 126 { 127 return ICE_UNCHECKED_CAST(HelloPrx, _adapter1->createIndirectProxy( 128 Ice::stringToIdentity("hello"))); 129 } 130 131 HelloPrxPtr 132 TestI::getReplicatedHello(const Ice::Current&) 133 { 134 return ICE_UNCHECKED_CAST(HelloPrx, _adapter1->createProxy(Ice::stringToIdentity("hello"))); 135 } 136 137 void 138 TestI::migrateHello(const Ice::Current&) 139 { 140 const Ice::Identity id = Ice::stringToIdentity("hello"); 141 try 142 { 143 _registry->addObject(_adapter2->add(_adapter1->remove(id), id)); 144 } 145 catch(const Ice::NotRegisteredException&) 146 { 147 _registry->addObject(_adapter1->add(_adapter2->remove(id), id)); 148 } 149 } 150 151 void 152 HelloI::sayHello(const Ice::Current&) 153 { 154 } 155