00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _WIN32
00035 #include "config.h"
00036 #else
00037 #include "winconfig.h"
00038 #endif
00039
00040 #ifndef _WIN32
00041 #include <unistd.h>
00042 #include <sys/socket.h>
00043 #include <netdb.h>
00044 #include <netinet/in.h>
00045 #include <arpa/inet.h>
00046 #endif
00047 #include "include/acc.h"
00048 #include "include/agent.h"
00049 #include "include/mc_platform.h"
00050 #include "include/macros.h"
00051
00052 #define DEFAULT_HOSTNAME_LENGTH 200
00053 mc_platform_p
00054 mc_platform_Initialize(MCAgency_t agency, ChOptions_t* ch_options)
00055 {
00056 int i;
00057 #ifdef _WIN32
00058 WORD wVersionRequested = MAKEWORD(1,1);
00059 int nret;
00060 #endif
00061 struct hostent* localhost;
00062 char hostname[DEFAULT_HOSTNAME_LENGTH];
00063
00064 mc_platform_p mc_platform;
00065 ChInterp_t* interp;
00066
00067
00068 mc_platform = (mc_platform_p)malloc(sizeof(mc_platform_t));
00069 CHECK_NULL(mc_platform, agency->last_error = MC_ERR_MEMORY; return NULL);
00070
00071
00072 mc_platform->err = 0;
00073 mc_platform->default_agentstatus = agency->default_agentstatus;
00074
00075 for(i = 0; i < MC_THREAD_ALL; i++) {
00076 mc_platform->stack_size[i] = agency->stack_size[i];
00077 }
00078
00079 mc_platform->bluetooth = agency->bluetooth;
00080 mc_platform->agency = agency;
00081
00082
00083 #ifdef _WIN32
00084 nret = WSAStartup(wVersionRequested, &(mc_platform->wsaData));
00085 if (nret != 0) {
00086 printf("\nWSAStartup Error %d. %s:%d\n", nret, __FILE__,__LINE__);
00087 exit(0);
00088 }
00089 if (mc_platform->wsaData.wVersion != wVersionRequested) {
00090 printf("\nWrong Winsock Version %s:%d\n", __FILE__,__LINE__);
00091 exit(0);
00092 }
00093 #endif
00094
00095 gethostname(hostname, DEFAULT_HOSTNAME_LENGTH);
00096 if (strlen(hostname) < 1) {
00097 strcpy(hostname, "localhost");
00098 }
00099 localhost = gethostbyname(hostname);
00100 if(localhost == NULL) {
00101 #ifdef _WIN32
00102 printf("Fatal Error: %d %s:%d\n", WSAGetLastError(), __FILE__, __LINE__);
00103 #else
00104 fprintf(stderr, "Fatal Error %s:%d\n", __FILE__, __LINE__);
00105 #endif
00106
00107 }
00108 mc_platform->hostname = (char*)malloc(sizeof(char)*DEFAULT_HOSTNAME_LENGTH);
00109 CHECK_NULL(mc_platform->hostname, agency->last_error = MC_ERR_MEMORY;return NULL);
00110
00111 if(localhost == NULL)
00112 strcpy(mc_platform->hostname, "127.0.0.1");
00113 else if(localhost->h_name)
00114 strcpy(mc_platform->hostname, localhost->h_name);
00115 else
00116
00117 strcpy(mc_platform->hostname, inet_ntoa( *(struct in_addr*)localhost->h_addr) );
00118
00119 mc_platform->hostname = (char*)realloc( mc_platform->hostname, sizeof(char) * (strlen(mc_platform->hostname)+1));
00120 CHECK_NULL(mc_platform->hostname, agency->last_error = MC_ERR_MEMORY;return NULL);
00121 mc_platform->port = agency->portno;
00122 mc_platform->interp_options = ch_options;
00123
00124 mc_platform->message_queue = message_queue_New();
00125 mc_platform->agent_queue = agent_queue_New();
00126 mc_platform->connection_queue = connection_queue_New();
00127
00128 mc_platform->syncList = syncListInit();
00129 mc_platform->barrier_queue = barrier_queue_New();
00130
00131 mc_platform->interpreter_queue = interpreter_queue_New();
00132 mc_platform->initInterps = agency->initInterps;
00133
00134
00135
00136 for(i = 0; i < mc_platform->initInterps; i++) {
00137 interp = (ChInterp_t*)malloc(sizeof(ChInterp_t));
00138 if( mc_platform->interp_options == NULL ) {
00139 if(Ch_Initialize(interp, NULL)) {
00140 printf("CH INIT ERROR \n");
00141 exit(EXIT_FAILURE);
00142 }
00143 }
00144 else {
00145 if(Ch_Initialize(interp, mc_platform->interp_options)) {
00146 printf("CH INIT ERROR \n");
00147 exit(EXIT_FAILURE);
00148 }
00149 }
00150
00151
00152 agent_ChScriptInitVar(interp);
00153
00154 interpreter_queue_Add(
00155 mc_platform->interpreter_queue,
00156 (struct AP_GENERIC_s*)interp );
00157 }
00158
00159
00160 mc_platform->MC_signal_cond = (COND_T*)malloc(sizeof(COND_T));
00161 mc_platform->MC_sync_cond = (COND_T*)malloc(sizeof(COND_T));
00162 mc_platform->MC_signal_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00163 mc_platform->MC_sync_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00164 mc_platform->MC_steer_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00165 mc_platform->MC_steer_cond = (COND_T*)malloc(sizeof(COND_T));
00166
00167
00168 CHECK_NULL( mc_platform->MC_signal_cond,
00169 agency->last_error = MC_ERR_MEMORY;return NULL );
00170 CHECK_NULL( mc_platform->MC_sync_cond ,
00171 agency->last_error = MC_ERR_MEMORY;return NULL );
00172 CHECK_NULL( mc_platform->MC_signal_lock,
00173 agency->last_error = MC_ERR_MEMORY;return NULL );
00174 CHECK_NULL( mc_platform->MC_sync_lock ,
00175 agency->last_error = MC_ERR_MEMORY;return NULL );
00176 CHECK_NULL( mc_platform->MC_steer_lock ,
00177 agency->last_error = MC_ERR_MEMORY;return NULL );
00178 CHECK_NULL( mc_platform->MC_steer_cond ,
00179 agency->last_error = MC_ERR_MEMORY;return NULL );
00180
00181
00182 COND_INIT ( mc_platform->MC_signal_cond );
00183 COND_INIT ( mc_platform->MC_sync_cond );
00184 MUTEX_INIT( mc_platform->MC_signal_lock );
00185 MUTEX_INIT( mc_platform->MC_sync_lock );
00186 MUTEX_INIT( mc_platform->MC_steer_lock );
00187 COND_INIT ( mc_platform->MC_steer_cond );
00188
00189
00190 mc_platform->quit = 0;
00191 mc_platform->quit_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00192 MUTEX_INIT(mc_platform->quit_lock);
00193 mc_platform->quit_cond = (COND_T*)malloc(sizeof(COND_T));
00194 COND_INIT(mc_platform->quit_cond);
00195
00196
00197 mc_platform->MC_signal = MC_NO_SIGNAL;
00198
00199
00200 mc_platform->giant = 1;
00201 mc_platform->giant_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00202 CHECK_NULL(mc_platform->giant_lock,
00203 agency->last_error = MC_ERR_MEMORY;return NULL );
00204 mc_platform->giant_cond = (COND_T*)malloc(sizeof(COND_T));
00205 CHECK_NULL(mc_platform->giant_cond,
00206 agency->last_error = MC_ERR_MEMORY;return NULL );
00207
00208 MUTEX_INIT( mc_platform->giant_lock );
00209 COND_INIT ( mc_platform->giant_cond );
00210
00211
00212 mc_platform->df = df_Initialize(mc_platform);
00213 mc_platform->ams = ams_Initialize(mc_platform);
00214 mc_platform->acc = acc_Initialize(mc_platform);
00215 mc_platform->cmd_prompt = cmd_prompt_Initialize(mc_platform);
00216 if (GET_THREAD_MODE(agency->threads, MC_THREAD_DF)) {
00217 df_Start(mc_platform);
00218 MUTEX_LOCK(mc_platform->df->waiting_lock);
00219
00220 while(mc_platform->df->waiting == 0) {
00221 COND_WAIT(mc_platform->df->waiting_cond, mc_platform->df->waiting_lock);
00222 }
00223 MUTEX_UNLOCK(mc_platform->df->waiting_lock);
00224 }
00225 if (GET_THREAD_MODE(agency->threads, MC_THREAD_AMS)) {
00226 ams_Start(mc_platform);
00227 MUTEX_LOCK(mc_platform->ams->waiting_lock);
00228
00229 while(mc_platform->ams->waiting == 0) {
00230 COND_WAIT(mc_platform->ams->waiting_cond, mc_platform->ams->waiting_lock);
00231 }
00232 MUTEX_UNLOCK(mc_platform->ams->waiting_lock);
00233 }
00234 if (GET_THREAD_MODE(agency->threads, MC_THREAD_ACC)) {
00235 acc_Start(mc_platform);
00236 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00237
00238 while(mc_platform->acc->waiting == 0) {
00239 COND_WAIT(mc_platform->acc->waiting_cond, mc_platform->acc->waiting_lock);
00240 }
00241 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00242 }
00243 if (GET_THREAD_MODE(agency->threads, MC_THREAD_CP))
00244 cmd_prompt_Start(mc_platform);
00245 return mc_platform;
00246 }
00247
00248 int
00249 mc_platform_Destroy(mc_platform_p platform)
00250 {
00251 #if 0
00252
00253
00254 #ifdef _WIN32
00255 closesocket(platform->sockfd);
00256 #else
00257 if(close(platform->sockfd) <0 ) {
00258 SOCKET_ERROR();
00259 }
00260 #endif
00261 #endif
00262 ChInterp_t* interp;
00263
00264 message_queue_Destroy(platform->message_queue);
00265
00266 agent_queue_Destroy(platform->agent_queue);
00267
00268 connection_queue_Destroy(platform->connection_queue);
00269
00270 df_Destroy(platform->df);
00271
00272 ams_Destroy(platform->ams);
00273
00274 acc_Destroy(platform->acc);
00275
00276 cmd_prompt_Destroy(platform->cmd_prompt);
00277
00278
00279 syncListDestroy(platform->syncList);
00280 barrier_queue_Destroy(platform->barrier_queue);
00281
00282 free(platform->hostname);
00283
00284
00285 while((interp = (ChInterp_t*)interpreter_queue_Pop(platform->interpreter_queue))) {
00286 Ch_End(*interp);
00287 free(interp);
00288 }
00289 interpreter_queue_Destroy(platform->interpreter_queue);
00290
00291 COND_DESTROY(platform->MC_signal_cond);
00292 free(platform->MC_signal_cond);
00293 COND_DESTROY(platform->MC_sync_cond);
00294 free(platform->MC_sync_cond);
00295 MUTEX_DESTROY(platform->MC_signal_lock);
00296 free(platform->MC_signal_lock);
00297 MUTEX_DESTROY(platform->MC_sync_lock);
00298 free(platform->MC_sync_lock);
00299
00300 MUTEX_DESTROY(platform->MC_steer_lock);
00301 free(platform->MC_steer_lock);
00302 COND_DESTROY(platform->MC_steer_cond);
00303 free(platform->MC_steer_cond);
00304
00305 MUTEX_DESTROY(platform->quit_lock);
00306 free(platform->quit_lock);
00307 COND_DESTROY(platform->quit_cond);
00308 free(platform->quit_cond);
00309
00310 MUTEX_DESTROY(platform->giant_lock);
00311 free(platform->giant_lock);
00312 COND_DESTROY(platform->giant_cond);
00313 free(platform->giant_cond);
00314
00315
00316 if (platform->interp_options != NULL) {
00317 if (platform->interp_options->chhome != NULL) {
00318 free(platform->interp_options->chhome);
00319 }
00320 free(platform->interp_options);
00321 }
00322
00323 free(platform);
00324 return MC_SUCCESS;
00325 }
00326
00327
00328