MCS  0.3.3-alpha7
Main.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 
24 #include <signal.h>
25 #include <mcs.hh>
26 using namespace mcs;
27 
28 static bool sigreceived;
29 
30 void handle_signal(int signum)
31 {
32  cout << "Signal " << signum << " received" << endl;
33  sigreceived = true;
34 }
35 
36 
37 void set_signal()
38 {
39  signal(SIGINT, handle_signal); /* Interrupt (ANSI). */
40  signal(SIGQUIT, handle_signal); /* Quit (POSIX). */
41  signal(SIGTERM, handle_signal); /* Termination (ANSI). */
42  signal(SIGPIPE, handle_signal); /* Broken pipe */
43 }
44 
45 
46 void reset_signal()
47 {
48  signal(SIGINT, SIG_DFL); /* Interrupt (ANSI). */
49  signal(SIGQUIT, SIG_DFL); /* Quit (POSIX). */
50  signal(SIGTERM, SIG_DFL); /* Termination (ANSI). */
51  signal(SIGPIPE, SIG_DFL); /* Broken pipe */
52 }
53 
54 
55 //Env* mcs::mcsStart(string appname, string inipath,
56 // Server* (*cb_newServer)(Env*))
57 //{
58 // Env* env = NULL;
59 //
60 // //We handle signals so the server can close all files and
61 // //connections still open.
62 // set_signal();
63 // sigreceived = false;
64 //
65 // try {
66 // if (inipath.empty())
67 // env = new Env(appname);
68 // else
69 // env = new Env(appname, inipath);
70 //
71 // if (cb_newServer) {
72 // env->server = (*cb_newServer)(env);
73 // }
74 // else
75 // env->server = new Server(env);
76 //
77 // //Start the server.
78 // env->server->start();
79 // }
80 //
81 // catch (Event e) {
82 // cerr << e.msg() << endl;
83 //
84 // if (env) delete env;
85 // return NULL;
86 // }
87 //
88 // return env;
89 //}
90 //
91 //
92 //void mcs::mcsWait(Env* env)
93 //{
94 // if (! env)
95 // return;
96 //
97 // //env->server->allowTermination();
98 //
99 // while ( (! sigreceived) && (env->server->state() <= MCS_STATE_RUNNING))
100 // sleep_ms(500);
101 //
102 // env->server->stop();
103 // delete env->server;
104 //
105 // reset_signal();
106 // delete env;
107 //}
108 
109 
110 
111 
112 
113 Env* mcs::mcsStart(string appname, string inipath,
114  Server* (*cb_newServer)(Env*))
115 {
116  Env* env = NULL;
117 
118  try {
119  if (inipath.empty())
120  env = new Env(appname);
121  else
122  env = new Env(appname, inipath);
123  env-> server = NULL;
124 
125  if (chdir(env->path.c_str()) != 0)
126  throw MCS_ERROR(MSG_DIRECTORY_NOT_FOUND, env->path);
127 
128  int ret = daemon(1, (env->cl_logstdout ? 1 : 0));
129  if (ret != 0)
130  throw MCS_ERROR(MSG_UNEXPECTED);
131 
132  //We handle signals so the server can close all files and
133  //connections still open.
134  set_signal();
135  sigreceived = false;
136 
137  if (cb_newServer)
138  env->server = (*cb_newServer)(env);
139  else
140  env->server = new Server(env);
141 
142  //Start the server.
143  env->server->start();
144 
145  //Wait for a signal or the server termination
146  while ( (! sigreceived) && (env->server->state() <= MCS_STATE_RUNNING))
147  sleep_ms(500);
148 
149  env->server->stop();
150  reset_signal();
151  }
152  catch (Event e) {
153  cerr << e.msg() << endl;
154  }
155 
156  if (env->server) delete env->server;
157  if (env) delete env;
158 
159  return EXIT_SUCCESS;
160 }
161 
162 
void start()
Start a new thread in the joinable state.
Definition: Thread.cc:378
void stop()
Stop thread execution.
Definition: Thread.cc:507
Main server class for a MCS-based application.
Definition: mcs.hh:7166
Hold informations about an event.
Definition: mcs.hh:814
string path
Main server path (APPD).
Definition: mcs.hh:6929
void sleep_ms(unsigned int millisec)
A millisecond resolution sleep function.
Definition: Thread.cc:603
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
string msg()
Returns the message.
Definition: Event.cc:104
Main include file for all MCS based applications.
Env * mcsStart(string appname, string inipath="", Server *(*cb_newServer)(Env *)=NULL)
Start a MCS server.
Definition: Main.cc:113
bool cl_logstdout
Copy log on stdout.
Definition: mcs.hh:6970
#define MCS_STATE_RUNNING
Thread state: the separate thread is executing the run() method.
Definition: mcs.hh:2409
Hold all environment variables.
Definition: mcs.hh:6867
int state()
Return the state of the thread.
Definition: Thread.cc:307
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018