MCS: Use remote.h and cleanup header file inclusion
[remote/remote-mci.git] / mcs / moteserver.cc
blob5f9f255a1f933b621d7f879da7ac38efbce1bea4
1 #include "FileDescriptor.h"
2 #include "SessionListener.h"
3 #include "HostListener.h"
4 #include "database.h"
5 #include "MMSException.h"
6 #include <exception>
7 #include <unistd.h>
8 #include "libutil/File.h"
9 #include "libutil/Config.h"
11 using namespace remote;
12 using namespace remote::mcs;
13 using namespace remote::util;
15 std::string dbName = "REMOTE";
16 std::string dbHost = "localhost";
17 std::string dbUser = "remote_admin";
18 std::string dbPassword = "remote";
19 std::string logFile = "/var/log/remote-mcs.log";
20 std::string errorFile = "/var/log/remote-mcs-error.log";
21 std::string pidFile = "/var/run/remote-mcs.pid";
22 uint16_t sessionPort = 10000;
23 uint16_t moteHostPort = 10001;
24 bool daemonize = false;
26 void handleExit()
28 Log::info("Shutting down");
29 remove(pidFile.c_str());
32 int main(int argc,char** argv)
34 std::ostringstream oss;
35 std::string pid;
36 bool eCode = true;
37 Config config("remote-mcs", "/etc/remote-mcs.cfg");
39 config("daemonize", &daemonize, "Run as daemon");
40 config("pidFile", &pidFile, "Path to PID file");
41 config("dbName", &dbName, "Database user name");
42 config("dbHost", &dbHost, "Database host name");
43 config("dbUser", &dbUser, "Database user name");
44 config("dbPassword", &dbPassword, "Database password");
45 config("sessionPort", &sessionPort, "Port number for sessions");
46 config("moteHostPort", &moteHostPort, "Port number for mote hosts");
47 config("logFile", &logFile, "Path to log file when running as a daemon");
48 config("errorFile", &errorFile, "Path to error file when running as a daemon");
50 config("sessionListenerPort", "sessionPort");
51 config("hostListenerPort", "moteHostPort");
52 config("log-file", "logFile");
53 config("errorlog-file", "errorFile");
55 if (!config.read(argc, argv))
56 return EXIT_FAILURE;
58 if (daemonize) {
59 printf("Daemonizing!\n");
60 if (fork()) exit(0);
61 setsid();
62 fclose(stdin);
63 close(1);
64 // reopen stdout
65 if (!freopen(logFile.c_str(), "a", stdout)) {
66 fprintf(stderr, "Unable to open file %s for logging! Bailing out!\n", logFile.c_str());
67 return -1;
69 if (!freopen(errorFile.c_str(), "a", stderr)) {
70 return -1;
72 Log::open("remote-mcs", Log::INFO, Log::SYSLOG);
73 } else {
74 Log::open("remote-mcs", Log::INFO, stdout);
77 Log::info("Starting mote server");
79 atexit(handleExit);
81 oss << getpid() << std::endl;
82 pid = oss.str();
84 if (!File::writeFile(pidFile, pid.c_str(), pid.size()))
85 Log::error("Failed to create .pid file");
87 do {
88 dbConn.connect(dbName, dbHost, dbUser, dbPassword);
89 Log::info("Connected to database");
90 Mote::resetDb();
91 Log::info("Deleted old mote data");
92 Session::resetDb();
93 Log::info("Deleted old session data");
94 HostListener hostListener(moteHostPort);
95 SessionListener sessionListener(sessionPort);
96 Log::info("Entering service loop");
97 eCode = FileDescriptor::serviceLoop();
98 } while (eCode);