00001 /** 00002 * @file clihandler.h 00003 * @author Reshen Amin <reshen@zensrc.com> 00004 * 00005 * @brief Contains the class definition for kicking off the program 00006 * 00007 * @license Byzantine Generals Problem Coding Sample.<br> 00008 * Copyright (C) 2010 Reshen Amin.<br> 00009 * See @ref license_sec for details.<br> 00010 */ 00011 00012 #ifndef SRC_CLIHANDLER_H_ 00013 #define SRC_CLIHANDLER_H_ 00014 00015 #include <boost/interprocess/managed_shared_memory.hpp> 00016 00017 #include <log4cpp/Category.hh> 00018 #include <vector> 00019 00020 #include "sharedmemtypes.h" 00021 00022 /** 00023 * @brief Main's potential return codes on program exit 00024 */ 00025 namespace ByzGeneralsReturnCodes { 00026 /** 00027 * @brief See @ref ByzGeneralsReturnCodes 00028 */ 00029 enum type { 00030 kNoError = 0, /*!< No Error, execution completed fine. */ 00031 kHelpExitNoError, /*!< No Error, User asked for usage help. */ 00032 kInvalidInput, /*!< Invalid command line arguments received */ 00033 kIPCError, /*!< Interprocess Communication (IPC) Error */ 00034 kUnknown /*!< Unknown Error/Exception occured */ 00035 }; 00036 }; 00037 00038 class ByzantineGeneralThread; 00039 00040 /** 00041 * @brief This class is responsible for providing an execution harness into 00042 * the Byzantine Generals coding sample. It is intended to be called directly 00043 * from main(). 00044 */ 00045 class CLIHandler { 00046 public: 00047 /** 00048 * @brief Constructor 00049 */ 00050 CLIHandler(); 00051 00052 /** 00053 * @brief Destructor 00054 */ 00055 ~CLIHandler(); 00056 00057 /** 00058 * @brief Begin execution of the Byzantine General coding sample. This 00059 * function will not return until all of the threads which are created 00060 * are completed. 00061 * 00062 * @param argc Command Line argument count 00063 * @param argv Command Line argument values 00064 * 00065 * @return The result of parsing arguments and creating/executing of 00066 * the threads 00067 */ 00068 ByzGeneralsReturnCodes::type Exec(int argc, char* argv[]); 00069 00070 00071 private: 00072 /** 00073 * @brief Shared memory region name. This is the unique identifier 00074 * used to find the region of memory shared between the various 00075 * Byzantine General Threads. All shared objects are stored in this 00076 * region. 00077 */ 00078 static const char* kSharedMemoryName_; 00079 00080 /** 00081 * @brief Mailbox vector (in shared memory) name. See @ref Mailbox 00082 * for details on what each @ref Mailbox is used for. 00083 */ 00084 static const char* kMailboxVectorName_; 00085 00086 /** 00087 * @brief Traitor set (in shared memory) name. This is a simple set 00088 * used to track the Generals who are created as traitors. 00089 */ 00090 static const char* kTraitorSetName_; 00091 00092 /** 00093 * @brief Logging category name 00094 */ 00095 static const char* kLogCategoryName_; 00096 00097 /** 00098 * @brief Maximum size of shared memory region in bytes. We pick a 00099 * maximum memory area size sufficiently large to allow testing of up to 00100 * 4 traitors and 9 loyalists. This value can be increased arbitrairly 00101 * to test larger cases 00102 */ 00103 static const int kSharedMemoryMaxSize_ = 50000000; 00104 00105 /** 00106 * @brief Logging endpoint 00107 */ 00108 log4cpp::Category& log_; 00109 00110 /** 00111 * @brief Create a logging endpoint 00112 * 00113 * @return Logging endpoint reference 00114 */ 00115 log4cpp::Category& InitializeLogger(); 00116 00117 /** 00118 * @brief Parse and validate command line arguments (CLI) 00119 * 00120 * @param argc Command Line argument count 00121 * @param argv Command Line argument values 00122 * 00123 * @return Status of argument parsing 00124 */ 00125 ByzGeneralsReturnCodes::type ParseAndValidateArguments(int argc, 00126 char* argv[]); 00127 00128 /** 00129 * @brief Create the Byzantine General Threads. This function will 00130 * store created thread references into @ref byzantine_generals_ 00131 */ 00132 void CreateGeneralThreads(); 00133 00134 /** 00135 * @brief Destroy Byzantine General Threads. This function will 00136 * destroy any thread references stored in @ref byzantine_generals_ 00137 * Threads should be completed prior to calling this function. 00138 */ 00139 void DestroyGeneralThreads(); 00140 00141 /** 00142 * @brief Calculate the number of orders each general should expect 00143 * and the total number of rounds that those orders will be sent across. 00144 */ 00145 void CalculateOrdersAndRounds(); 00146 00147 /** 00148 * @brief Create shared memory region and the data structures within 00149 * the shared memory region (Traitor Set, Mailbox Vector, etc..) 00150 */ 00151 void CreateSharedMemoryItems(); 00152 00153 /** 00154 * @brief Destroy the shared memory region and all of its contents 00155 */ 00156 void DestroySharedMemoryItems(); 00157 00158 /** 00159 * @brief The number of rounds that communication will take place over. 00160 * This is always m + 1, where m is the number of traitors the user 00161 * specified. 00162 */ 00163 unsigned int number_rounds_; 00164 00165 /** 00166 * @brief User specified number of traitor (disloyal) generals 00167 */ 00168 unsigned int number_traitor_generals_; 00169 00170 /** 00171 * @brief User specified number of loyal generals 00172 */ 00173 unsigned int number_loyal_generals_; 00174 00175 /** 00176 * @brief User specified generation of DOT graphs, TRUE to generate 00177 * graphs, FALSE otherwise. 00178 */ 00179 bool generate_dot_graph_; 00180 00181 /** 00182 * @brief Calculated total number of general (always equal to the 00183 * number of loyal generals + number of disloyal generals). 00184 */ 00185 unsigned int total_number_generals_; 00186 00187 /** 00188 * @brief Calculated total number of messages that each Lieutenant 00189 * General should expect to receive. 00190 */ 00191 unsigned int total_messages_per_lt_general_; 00192 00193 /** 00194 * @brief This vector stores references to the created Byzantine 00195 * General Threads. It is populated by @ref CreateGeneralThreads and 00196 * cleared by @ref DestroyGeneralThreads 00197 */ 00198 std::vector<ByzantineGeneralThread *> byzantine_generals_; 00199 00200 /** 00201 * @brief Reference to the created shared memory region. It is 00202 * populated by @ref CreateSharedMemoryItems and destroyed by @ref 00203 * DestroySharedMemoryItems 00204 */ 00205 boost::interprocess::managed_shared_memory* segment_; 00206 00207 /** 00208 * @brief This set, which is stored in shared memory, keeps track of 00209 * the unique identifiers of each of the disloyal Byzantine General 00210 * Threads. It is populated by @ref CreateSharedMemoryItems and 00211 * destroyed by @ref DestroySharedMemoryItems 00212 */ 00213 SharedMemoryUIntSet* traitor_set_; 00214 00215 /** 00216 * @brief This vector stores the mailboxes for each of the Generals. 00217 * See @ref Mailbox for details. It is populated by @ref 00218 * CreateSharedMemoryItems and destroyed by @ref 00219 * DestroySharedMemoryItems 00220 */ 00221 SharedMemoryMailboxVector* mailbox_vector_; 00222 }; 00223 00224 #endif // SRC_CLIHANDLER_H_
1.6.3