CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
main.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include <iostream>
34 #include <math.h>
35 #include <cstdlib>
36 #include <cstdio>
37 
38 #include <QApplication>
39 #include <QString>
40 #include "cxImageServer.h"
41 #include "cxImageStreamerOpenCV.h"
43 #include "cxStringHelpers.h"
44 #include "cxLogger.h"
45 #include "cxReporter.h"
46 
47 
48 #ifdef WIN32
49 #include <windows.h>
50 //#include <tchar.h>
51 #endif
52 
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 
57 #ifdef WIN32
58 // Catch ctrl + c on windows: http://www.cplusplus.com/forum/beginner/1501/
59 BOOL WINAPI ConsoleHandler(
60  DWORD dwCtrlType // control signal type
61 );
62 
63 BOOL WINAPI ConsoleHandler(DWORD CEvent)
64 {
65 // char mesg[128];
66 
67  switch(CEvent)
68  {
69  case CTRL_C_EVENT:
70  //MessageBox(NULL,_T("CTRL+C received!"),_T("CEvent"),MB_OK);
71  break;
72  case CTRL_BREAK_EVENT:
73  //MessageBox(NULL, _T("CTRL+BREAK received!"),_T("CEvent"),MB_OK);
74  break;
75  case CTRL_CLOSE_EVENT:
76  //MessageBox(NULL,_T("Program being closed!"),_T("CEvent"),MB_OK);
77  break;
78  case CTRL_LOGOFF_EVENT:
79  //MessageBox(NULL,_T("User is logging off!"),_T("CEvent"),MB_OK);
80  break;
81  case CTRL_SHUTDOWN_EVENT:
82  //MessageBox(NULL,_T("User is logging off!"),_T("CEvent"),MB_OK);
83  break;
84 
85  }
86  qApp->quit();
87  return TRUE;
88 }
89 #endif
90 
91 #ifndef WIN32
92 void my_handler(int s)
93 {
94 // printf("Caught signal %d\n",s);
95  qApp->quit();
96 }
97 #endif
98 
99 int main(int argc, char* argv[])
100 {
101 #ifdef WIN32
102  if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
103  {
104  // unable to install handler...
105  // display message to the user
106  printf("Unable to install handler!\n");
107  //return -1;
108  }
109 
110 #endif
111 #ifndef WIN32
112  // nice shutdown of app
113  // http://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c
114  struct sigaction sigIntHandler;
115 
116  sigIntHandler.sa_handler = my_handler;
117  sigemptyset(&sigIntHandler.sa_mask);
118  sigIntHandler.sa_flags = 0;
119 
120  sigaction(SIGINT, &sigIntHandler, NULL);
121 #endif
122 
123  QApplication app(argc, argv);
124  app.setOrganizationName("SINTEF");
125  app.setOrganizationDomain("www.sintef.no");
126  app.setApplicationName("OpenIGTLinkServer");
127  //------------------------------------------------------------
128  // Parse Arguments
129  cx::StringMap args = cx::extractCommandlineOptions(app.arguments());
131 
132  cx::ImageServer server;
133  if (args.count("help") || args.count("h"))
134  {
135  server.printHelpText();
136  return 0;
137  }
138 
139  int port = cx::convertStringWithDefault(args["port"], 18333);
140 
141  bool ok = server.initialize();
142  if (!ok)
143  {
144  std::cout << "Can not start streaming. Quitting application" << std::endl;
145  return false;
146  }
147  ok = server.startListen(port);
148  if (!ok)
149  {
150  std::cout << "Can not start listening. Quitting..." << std::endl;
151  return false;
152  }
153 
154  int retVal = app.exec();
156  return retVal;
157 
158 }
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:96
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:83
int main(int argc, char *argv[])
Definition: main.cpp:37
ImageServer.
Definition: cxImageServer.h:55
std::map< QString, QString > StringMap
static void printHelpText()
bool startListen(int port)
int convertStringWithDefault(QString text, int def)
StringMap extractCommandlineOptions(QStringList cmdline)