1
0

AlarmServer.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "MailServer/Server.h"
  2. #include <Logging.h>
  3. #include <asio.hpp>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <iostream>
  8. int main(int argc, char* argv[])
  9. {
  10. try
  11. {
  12. Logging::OpenLog();
  13. Logging::SetLogMask(Logging::Severity::Info);
  14. /* error checking elided for brevity */
  15. int fd = ::open("/dev/null", O_WRONLY);
  16. ::dup2(fd, 2);
  17. ::close(fd);
  18. unsigned int port;
  19. std::string path;
  20. std::string url;
  21. if (argc == 3)
  22. {
  23. port = std::atoi(argv[1]);
  24. path = std::string(argv[2]);
  25. }
  26. else if (argc == 4)
  27. {
  28. port = std::atoi(argv[1]);
  29. path = std::string(argv[2]);
  30. url = std::string(argv[3]);
  31. }
  32. else
  33. {
  34. std::cout << "Usage: AlarmServer <port> <path> [url]\n";
  35. return 1;
  36. }
  37. Logging::Log(Logging::Severity::Info, "Starting AlarmServer");
  38. asio::io_service io_service;
  39. MailServer::Server s(io_service, port, path, url);
  40. io_service.run();
  41. Logging::Log(Logging::Severity::Info, "Stopping AlarmServer...");
  42. Logging::CloseLog();
  43. }
  44. catch (std::exception& e)
  45. {
  46. std::stringstream ss;
  47. ss << "ERROR: " << e.what() << std::endl;
  48. std::cerr << ss.str() << std::endl;
  49. Logging::Log(Logging::Severity::Error, ss.str());
  50. Logging::CloseLog();
  51. return -1;
  52. }
  53. return 0;
  54. }