fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Cluster / Base / testDynamicClusterClient.cpp
bloba4a8f55cd1908e56b74e9c503983cf15217ca9a5
1 // testDynamicClusterClient2
2 //
3 // OpenSG cluster client program where you can dynamicly connect/disconnect
4 // to the servers.
5 // It is similar to testDynamicClusterClient but this time we create from the
6 // current state a changelist and send this to the cluster servers.
7 // This variant is much easier to implement in your own app but could be
8 // problematic if you use more than one aspect in your app.
10 // To test it, run
11 // ./testClusterServer -w pipe0 &
12 // ./testDynamicClusterClient2 pipe0
14 // press 'c' to connect to the servers and 'd' to disconnect.
17 #include "OSGGLUT.h"
18 #include "OSGConfig.h"
19 #include "OSGSimpleGeometry.h"
20 #include "OSGGLUTWindow.h"
21 #include "OSGSimpleSceneManager.h"
22 #include "OSGMultiDisplayWindow.h"
23 #include "OSGSceneFileHandler.h"
24 #include "OSGRemoteAspect.h"
25 #include "OSGFieldContainerFactory.h"
27 #include <string>
28 #include <vector>
30 // The SimpleSceneManager to manage simple applications
31 OSG::SimpleSceneManagerRefPtr _mgr = NULL;
32 OSG::GLUTWindowRecPtr _client_win = NULL;
33 OSG::MultiDisplayWindowRecPtr _cluster_win = NULL;
34 OSG::NodeRecPtr _root = NULL;
35 std::vector<std::string> _pipenames;
37 // forward declaration so we can have the interesting stuff upfront
38 int setupGLUT( int *argc, char *argv[] );
39 void display(void);
41 // Initialize GLUT & OpenSG and set up the scene
42 int doMain(int argc, char **argv)
44 std::cout << "start a cluster server with './testClusterServer -w pipe0'\n"
45 "press 'c' to connect to the servers.\n"
46 "press 'd' to disconnect from the servers.\n"
47 "press 'n' to delete current scene.\n"
48 "press 't' to create a torus.\n"
49 "press 'l' to load scene 'tie.wrl'.\n"
50 << std::endl;
52 // OSG init
53 OSG::osgInit(argc,argv);
55 // GLUT init
56 int winid = setupGLUT(&argc, argv);
58 // the connection between GLUT and OpenSG
60 _client_win = OSG::GLUTWindow::create();
62 _client_win->setGlutId(winid);
63 _client_win->init();
64 _client_win->setSize(300,300);
66 for(OSG::Int32 i=0;i<argc-1;++i)
68 if(argv[i+1] != NULL)
69 _pipenames.push_back(argv[i+1]);
72 if(_pipenames.empty())
73 _pipenames.push_back("pipe0");
75 _root = OSG::Node::create();
77 _root->setCore(OSG::Group::create());
79 // create default scene
80 OSG::NodeUnrecPtr scene = OSG::makeTorus(.5, 2, 16, 16);
82 _root->addChild(scene);
84 // create the SimpleSceneManager helper
85 _mgr = OSG::SimpleSceneManager::create();
87 // tell the manager what to manage
88 _mgr->setWindow(_client_win );
89 _mgr->setRoot (_root);
91 // show the whole scene
92 _mgr->showAll();
94 return 0;
97 int main(int argc, char **argv)
99 doMain(argc, argv);
101 glutMainLoop();
103 return 0;
106 static void connectCluster(void)
108 if(_cluster_win != NULL)
109 return;
111 OSG::Viewport *clientvp = _client_win->getPort(0);
113 // create the viewports for the cluster just a simple one ...
114 OSG::ViewportUnrecPtr vp = OSG::Viewport::create();
116 vp->setCamera (_mgr->getCamera());
117 vp->setBackground(clientvp->getBackground());
118 vp->setRoot (clientvp->getRoot());
119 vp->setSize (0,0, 1,1);
121 // the connection between this client and the servers
122 _cluster_win = OSG::MultiDisplayWindow::create();
124 // all changes must be enclosed in beginEditCP and endEditCP
125 // otherwise the changes will not be transfered over the network.
127 for(OSG::UInt32 i=0;i<_pipenames.size();++i)
128 _cluster_win->editMFServers()->push_back(_pipenames[i]);
129 // dummy size for navigator
130 _cluster_win->setSize(300,300);
131 _cluster_win->addPort(vp);
133 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
134 OSG::Thread::getCurrentChangeList()->fillFromCurrentState();
135 //Thread::getCurrentChangeList()->dump();
136 // create from the current state a changelist.
138 // initialize window
139 _cluster_win->init();
141 // apply changelist to the servers
142 _cluster_win->render(_mgr->getRenderAction());
144 // clear changelist
145 OSG::Thread::getCurrentChangeList()->clear();
147 glutPostRedisplay();
150 static void disconnectCluster(void)
152 if(_cluster_win == NULL)
153 return;
155 _cluster_win = NULL;
159 // GLUT callback functions
162 // redraw the window
163 void display(void)
165 // redraw the client window
166 _mgr->redraw();
169 OSG::commitChanges();
173 if(_cluster_win != NULL)
175 // redraw the server windows
176 _cluster_win->render(_mgr->getRenderAction());
180 catch(OSG_STDEXCEPTION_NAMESPACE::exception &)
182 //printf("error: '%s'\n", e.what());
183 printf("ClusterServer was killed!\n");
185 _cluster_win = NULL;
188 OSG::commitChanges();
189 OSG::clearChangeList();
192 // react to size changes
193 void reshape(int w, int h)
195 glutPostRedisplay();
198 // react to mouse button presses
199 void mouse(int button, int state, int x, int y)
201 if (state)
202 _mgr->mouseButtonRelease(button, x, y);
203 else
204 _mgr->mouseButtonPress(button, x, y);
206 glutPostRedisplay();
209 // react to mouse motions with pressed buttons
210 void motion(int x, int y)
212 _mgr->mouseMove(x, y);
214 glutPostRedisplay();
217 // react to keys
218 void keyboard(unsigned char k, int x, int y)
220 switch(k)
222 case 27:
224 _mgr = NULL;
226 _client_win = NULL;
227 _cluster_win = NULL;
228 _root = NULL;
230 OSG::osgExit();
231 exit(0);
233 case 'n':
234 while(_root->getNChildren() > 0)
235 _root->subChild(_root->getChild(0));
237 glutPostRedisplay();
238 break;
239 case 'l':
241 OSG::NodeUnrecPtr scene =
242 OSG::SceneFileHandler::the()->read("tie.wrl", NULL);
244 if(scene != NULL)
246 _root->addChild(scene);
248 _mgr->showAll();
250 glutPostRedisplay();
253 break;
254 case 't':
256 OSG::NodeUnrecPtr scene = OSG::makeTorus(.5, 2, 16, 16);
258 _root->addChild(scene);
260 _mgr->showAll();
262 glutPostRedisplay();
264 break;
265 case 'c':
266 connectCluster();
267 break;
268 case 'd':
269 disconnectCluster();
270 break;
274 // setup the GLUT library which handles the windows for us
275 int setupGLUT(int *argc, char *argv[])
277 glutInit(argc, argv);
278 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
280 int winid = glutCreateWindow("OpenSG");
282 glutReshapeFunc(reshape);
283 glutDisplayFunc(display);
284 glutMouseFunc(mouse);
285 glutMotionFunc(motion);
286 glutKeyboardFunc(keyboard);
288 return winid;