| [404d2b] | 1 | /*
 | 
|---|
 | 2 |  * Project: JobMarket
 | 
|---|
 | 3 |  * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
 | 
|---|
 | 4 |  * Copyright (C)  2011-2012 Frederik Heber. All rights reserved.
 | 
|---|
 | 5 |  * 
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * \file server_main.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  * This file strongly follows the Serialization example from the boost::asio
 | 
|---|
 | 12 |  * library (see server.cpp)
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *  Created on: Oct 21, 2011
 | 
|---|
 | 15 |  *      Author: heber
 | 
|---|
 | 16 |  */
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | // include config.h
 | 
|---|
 | 19 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 20 | #include <config.h>
 | 
|---|
 | 21 | #endif
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | // boost asio needs specific operator new
 | 
|---|
 | 24 | #include <boost/asio.hpp>
 | 
|---|
 | 25 | // program_options must have some strange static stuff, causes double free or
 | 
|---|
 | 26 | // corruption if included after MemDebug
 | 
|---|
 | 27 | #include <boost/program_options.hpp>
 | 
|---|
 | 28 | 
 | 
|---|
| [9eb71b3] | 29 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [404d2b] | 30 | 
 | 
|---|
 | 31 | #include <cstdlib>
 | 
|---|
 | 32 | #include <iostream>
 | 
|---|
 | 33 | #include <vector>
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 36 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 37 | #include "JobMarket/atexit.hpp"
 | 
|---|
 | 38 | #include "JobMarket/FragmentScheduler.hpp"
 | 
|---|
 | 39 | #include "JobMarket/Jobs/SystemCommandJob.hpp"
 | 
|---|
 | 40 | #include "JobMarket/ServerOptions.hpp"
 | 
|---|
 | 41 | #include "JobMarket/SignalHandler.hpp"
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | int server_main(int argc, char* argv[])
 | 
|---|
 | 44 | {
 | 
|---|
 | 45 |   // from this moment on, we need to be sure to deinitialize in the correct order
 | 
|---|
 | 46 |   // this is handled by the cleanup function
 | 
|---|
 | 47 |   atexit(cleanUp);
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |   ServerOptions ServerOpts;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |   // Declare the supported options.
 | 
|---|
 | 52 |   boost::program_options::options_description desc("Allowed options");
 | 
|---|
 | 53 |   desc.add_options()
 | 
|---|
 | 54 |       ("help,h", "produce help message")
 | 
|---|
 | 55 |       ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
 | 
|---|
 | 56 |       ("signal", boost::program_options::value< std::vector<size_t> >(), "set signal to catch (can be given multiple times)")
 | 
|---|
 | 57 |       ("workerport", boost::program_options::value< unsigned short >(), "listen on this port for connecting workers")
 | 
|---|
 | 58 |       ("controllerport", boost::program_options::value< unsigned short >(), "listen on this port for connecting controller")
 | 
|---|
 | 59 |       ("timeout", boost::program_options::value< size_t >(), "timeout in seconds for alive status of workers")
 | 
|---|
 | 60 |   ;
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |   boost::program_options::variables_map vm;
 | 
|---|
 | 63 |   boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
 | 
|---|
 | 64 |   boost::program_options::notify(vm);
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |   int status = 0;
 | 
|---|
 | 67 |   status = ServerOpts.parseHelp(vm, desc);
 | 
|---|
 | 68 |   if (status) return status;
 | 
|---|
 | 69 |   status = ServerOpts.parseVerbosity(vm);
 | 
|---|
 | 70 |   if (status) return status;
 | 
|---|
 | 71 |   status = ServerOpts.parseWorkerPort(vm);
 | 
|---|
 | 72 |   if (status) return status;
 | 
|---|
 | 73 |   status = ServerOpts.parseControllerPort(vm);
 | 
|---|
 | 74 |   if (status) return status;
 | 
|---|
 | 75 |   status = ServerOpts.parseTimeout(vm);
 | 
|---|
 | 76 |   if (status) return status;
 | 
|---|
 | 77 |   status = ServerOpts.parseSignals(vm);
 | 
|---|
 | 78 |   if (status) return status;
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 |   size_t Exitflag = 0;
 | 
|---|
 | 81 |   try
 | 
|---|
 | 82 |   {
 | 
|---|
 | 83 |     boost::asio::io_service io_service;
 | 
|---|
 | 84 |     FragmentScheduler Server(io_service, ServerOpts.workerport, ServerOpts.controllerport, ServerOpts.timeout);
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |     // catch ctrl-c and shutdown worker properly
 | 
|---|
 | 87 |     boost::function<void (int)> shutdownfunction = boost::bind(&FragmentScheduler::shutdown, boost::ref(Server), _1);
 | 
|---|
 | 88 |     SignalHandler signalhandler(shutdownfunction, ServerOpts.signals);
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |     {
 | 
|---|
 | 91 |       Info info("io_service");
 | 
|---|
 | 92 |       io_service.run();
 | 
|---|
 | 93 |     }
 | 
|---|
 | 94 |     Exitflag = Server.getExitflag();
 | 
|---|
 | 95 |   }
 | 
|---|
 | 96 |   catch (std::exception& e)
 | 
|---|
 | 97 |   {
 | 
|---|
 | 98 |     std::cerr << e.what() << std::endl;
 | 
|---|
 | 99 |   }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   return Exitflag;
 | 
|---|
 | 102 | }
 | 
|---|