fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Cluster / Server / testClusterServer.cpp
blobcdce4a7e5e7fe690b09d9d5fc26ab23498950983
1 #include "OSGGLUT.h"
2 #include "OSGConfig.h"
3 #include <iostream>
4 #include "OSGLog.h"
5 #include "OSGClusterServer.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGRenderAction.h"
8 #include "OSGSceneFileHandler.h"
9 #include "OSGViewport.h"
11 int winid;
12 OSG::ClusterServer *server;
13 OSG::GLUTWindowRecPtr window;
14 OSG::RenderActionRefPtr ract;
15 bool running=false;
16 bool exitOnError=false;
17 OSG::UInt32 servicePort=8437;
18 std::string serviceGroup="224.245.211.234";
19 int winWidth=0,winHeight=0;
21 void cleanup(void)
23 window = NULL;
25 server->stop();
26 delete server;
28 server = NULL;
29 ract = NULL;
31 OSG::osgExit();
34 void display()
36 if(!running)
38 server->start();
39 running=true;
40 glutShowWindow();
42 /*! ignore rendering in zero sized windows */
43 if(!winWidth || !winHeight)
44 return;
45 try
47 OSG::commitChanges();
48 server->render(ract);
49 // clear changelist from prototypes
50 OSG::Thread::getCurrentChangeList()->clear();
52 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
54 if(exitOnError)
56 SLOG << e.what() << std::endl;
57 printf("Exit on error %s",e.what());
58 try
60 cleanup();
63 catch(...)
67 exit(0);
69 else
71 window->clearPorts();
72 // try to restart server
73 try
75 server->stop();
77 catch(...)
80 running=false;
81 glutHideWindow();
86 void reshape( int width, int height )
88 winWidth = width;
89 winHeight = height;
90 std::cout << "reshape " << width << " " << height << std::endl;
91 window->resize( width, height );
94 void key(unsigned char key, int /*x*/, int /*y*/)
96 switch ( key )
98 case 'd':
99 window->getPort(0)->getRoot()->dump();
100 break;
101 case 's':
102 OSG::SceneFileHandler::the()->write(
103 window->getPort(0)->getRoot(),"server.osg");
104 cleanup();
105 exit(0);
106 break;
110 int main(int argc,char **argv)
112 const char *name ="ClusterServer";
113 const char *connectionType="StreamSock";
114 // const char *connectionType="Multicast";
115 bool fullscreen =true;
116 std::string address ="";
117 int width=-1,height=300,x=0,y=0;
118 bool doStereo=false;
119 char *str;
121 for(int i = 1 ; i < argc ; ++i)
123 if(argv[i][0] == '-')
125 switch(argv[i][1])
127 case 'm':
128 connectionType="Multicast";
129 break;
130 case 's':
131 doStereo=true;
132 break;
133 case 'w':
134 fullscreen=false;
135 break;
136 case 'e':
137 exitOnError=true;
138 break;
139 case 'a':
140 address=&(argv[i][2]);
141 break;
142 case 'g':
143 if(argv[i][2] != '\0')
144 str=argv[i]+2;
145 else
146 str=argv[++i];
147 if(sscanf(str,"%d,%d,%d,%d",
148 &width,&height,&x,&y)!=4)
150 SWARNING << "Wrong args in -g. Use -gw,h,x,y"
151 << std::endl;
152 cleanup();
153 exit(0);
155 break;
156 case 'p':
157 if(argv[i][2] != '\0')
158 servicePort=atoi(argv[i]+2);
159 else
160 servicePort=atoi(argv[++i]);
161 break;
162 case 'j':
163 if(argv[i][2] != '\0')
164 serviceGroup=argv[i]+2;
165 else
166 serviceGroup=argv[++i];
167 break;
168 case 'h':
169 std::cout << argv[0]
170 << "-m "
171 << "-s "
172 << "-w "
173 << "-e "
174 << "-g w,h,x,y "
175 << "-a Address "
176 << "-j group "
177 << "-p servicePort "
178 << std::endl;
179 std::cout << "-m use multicast" << std::endl;
180 std::cout << "-s enable stereo" << std::endl;
181 std::cout << "-w no fullscreen" << std::endl;
182 std::cout << "-e exit after closed connection"
183 << std::endl;
184 std::cout << "-g geometry" << std::endl;
185 std::cout << "-a Address Server network address"
186 << std::endl;
187 std::cout << "-m Address wait for requests on "
188 << "multicast group" << std::endl;
189 std::cout << "-p port wait for requests on port"
190 << std::endl;
191 return 0;
194 else
196 name=argv[i];
202 OSG::osgInit(argc, argv);
203 glutInit(&argc, argv);
204 if(doStereo)
205 glutInitDisplayMode( GLUT_STEREO |
206 GLUT_RGB |
207 GLUT_DEPTH |
208 GLUT_DOUBLE);
209 else
210 glutInitDisplayMode( GLUT_RGB |
211 GLUT_DEPTH |
212 GLUT_DOUBLE);
213 glutInitWindowPosition(x,y);
214 if(width>0)
215 glutInitWindowSize(width,height);
216 winid = glutCreateWindow("OpenSG Cluster Client");
217 if(fullscreen)
218 glutFullScreen();
220 else
221 glutReshapeWindow(300,300);
223 if(width>0)
224 glutReshapeWindow(width,height);
225 glutPositionWindow(x, y);
227 glutPopWindow();
228 glutDisplayFunc(display);
229 glutIdleFunc(display);
230 glutKeyboardFunc(key);
231 glutReshapeFunc(reshape);
232 glutSetWindowTitle(name);
233 // switch off cursor
234 glutSetCursor(GLUT_CURSOR_NONE);
235 glEnable( GL_LIGHTING );
236 glEnable( GL_LIGHT0 );
237 glEnable( GL_DEPTH_TEST );
238 glEnable( GL_NORMALIZE );
239 ract=OSG::RenderAction::create();
240 // ract->setFrustumCulling(false);
241 window = OSG::GLUTWindow::create();
242 window->setGlutId(winid);
243 window->init();
244 server = new OSG::ClusterServer(window,
245 name,
246 connectionType,
247 address,
248 servicePort,
249 serviceGroup);
250 server->start();
251 running=true;
252 glutMainLoop();
254 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
256 SLOG << e.what() << std::endl;
257 delete server;
258 cleanup();
259 OSG::osgExit();
261 return 0;