main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "main.h"
  2. #include "websocket.h"
  3. #include "Debug.h"
  4. #include "error_signals.h"
  5. #include "test.h"
  6. int main( int argc, char *argv[] )
  7. {
  8. the_websocket = nullptr;
  9. // Start of error handling.
  10. // This is error handling to display the stack trace of a GPF error.
  11. // This code has to be done at the beginning of each thread.
  12. error_signals::AddHandlers();
  13. pid_t tid = error_signals::GetThreadID();
  14. error_signals *error_thread = error_signals::GetThread(tid);
  15. if (error_thread == nullptr)
  16. {
  17. error_thread = error_signals::AddThread(tid);
  18. }
  19. volatile int val = 0;
  20. if (error_thread != nullptr)
  21. {
  22. error_thread->LineNumberStack = 0;
  23. val = setjmp(error_thread->position);
  24. }
  25. if (val != 0)
  26. {
  27. // This code runs if there's an error.
  28. error_thread->DisplayErrorMessage(val); // Display a stack trace like C#.
  29. error_signals::RemoveHandlers();
  30. if (the_websocket != nullptr) {
  31. the_websocket->shutdown = true;
  32. }
  33. return val;
  34. }
  35. // End of error handling.
  36. Debug debug(__FILE__,__func__,__LINE__);
  37. the_websocket = new websocket();
  38. debug = __LINE__;
  39. int port;
  40. if (argc != 3) {
  41. printf("%s Port ServerPassword\n",argv[0]);
  42. printf(" starts the server listening to Port.\n");
  43. printf(" ServerPassword is the password to edit users/chatrooms.\n");
  44. printf("%s test classname\n",argv[0]);
  45. printf(" Runs the test for classname.\n");
  46. printf("%s test testnumber\n",argv[0]);
  47. printf(" testnumber can be from 1 to %d.\n",test::testcount());
  48. printf("%s test all\n",argv[0]);
  49. printf(" Runs all the tests.\n");
  50. printf("To stop the program, press CTRL C or CTRL Z.\n");
  51. printf("To stop the program running in the background, call kill -SIGTERM <pid>\n");
  52. printf(" <pid> is the process id from ps -e|grep %s\n",argv[0]);
  53. printf("As a last resort, use killall -9 %s\n",argv[0]);
  54. return 1;
  55. }
  56. debug = __LINE__;
  57. if (strcmp(argv[1],"test")==0) {
  58. port = test::runtest(argv[2]);
  59. return port;
  60. }
  61. debug = __LINE__;
  62. port = atoi(argv[1]);
  63. debug = __LINE__;
  64. if ((port <=0) || (port > 65535)) {
  65. printf("Error: Port must be between 1 and 65535.\n");
  66. return 2;
  67. }
  68. debug = __LINE__;
  69. struct lws_context_creation_info info;
  70. memset( &info, 0, sizeof(info) );
  71. info.port = port;
  72. info.protocols = protocols;
  73. info.gid = -1;
  74. info.uid = -1;
  75. debug = __LINE__;
  76. the_websocket->server_password = new datablock(argv[2]);
  77. debug = __LINE__;
  78. struct lws_context *context = lws_create_context( &info );
  79. debug = __LINE__;
  80. if (the_websocket->run_async) {
  81. // Start a separate thread for tasks.
  82. if (pthread_create(&the_websocket->task_thread,NULL,websocket::task_thread_routine,the_websocket->chatroom_tasks) != 0) {
  83. // Thread was unable to be created.
  84. the_websocket->run_async = false;
  85. }
  86. }
  87. debug = __LINE__;
  88. while( !the_websocket->shutdown )
  89. {
  90. lws_service( context, /* timeout_ms = */ 1000 );
  91. }
  92. debug = __LINE__;
  93. printf("%s is shutting down.\n",argv[0]);
  94. lws_context_destroy( context );
  95. delete the_websocket;
  96. error_thread->ReleaseThread(tid); // Error thread cleanup.
  97. return 0;
  98. }