Main.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 
00023 
00024 #include <signal.h>
00025 #include <mcs.hh>
00026 using namespace mcs;
00027 
00028 static bool sigreceived;
00029 
00030 void handle_signal(int signum)
00031 {
00032   cout << "Signal " << signum << " received" << endl;
00033   sigreceived = true;
00034 }
00035 
00036 
00037 void set_signal()
00038 {
00039   signal(SIGINT,  handle_signal);   /* Interrupt (ANSI).    */
00040   signal(SIGQUIT, handle_signal);   /* Quit (POSIX).        */
00041   signal(SIGTERM, handle_signal);   /* Termination (ANSI).  */
00042   signal(SIGPIPE, handle_signal);       /* Broken pipe          */
00043 }
00044 
00045 
00046 void reset_signal()
00047 {
00048   signal(SIGINT,  SIG_DFL); /* Interrupt (ANSI).    */
00049   signal(SIGQUIT, SIG_DFL); /* Quit (POSIX).        */
00050   signal(SIGTERM, SIG_DFL); /* Termination (ANSI).  */
00051   signal(SIGPIPE, SIG_DFL);     /* Broken pipe          */
00052 }
00053 
00054 
00055 //Env* mcs::mcsStart(string appname, string inipath,
00056 //         Server* (*cb_newServer)(Env*))
00057 //{
00058 //  Env* env = NULL;
00059 //
00060 //  //We handle signals so the server can close all files and
00061 //  //connections still open.
00062 //  set_signal();
00063 //  sigreceived = false;
00064 //
00065 //  try {
00066 //    if (inipath.empty())
00067 //      env = new Env(appname);
00068 //    else
00069 //      env = new Env(appname, inipath);
00070 //
00071 //    if (cb_newServer) {
00072 //      env->server = (*cb_newServer)(env);
00073 //    }
00074 //    else
00075 //      env->server = new Server(env);
00076 //
00077 //    //Start the server.
00078 //    env->server->start();
00079 //  }
00080 //
00081 //  catch (Event e) {
00082 //    cerr << e.msg() << endl;
00083 //
00084 //    if (env) delete env;
00085 //    return NULL;
00086 //  }
00087 //
00088 //  return env;
00089 //}
00090 //
00091 //
00092 //void mcs::mcsWait(Env* env)
00093 //{
00094 //  if (! env)
00095 //    return;
00096 //
00097 //  //env->server->allowTermination();
00098 //
00099 //  while ( (! sigreceived)   &&   (env->server->state() <= MCS_STATE_RUNNING))
00100 //    sleep_ms(500);
00101 //  
00102 //  env->server->stop();
00103 //  delete env->server;
00104 //
00105 //  reset_signal();
00106 //  delete env;
00107 //}
00108 
00109 
00110 
00111 
00112 
00113 Env* mcs::mcsStart(string appname, string inipath,
00114            Server* (*cb_newServer)(Env*))
00115 {
00116   Env* env = NULL;
00117 
00118   try {
00119     if (inipath.empty())
00120       env = new Env(appname);
00121     else
00122       env = new Env(appname, inipath);
00123     env-> server = NULL;
00124 
00125     if (chdir(env->path.c_str()) != 0)
00126       throw MCS_ERROR(MSG_DIRECTORY_NOT_FOUND, env->path);
00127 
00128     int ret = daemon(1, (env->cl_logstdout   ?   1   :   0));
00129     if (ret != 0)
00130       throw MCS_ERROR(MSG_UNEXPECTED);
00131 
00132     //We handle signals so the server can close all files and
00133     //connections still open.
00134     set_signal();
00135     sigreceived = false;
00136 
00137     if (cb_newServer)
00138       env->server = (*cb_newServer)(env);
00139     else
00140       env->server = new Server(env);
00141 
00142     //Start the server.
00143     env->server->start();
00144 
00145     //Wait for a signal or the server termination
00146     while ( (! sigreceived)   &&   (env->server->state() <= MCS_STATE_RUNNING))
00147       sleep_ms(500);
00148     
00149     env->server->stop();
00150     reset_signal();
00151   }
00152   catch (Event e) {
00153     cerr << e.msg() << endl;
00154   }
00155 
00156   if (env->server)  delete env->server;
00157   if (env)          delete env;
00158 
00159   return EXIT_SUCCESS;
00160 }
00161 
00162 

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012