00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047 #include "OSGClusterNetwork.h"
00048 #include "OSGConnection.h"
00049 #include "OSGConnectionFactory.h"
00050 #include "OSGRemoteAspect.h"
00051
00052 OSG_USING_NAMESPACE
00053
00072
00073
00074
00077 ClusterNetwork::ClusterNetwork(UInt32 id) :
00078 Inherited(),
00079 _aspect(NULL),
00080 _mainConnection(NULL),
00081 _connection(),
00082 _id(id)
00083 {
00084 }
00085
00086
00087
00088
00091 ClusterNetwork::~ClusterNetwork(void)
00092 {
00093 if(_aspect)
00094 delete _aspect;
00095 _aspect=NULL;
00096 if(_mainConnection)
00097 delete _mainConnection;
00098 _mainConnection=NULL;
00099 for(ConnectionsT::iterator c = _connection.begin();
00100 c != _connection.end();
00101 ++c)
00102 {
00103 if(*c)
00104 delete (*c);
00105 (*c)=NULL;
00106 }
00107 _connection.clear();
00108 ConnectionInfoMapT::iterator ci=_map.find(_id);
00109 if(ci!=_map.end())
00110 {
00111 _map.erase(ci);
00112 }
00113 }
00114
00115
00116
00117
00121 Connection *ClusterNetwork::getMainConnection(void)
00122 {
00123 return _mainConnection;
00124 }
00125
00129 GroupConnection *ClusterNetwork::getMainGroupConnection(void)
00130 {
00131 return dynamic_cast<GroupConnection*>(_mainConnection);
00132 }
00133
00137 PointConnection *ClusterNetwork::getMainPointConnection(void)
00138 {
00139 return dynamic_cast<PointConnection*>(_mainConnection);
00140 }
00141
00144 Connection *ClusterNetwork::getConnection(UInt32 id)
00145 {
00146 if(id<_connection.size())
00147 return _connection[id];
00148 else
00149 return NULL;
00150 }
00151
00155 GroupConnection *ClusterNetwork::getGroupConnection(UInt32 id)
00156 {
00157 return dynamic_cast<GroupConnection*>(getConnection(id));
00158 }
00159
00163 PointConnection *ClusterNetwork::getPointConnection(UInt32 id)
00164 {
00165 return dynamic_cast<PointConnection*>(getConnection(id));
00166 }
00167
00170 RemoteAspect *ClusterNetwork::getAspect(void)
00171 {
00172 return _aspect;
00173 }
00174
00175 ClusterNetwork::ConnectionsT &ClusterNetwork::getConnection(void)
00176 {
00177 return _connection;
00178 }
00179
00180
00181
00182
00185 void ClusterNetwork::setAspect(RemoteAspect *aspect)
00186 {
00187 _aspect=aspect;
00188 }
00189
00192 void ClusterNetwork::setMainConnection(Connection *connection)
00193 {
00194 _mainConnection=connection;
00195 }
00196
00199 void ClusterNetwork::setConnection(UInt32 id,Connection *connection)
00200 {
00201 while(id<_connection.size())
00202 _connection.push_back(NULL);
00203 _connection[id]=connection;
00204 }
00205
00206
00207
00208
00220 void ClusterNetwork::connectAllPointToPoint(
00221 UInt32 thisId,
00222 const std::string &connectionType)
00223 {
00224 bool isClient;
00225 UInt32 servers;
00226 Connection *addressSource;
00227 UInt32 id,c;
00228 std::map<UInt32,std::string> address;
00229 std::vector<UInt32> fromId;
00230 std::vector<UInt32> toId;
00231 std::vector<std::string> toAddr;
00232 UInt32 addrCount;
00233 Connection::Channel channel;
00234 std::string clientAddress;
00235
00236
00237 if(dynamic_cast<GroupConnection*>(_mainConnection))
00238 isClient = true;
00239 else
00240 isClient = false;
00241
00242 if(isClient)
00243 {
00244 servers = getMainGroupConnection()->getChannelCount();
00245 _mainConnection->putValue(servers);
00246 _mainConnection->flush();
00247 }
00248 else
00249 {
00250 _mainConnection->getValue(servers);
00251 }
00252
00253 addrCount = (servers+1) * servers / 2;
00254 fromId.resize(addrCount);
00255 toId.resize (addrCount);
00256 toAddr.resize(addrCount);
00257
00258 _connection.resize(servers+1);
00259
00260 for(id = 0; id <= servers; ++id)
00261 {
00262 if(isClient && id == thisId)
00263 _connection[id] =
00264 ConnectionFactory::the().createGroup(connectionType);
00265 else
00266 _connection[id] =
00267 ConnectionFactory::the().createPoint(connectionType);
00268 if(id > thisId)
00269 {
00270 _mainConnection->putValue(id);
00271 _mainConnection->putValue(thisId);
00272 _mainConnection->putValue(_connection[id]->bind());
00273 _mainConnection->flush();
00274 }
00275 }
00276
00277 if(isClient)
00278 {
00279 for(c = 0 ; c < addrCount ; ++c)
00280 {
00281 channel = getMainGroupConnection()->selectChannel();
00282 getMainGroupConnection()->getValue(fromId[c]);
00283 getMainGroupConnection()->getValue(toId[c]);
00284 getMainGroupConnection()->getValue(toAddr[c]);
00285
00286 if(fromId[c] == thisId)
00287 {
00288 address[toId[c]] = toAddr[c];
00289 getMainGroupConnection()->subSelection(channel);
00290 }
00291 }
00292 getMainGroupConnection()->resetSelection();
00293 for(c = 0 ; c < addrCount ; ++c)
00294 {
00295 _mainConnection->putValue(fromId[c]);
00296 _mainConnection->putValue(toId[c]);
00297 _mainConnection->putValue(toAddr[c]);
00298 }
00299 _mainConnection->flush();
00300 }
00301
00302 if(!isClient)
00303 {
00304 _mainConnection->selectChannel();
00305 for(c = 0 ; c < (servers+1) * servers / 2 ; ++c)
00306 {
00307 _mainConnection->getValue(fromId[c]);
00308 _mainConnection->getValue(toId[c]);
00309 _mainConnection->getValue(toAddr[c]);
00310 if(fromId[c] == thisId)
00311 address[toId[c]] = toAddr[c];
00312 }
00313 }
00314
00315 for(id = 0; id <= servers; ++id)
00316 {
00317 if(id > thisId)
00318 {
00319 _connection[id]->acceptPoint();
00320 }
00321 if(id < thisId)
00322 {
00323 for(;;)
00324 try
00325 {
00326 _connection[id]->connectPoint(address[id]);
00327 break;
00328 }
00329 catch(...)
00330 {
00331 }
00332 }
00333 }
00334
00335
00336 if(isClient)
00337 {
00338 _mainConnection->putValue(_connection[thisId]->bind());
00339 _mainConnection->flush();
00340 for(id=0 ; id < servers ; ++id)
00341 {
00342 getGroupConnection(thisId)->acceptPoint();
00343 }
00344 }
00345 else
00346 {
00347 _mainConnection->getValue(clientAddress);
00348 for(;;)
00349 try
00350 {
00351 getPointConnection(thisId)->connectGroup(clientAddress);
00352 break;
00353 }
00354 catch(...)
00355 {
00356 }
00357 }
00358 }
00359
00370 void ClusterNetwork::connectAllGroupToPoint(
00371 UInt32 thisId,
00372 const std::string &connectionType)
00373 {
00374 UInt32 id,toId;
00375 bool isClient;
00376 UInt32 servers;
00377 std::map<UInt32,std::string> address;
00378 Connection::Channel channel;
00379 std::string groupAddress;
00380
00381
00382 if(dynamic_cast<GroupConnection*>(_mainConnection))
00383 isClient = true;
00384 else
00385 isClient = false;
00386
00387
00388 if(isClient)
00389 {
00390 servers = getMainGroupConnection()->getChannelCount();
00391 _mainConnection->putValue(servers);
00392 _mainConnection->flush();
00393 }
00394 else
00395 {
00396 _mainConnection->getValue(servers);
00397 }
00398
00399
00400 _connection.resize(servers+1);
00401 for(id = 0; id <= servers; ++id)
00402 {
00403 if(id == thisId)
00404 {
00405 _connection[id] =
00406 ConnectionFactory::the().createGroup(connectionType);
00407 groupAddress = _connection[id]->bind();
00408 }
00409 else
00410 _connection[id] =
00411 ConnectionFactory::the().createPoint(connectionType);
00412 }
00413 if(isClient)
00414 {
00415 address[thisId] = groupAddress;
00416 while(getMainGroupConnection()->getSelectionCount())
00417 {
00418 channel = _mainConnection->selectChannel();
00419 getMainGroupConnection()->subSelection(channel);
00420 getMainGroupConnection()->getValue(groupAddress);
00421 address[channel] = groupAddress;
00422 }
00423 getMainGroupConnection()->resetSelection();
00424 for(id = 0; id <= servers; ++id)
00425 {
00426 _mainConnection->putValue(address[id]);
00427 }
00428 _mainConnection->flush();
00429 }
00430 else
00431 {
00432 _mainConnection->putValue(groupAddress);
00433 _mainConnection->flush();
00434 for(id = 0; id <= servers; ++id)
00435 {
00436 _mainConnection->getValue(groupAddress);
00437 address[id] = groupAddress;
00438 }
00439 }
00440
00441 for(id = 1; id <= servers; ++id)
00442 {
00443 toId = (thisId+id)%(servers+1);
00444 if(thisId & 1)
00445 {
00446 getGroupConnection(thisId)->acceptPoint();
00447 getPointConnection(toId)->connectGroup(address[toId]);
00448 }
00449 else
00450 {
00451 getPointConnection(toId)->connectGroup(address[toId]);
00452 getGroupConnection(thisId)->acceptPoint();
00453 }
00454 }
00455 }
00456
00457
00458
00459
00462 ClusterNetwork *ClusterNetwork::getInstance(UInt32 clusterWindowId)
00463 {
00464 ConnectionInfoMapT::iterator ci=_map.find(clusterWindowId);
00465 if(ci==_map.end())
00466 {
00467 ClusterNetwork *ciP=new ClusterNetwork(clusterWindowId);
00468 _map[clusterWindowId]=ciP;
00469 return ciP;
00470 }
00471 else
00472 {
00473 return ci->second;
00474 }
00475 }
00476
00477
00478
00479
00480 ClusterNetwork::ConnectionInfoMapT ClusterNetwork::_map;
00481
00482
00483
00484
00485 #ifdef __sgi
00486 #pragma set woff 1174
00487 #endif
00488
00489 #ifdef OSG_LINUX_ICC
00490 #pragma warning( disable : 177 )
00491 #endif
00492
00493 namespace
00494 {
00495 static Char8 cvsid_cpp[] = "@(#)$Id:$";
00496 static Char8 cvsid_hpp[] = OSG_CLUSTERCONNECTINFOHEADER_CVSID;
00497 }
00498