Reorganize to remove DIKU specific naming
[remote/remote-mci.git] / mcs / moteserver.cc
blobf945607a12922c45fe4f6954ada9c616fcbdea57
1 #include "FileDescriptor.h"
2 #include "SessionListener.h"
3 #include "HostListener.h"
4 #include "database.h"
5 #include "Configuration.h"
6 #include "MMSException.h"
7 #include "macros.h"
8 #include <exception>
9 #include <unistd.h>
10 #include "libutil/File.h"
12 using namespace remote;
13 using namespace remote::mcs;
14 using namespace remote::util;
16 void handleExit()
18 log("Shutting down\n");
19 remove(Configuration::vm["pidFile"].as<std::string>().c_str());
22 int main(int argc,char** argv)
24 std::ostringstream oss;
25 std::string pid;
27 Configuration::read(argc,argv);
29 if (Configuration::vm["daemonize"].as<int>())
31 printf("Daemonizing!\n");
32 if (fork()) exit(0);
33 setsid();
34 fclose(stdin);
35 close(1);
36 // reopen stdout
37 if (NULL == freopen(Configuration::vm["log-file"].as<std::string>().c_str(),"a",stdout))
39 fprintf(stderr,"Unable to open file %s for logging! Bailing out!\n",Configuration::vm["log-file"].as<std::string>().c_str());
40 return -1;
42 if (NULL == freopen(Configuration::vm["errorlog-file"].as<std::string>().c_str(),"a",stderr))
44 return -1;
48 log("Starting mote server\n");
50 atexit(handleExit);
52 oss << getpid() << std::endl;
53 pid = oss.str();
55 if (!File::writeFile(Configuration::vm["pidFile"].as<std::string>(),
56 pid.c_str(), pid.size()))
57 log("Failed to create .pid file\n");
59 do {
60 dbConn.connect( Configuration::vm["dbName"].as<std::string>(),
61 Configuration::vm["dbHost"].as<std::string>(),
62 Configuration::vm["dbUser"].as<std::string>(),
63 Configuration::vm["dbPassword"].as<std::string>());
64 log("Connected to database\n");
65 Mote::resetDb();
66 log("Deleted old mote data\n");
67 Session::resetDb();
68 log("Deleted old session data\n");
69 HostListener hostListener(Configuration::vm["hostListenerPort"].as<unsigned int>());
70 SessionListener sessionListener(Configuration::vm["sessionListenerPort"].as<unsigned int>());
71 log("Entering service loop\n");
73 } while (FileDescriptor::serviceLoop());