1 #include "FileDescriptor.h"
2 #include "SessionListener.h"
3 #include "HostListener.h"
5 #include "MMSException.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;
28 Log::info("Shutting down");
29 remove(pidFile
.c_str());
32 int main(int argc
,char** argv
)
34 std::ostringstream oss
;
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
))
59 printf("Daemonizing!\n");
65 if (!freopen(logFile
.c_str(), "a", stdout
)) {
66 fprintf(stderr
, "Unable to open file %s for logging! Bailing out!\n", logFile
.c_str());
69 if (!freopen(errorFile
.c_str(), "a", stderr
)) {
72 Log::open("remote-mcs", Log::INFO
, Log::SYSLOG
);
74 Log::open("remote-mcs", Log::INFO
, stdout
);
77 Log::info("Starting mote server");
81 oss
<< getpid() << std::endl
;
84 if (!File::writeFile(pidFile
, pid
.c_str(), pid
.size()))
85 Log::error("Failed to create .pid file");
88 dbConn
.connect(dbName
, dbHost
, dbUser
, dbPassword
);
89 Log::info("Connected to database");
91 Log::info("Deleted old mote data");
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();