Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsFsDaemonLoader.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsFsDaemonLoader.cpp
4  *
5  * Description:
6  *
7  * Created: 02/14/12 11:58:54
8  *
9  * Author: Benoit Daccache, ben.daccache@gmail.com
10  *
11  */
12 
13 
14 #include "WsFsDaemonLoader.h"
15 #include <unistd.h>
16 #include <iostream>
17 
18 #include <Logger/WsCrashLogger.h>
19 
21  m_props(props),
22  m_daemon(bdaemon)
23 {
24 }
25 
27 {
28  LOG(DEBUG) << "WsFsDaemonLoader::~WsFsDaemonLoader() : deleting this";
29  delete m_server;
30 }
31 
33 {
34  if (!m_daemon) {
36  int nworkers = boost::lexical_cast<int>(m_props->get("global", "num_workers", "1"));
37  return m_server->bind(nworkers);
38  } else
39  return daemonize();
40 }
41 
43 {
44  pid_t pid = fork();
45  if (pid == 0) {
47  return m_server->bind();
48  } else {
49  exit(0);
50  }
51 }
52 
53 
54 void usage()
55 {
56  cout << "Usage : " << endl;
57  cout << "[-h | --host] value\t: connect on host" << endl;
58  cout << "[-p | --port] value\t: connect on port" << endl;
59  cout << "[-w | --nworkers] value\t: number of workers" << endl;
60  cout << "[--protocol] value\t: use protocol" << endl;
61  cout << "[--daemonize]\t\t: run server in background" << endl;
62  cout << "[--root]\t\t: Filesystem root path" << endl;
63  cout << "[--help]\t\t: display this help message" << endl;
64  cout << "[--pid] value\t\t: write PID in file" << endl;
65  exit(0);
66 }
67 
69 
70 void handle_int(int sig)
71 {
72  LOG(INFO) << "WsFsDaemonLoader::handle_int() : Stopping server";
73  delete serverLoader;
74  exit(sig);
75 }
76 
77 
78 int main(int argc, char** argv)
79 {
80  signal( SIGINT, &handle_int);
81  signal( SIGSEGV, &WsCrashLogger::handleSignal);
82  signal( SIGABRT, &WsCrashLogger::handleSignal);
83  signal( SIGILL, &WsCrashLogger::handleSignal);
84  signal( SIGFPE, &WsCrashLogger::handleSignal);
86  static struct option long_options[] = {
87  {"host", required_argument, NULL, 'h'},
88  {"port", required_argument, NULL, 'p'},
89  {"nworkers", required_argument, NULL, 'w'},
90  {"daemonize", no_argument, NULL, 0},
91  {"protocol", required_argument, NULL, 1},
92  {"help", no_argument, NULL, 2},
93  {"root", required_argument, NULL, 3},
94  {"pid", required_argument, NULL, 4},
95  {NULL, 0, NULL, 0}
96  };
97  int option_index = 0;
98  bool bdaemon = false;
99  bool bpid = false;
100  std::string pidPath = "";
101  char c;
102  while ((c = getopt_long (argc, argv, "h:p:w:01:", long_options, &option_index)) != -1) {
103  switch (c) {
104  case 'h':
105  props->set("global", "host", string((char*)optarg));
106  break;
107  case 'p':
108  props->set("global", "port", string((char*)optarg));
109  break;
110  case 'w':
111  props->set("global", "num_workers", string((char*)optarg));
112  break;
113  case 0:
114  bdaemon = true;
115  break;
116  case 1:
117  props->set("global", "protocol", string((char*)optarg));
118  break;
119  case 2:
120  usage();
121  break;
122  case 3:
123  props->set("global", "root_path", string((char*)optarg));
124  break;
125  case 4:
126  bpid = true;
127  pidPath = string((char*)optarg);
128  break;
129  default:
130  usage();
131  exit(-1);
132  }
133  }
134  /* Set apache www-data uid */
135  /* setuid(33);*/
136  serverLoader = new WsFsDaemonLoader(props, bdaemon);
137  if (bpid) {
138  ofstream myfile;
139  myfile.open (pidPath.c_str());
140  myfile << getpid();
141  myfile.close();
142  }
143  /* Init the random number generator */
144  srand (time(NULL));
145  if (serverLoader->start() == ErrorCode::Success)
146  return EXIT_SUCCESS;
147  else return EXIT_FAILURE;
148 }
149 
DaemonStatus bind(unsigned int numWorkers=1)
start listening on the port
Definition: WsFsDaemon.cpp:83
#define DEBUG
Definition: WsLogger.h:27
Global properties class.
int main(int argc, char **argv)
int daemonize()
daemonize the server, fork and put it in backgroud
static void handleSignal(int signum)
Loads the FsDaemon depending on the params passed as args.
void usage()
Main class of the FsDaemon.
Definition: WsFsDaemon.h:44
int start()
launch the server
#define LOG
Definition: WsLogger.h:22
bool m_daemon
daemonize or not
#define INFO
Definition: WsLogger.h:32
WsFsDaemonLoader * serverLoader
std::string get(const std::string &section, const std::string &id, const std::string &def)
WsGlobalProperties * m_props
static WsGlobalProperties * instance()
WsFsDaemonLoader(WsGlobalProperties *props, bool daemon=false)
Constructor.
WsFsDaemon * m_server
const int Success
void set(const std::string &section, std::string key, std::string value)
Changes or sets the propety with key 'key' to value 'value' If the property already exist...
~WsFsDaemonLoader()
destructor
void handle_int(int sig)