CustusX  18.04
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include <iostream>
13 #include <math.h>
14 #include <cstdlib>
15 #include <cstdio>
16 
17 #include <QApplication>
18 #include <QString>
19 #include "cxImageServer.h"
20 #include "cxImageStreamerOpenCV.h"
22 #include "cxStringHelpers.h"
23 #include "cxLogger.h"
24 #include "cxReporter.h"
25 
26 
27 #ifdef WIN32
28 #include <windows.h>
29 //#include <tchar.h>
30 #endif
31 
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 
36 #ifdef WIN32
37 // Catch ctrl + c on windows: http://www.cplusplus.com/forum/beginner/1501/
38 BOOL WINAPI ConsoleHandler(
39  DWORD dwCtrlType // control signal type
40 );
41 
42 BOOL WINAPI ConsoleHandler(DWORD CEvent)
43 {
44 // char mesg[128];
45 
46  switch(CEvent)
47  {
48  case CTRL_C_EVENT:
49  //MessageBox(NULL,_T("CTRL+C received!"),_T("CEvent"),MB_OK);
50  break;
51  case CTRL_BREAK_EVENT:
52  //MessageBox(NULL, _T("CTRL+BREAK received!"),_T("CEvent"),MB_OK);
53  break;
54  case CTRL_CLOSE_EVENT:
55  //MessageBox(NULL,_T("Program being closed!"),_T("CEvent"),MB_OK);
56  break;
57  case CTRL_LOGOFF_EVENT:
58  //MessageBox(NULL,_T("User is logging off!"),_T("CEvent"),MB_OK);
59  break;
60  case CTRL_SHUTDOWN_EVENT:
61  //MessageBox(NULL,_T("User is logging off!"),_T("CEvent"),MB_OK);
62  break;
63 
64  }
65  qApp->quit();
66  return TRUE;
67 }
68 #endif
69 
70 #ifndef WIN32
71 void my_handler(int s)
72 {
73 // printf("Caught signal %d\n",s);
74  qApp->quit();
75 }
76 #endif
77 
78 int main(int argc, char* argv[])
79 {
80 #ifdef WIN32
81  if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
82  {
83  // unable to install handler...
84  // display message to the user
85  printf("Unable to install handler!\n");
86  //return -1;
87  }
88 
89 #endif
90 #ifndef WIN32
91  // nice shutdown of app
92  // http://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c
93  struct sigaction sigIntHandler;
94 
95  sigIntHandler.sa_handler = my_handler;
96  sigemptyset(&sigIntHandler.sa_mask);
97  sigIntHandler.sa_flags = 0;
98 
99  sigaction(SIGINT, &sigIntHandler, NULL);
100 #endif
101 
102  QApplication app(argc, argv);
103  app.setOrganizationName("SINTEF");
104  app.setOrganizationDomain("www.sintef.no");
105  app.setApplicationName("OpenIGTLinkServer");
106  //------------------------------------------------------------
107  // Parse Arguments
108  cx::StringMap args = cx::extractCommandlineOptions(app.arguments());
110 
111  cx::ImageServer server;
112  if (args.count("help") || args.count("h"))
113  {
114  server.printHelpText();
115  return 0;
116  }
117 
118  int port = cx::convertStringWithDefault(args["port"], 18333);
119 
120  bool ok = server.initialize();
121  if (!ok)
122  {
123  std::cout << "Can not start streaming. Quitting application" << std::endl;
124  return false;
125  }
126  ok = server.startListen(port);
127  if (!ok)
128  {
129  std::cout << "Can not start listening. Quitting..." << std::endl;
130  return false;
131  }
132 
133  int retVal = app.exec();
135  return retVal;
136 
137 }
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:75
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:62
ImageServer.
Definition: cxImageServer.h:34
std::map< QString, QString > StringMap
static void printHelpText()
int main(int argc, char *argv[])
Definition: main.cpp:16
bool startListen(int port)
int convertStringWithDefault(QString text, int def)
StringMap extractCommandlineOptions(QStringList cmdline)