/** * Socket(Listener only): Get contact string. */ char * ngrcSocketGetContactString( ngrcSocket_t *sock) { ngLog_t *log; struct sockaddr_in addr; char *ret = NULL; int result; ngiSockLen_t addr_len = sizeof(addr); NGEM_FNAME(ngrcSocketGetContactString); NGEM_ASSERT(sock != NULL); log = ngemLogGetDefault(); /* TODO: Local socket */ result = getsockname(sock->ngs_fd, (struct sockaddr *)&addr, &addr_len); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "getsockname: %s\n", strerror(errno)); return NULL; } NGEM_ASSERT(addr.sin_family == AF_INET); /* Remote Communication Proxy uses local address only. */ ret = ngemStrdupPrintf("ng_tcp://localhost:%d/", ntohs(addr.sin_port)); if (ret == NULL) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't create address string.\n"); return NULL; } return ret; }
static int ngexlStubCalculationEnd(int *error) { int result; ngLog_t *log; ngexiContext_t *context; ngiProtocol_t *protocol; ngexiExecutableStatus_t status; static const char fName[] = "ngexlStubCalculationEnd"; /* Initialize the local variables */ context = &ngexiContext; protocol = context->ngc_protocol; log = context->ngc_log; /* Set end time */ result = ngexiContextSetMethodEndTime(context, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't set the End Time.\n"); return 0; } /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Calculation finished.\n"); /* Get status */ status = ngexiContextExecutableStatusGet(context, error); /* Check Status */ switch(status) { case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: case NGEXI_EXECUTABLE_STATUS_PULL_WAIT: case NGEXI_EXECUTABLE_STATUS_END: return 1; case NGEXI_EXECUTABLE_STATUS_CALCULATING: /* Do nothing, go next */ break; default: NGI_SET_ERROR(error, NG_ERROR_INVALID_STATE); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected state: %d.\n", status); return 0; } /* Send notify */ result = ngexiProtocolNotifyCalculationEnd( context, protocol, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't send the notify Calculation End.\n"); return 0; } /* Success */ return 1; }
/** * Socket: Read * if nread == 0, EOF or Canceled. */ ngemResult_t ngrcSocketRead( ngrcSocket_t *sock, void *buf, size_t size, size_t *nread) { fd_set rfds; int result; ngLog_t *log; int fdmax; ssize_t nr = -1; NGEM_FNAME(ngrcSocketRead); NGEM_ASSERT(sock != NULL); NGEM_ASSERT(buf != NULL); NGEM_ASSERT(size > 0); NGEM_ASSERT(nread != NULL); *nread = 0; log = ngemLogGetDefault(); while (nr < 0) { FD_ZERO(&rfds); FD_SET(sock->ngs_fd, &rfds); FD_SET(NGEM_PIPE_IN(sock->ngs_pipe), &rfds); fdmax = NGEM_MAX(sock->ngs_fd, NGEM_PIPE_IN(sock->ngs_pipe)) + 1; result = select(fdmax, &rfds, NULL, NULL, NULL); if (result < 0) { if (errno == EINTR) { continue; } ngLogError(log, NGRC_LOGCAT_GT, fName, "select: %s\n", strerror(errno)); return NGEM_FAILED; } if (FD_ISSET(NGEM_PIPE_IN(sock->ngs_pipe), &rfds)) { /* Not continue */ return NGEM_SUCCESS; } if (FD_ISSET(sock->ngs_fd, &rfds)) { nr = read(sock->ngs_fd, buf, size); if (nr < 0) { if (errno == EAGAIN) {continue;} ngLogError(log, NGRC_LOGCAT_GT, fName, "read: %s\n", strerror(errno)); return NGEM_SUCCESS; } } } *nread = nr; return NGEM_SUCCESS; }
/** * Socket: Destroy */ ngemResult_t ngrcSocketDestroy( ngrcSocket_t *sock) { ngemResult_t ret = NGEM_SUCCESS; int result; ngLog_t *log = NULL; NGEM_FNAME(ngrcSocketDestroy); log = ngemLogGetDefault(); if (sock == NULL) { return NGEM_SUCCESS; } if (sock->ngs_fd >= 0) { result = close(sock->ngs_fd); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); ret = NGEM_FAILED; } sock->ngs_fd = -1; } if (sock->ngs_pipe[0] >= 0) { result = close(sock->ngs_pipe[0]); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); ret = NGEM_FAILED; } sock->ngs_pipe[0] = -1; } if (sock->ngs_pipe[1] >= 0) { result = close(sock->ngs_pipe[1]); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); ret = NGEM_FAILED; } sock->ngs_pipe[1] = -1; } NGI_DEALLOCATE(ngrcSocket_t, sock, log, NULL); return ret; }
/** * Finalize */ int ngiSessionInformationFinalize( ngSessionInformation_t *info, ngLog_t *log, int *error) { int result; static const char fName[] = "ngiSessionInformationFinalize"; /* Check the arguments */ assert(info != NULL); /* Finish the measurement */ result = ngiSessionInformationFinishMeasurement(info, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't finish the measurement.\n"); return 0; } /* Initialize the members */ nglSessionInformationInitializeMember(info); /* Success */ return 1; }
/** * GetCopy */ int ngclLocalMachineInformationGetCopy( ngclContext_t *context, ngclLocalMachineInformation_t *lmInfo, int *error) { int local_error, result; static const char fName[] = "ngclLocalMachineInformationGetCopy"; /* Clear the error */ NGI_SET_ERROR(&local_error, NG_ERROR_NO_ERROR); /* Is Ninf-G Context valid? */ result = ngcliContextIsValid(context, &local_error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Ninf-G Context is not valid.\n"); NGI_SET_ERROR(error, local_error); return 0; } result = ngcllLocalMachineInformationGetCopy( context, lmInfo, &local_error); NGI_SET_ERROR_CONTEXT(context, local_error, NULL); NGI_SET_ERROR(error, local_error); return result; }
static int ngexlStubFinalize( int *error) { int result; int rank; ngLog_t *log; static const char fName[] = "ngexlStubFinalize"; /* Initialize the local variables */ log = ngexiContext.ngc_log; rank = -1; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Finalizing Ninf-G Executable.\n"); /* Finalize the Context */ if (ngexiContextInitialized != 0) { rank = ngexiContext.ngc_rank; result = ngexiContextFinalize(&ngexiContext, error); ngexiContextInitialized = 0; log = NULL; if (result == 0) { log = NULL; ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't finalize the Context.\n"); return 0; } } log = NULL; if (rank == 0) { /* Finalize the Signal Manager */ result = ngexiSignalManagerFinalize(log, error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Can't finalize the Signal Manager.\n"); return 0; } } /* Success */ return 1; }
/* * Socket: Create(common) */ static ngrcSocket_t * ngrclSocketCreate(int fd) { ngLog_t *log; ngemResult_t nResult; ngrcSocket_t *sock = NULL; NGEM_FNAME(ngrclSocketCreate); log = ngemLogGetDefault(); sock = NGI_ALLOCATE(ngrcSocket_t, log, NULL); if (sock == NULL) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't allocate the storage for socket.\n."); goto error; } sock->ngs_fd = fd; sock->ngs_pipe[0] = -1; sock->ngs_pipe[1] = -1; nResult = ngemFDsetNonblockFlag(sock->ngs_fd); if (nResult != NGEM_SUCCESS) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't set non-block flag to fd.\n"); goto error; } nResult = ngemNonblockingPipe(sock->ngs_pipe); if (nResult != NGEM_SUCCESS) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't create the pipe.\n"); goto error; } return sock; error: nResult = ngrcSocketDestroy(sock); if (nResult != NGEM_SUCCESS) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't destroy the socket.\n"); } sock = NULL; return NULL; }
/** * Start the measurement. */ int ngiSessionInformationStartMeasurement( ngSessionInformation_t *info, int nArguments, ngLog_t *log, int *error) { int result; static const char fName[] = "ngiSessionInformationStartMeasurement"; /* Check the arguments */ assert(info != NULL); assert(nArguments >= 0); assert(info->ngsi_compressionInformation == NULL); /* Initialize */ result = ngiSessionInformationInitialize(info, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Session Information.\n"); return 0; } /* Allocate the storage for Compression Information */ info->ngsi_nCompressionInformations = nArguments; if (nArguments > 0) { info->ngsi_compressionInformation = ngiCalloc( nArguments, sizeof (*info->ngsi_compressionInformation), log, error); if (info->ngsi_compressionInformation == NULL) { NGI_SET_ERROR(error, NG_ERROR_MEMORY); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't allocate the storage for Session Information.\n"); return 0; } } /* Success */ return 1; }
/** * Construct */ ngSessionInformation_t * ngiSessionInformationConstruct( ngLog_t *log, int *error) { int result; ngSessionInformation_t *info; static const char fName[] = "ngiSessionInformationConstruct"; /* Allocate */ info = NGI_ALLOCATE(ngSessionInformation_t, log, error); if (info == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't allocate the storage for Session Information.\n"); return NULL; } /* Initialize */ result = ngiSessionInformationInitialize(info, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Session Information.\n"); goto error; } /* Success */ return info; /* Error occurred */ error: result = NGI_DEALLOCATE(ngSessionInformation_t, info, log, NULL); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't deallocate the storage for Session Information.\n"); return NULL; } /* Failed */ return NULL; }
static int ngexlStubCalculationStart(int *error) { int result; ngLog_t *log; ngexiContext_t *context; static const char fName[] = "ngexlStubCalculationStart"; static const ngexiExecutableStatus_t reqStatus[] = { NGEXI_EXECUTABLE_STATUS_CALCULATING, }; /* Initialize the local variables */ context = &ngexiContext; log = context->ngc_log; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Calculation started.\n"); /* Check the status */ result = ngexiContextExecutableStatusCheck( context, &reqStatus[0], NGI_NELEMENTS(reqStatus), log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Invalid state.\n"); return 0; } /* Set start time */ result = ngexiContextSetMethodStartTime(&ngexiContext, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't set the Start Time.\n"); return 0; } return 1; }
/** * Encodes the string by the following way(like URL encoding). * - Alphabetic characters, digits(0 through 9), '.', '-' and '_' is not changed. * - Space is changed to '+'. * - Another character is changed to "%XX". * (XX is an expression of the character-code by the hexadecimal * number of two digits) */ char * ngiCommunicationProxyEncode( const char *src, ngLog_t *log, int *error) { const char *p; char *q; int len; char *ret = NULL; static const char fName[] = "ngiCommunicationProxyEncode"; assert(src != NULL); len = 0; for (p = src;*p != '\0';++p) { if (nglIsCharAsIs(*p, log, error) != 0) { len++; } else if (*p == ' ') { len++; }else { len += 3;/* length of %XX */ } } len++; /* for '\0' */ ret = ngiMalloc(len, log, error); if (ret == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't allocate storage for string.\n"); return NULL; } q = ret; for (p = src;*p != '\0';++p) { if (nglIsCharAsIs(*p, log, error) != 0) { *q = *p; q++; } else if (*p == ' ') { *q = '+'; q++; } else { snprintf(q, 3+1, "%%%02X", (int)(unsigned char)*p); q += 3;/* length of %XX */ } } *q = '\0'; return ret; }
/** * Socket: duplicate */ ngrcSocket_t * ngrcSocketDup( ngrcSocket_t *src) { ngLog_t *log; ngrcSocket_t *sock = NULL; int fd = -1; int result; NGEM_FNAME(ngrcSocketDup); log = ngemLogGetDefault(); fd = dup(src->ngs_fd); if (fd < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "dup: %s.\n", strerror(errno)); goto error; } sock = ngrclSocketCreate(fd); if (sock == NULL) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't create socket.\n"); goto error; } return sock; error: if (fd >= 0) { result = close(fd); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); } } return NULL; }
/** * Socket: Cancels operation(read and write). */ ngemResult_t ngrcSocketCancel( ngrcSocket_t *sock) { int result; ngLog_t *log = NULL; NGEM_FNAME(ngrcSocketCancel); log = ngemLogGetDefault(); result = write(NGEM_PIPE_OUT(sock->ngs_pipe), "C", 1); if (result <= 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "write: %s\n", strerror(errno)); return NGEM_FAILED; } return NGEM_SUCCESS; }
/** * Decode by the string encoded by ngiCommunicationProxyEncode(). */ char * ngiCommunicationProxyDecode( const char *src, ngLog_t *log, int *error) { const char *p; char *q; int len; int req = 0; char hex[3];/* XX\0 */ int i; int found; char *ret = NULL; static const char fName[] = "ngiCommunicationProxyDecode"; assert(src != NULL); len = 0; req = 0; for (p = src;*p != '\0';++p) { if (req == 0) { if (*p == '%') { req = 2; /* %XX */ /* ^^ */ } len++; } else { found = 0; for (i = 0;nglCharSetXdigit[i] != '\0';++i) { if (nglCharSetXdigit[i] == *p) { found = 1; break; } } if (found == 0) { /* Error */ ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "%s: Invalid String.\n", src); NGI_SET_ERROR(error, NG_ERROR_SYNTAX); return NULL; } req--; } } if (req > 0) { NGI_SET_ERROR(error, NG_ERROR_SYNTAX); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "%s: Invalid String.\n", src); return NULL; } len++; /* for '\0' */ ret = ngiMalloc(len, log, error); if (ret == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't allocate storage for string.\n"); return NULL; } q = ret; for (p = src;*p != '\0';++p) { if (*p == '%') { hex[0] = *++p; hex[1] = *++p; hex[2] = '\0'; assert(hex[0] != '\0'); assert(hex[1] != '\0'); *q = (unsigned char)strtol(hex, NULL, 16); } else if (*p == '+') { *q = ' '; } else { *q = *p; } q++; } *q = '\0'; return ret; }
/** * Callback function is performed. */ int ngexStubCallback(int callbackID, int *error, ...) { int result; va_list ap; int finished; int methodID; char *string; ngLog_t *log; int i, count = 0; ngiProtocol_t *protocol; ngexiContext_t *context; ngexiExecutableStatus_t status; ngiArgument_t *arg, *callbackArg; ngRemoteMethodInformation_t *rmInfo, *rmInfoCallback; static const char fName[] = "ngexStubCallback"; static ngexiExecutableStatus_t waitStatus[] = { NGEXI_EXECUTABLE_STATUS_CALCULATING, NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED, NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED, NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED, NGEXI_EXECUTABLE_STATUS_END, }; /* Initialize the local variables */ context = &ngexiContext; protocol = context->ngc_protocol; log = context->ngc_log; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Performing callback (id=%d).\n", callbackID); /* Get status */ status = ngexiContextExecutableStatusGet(context, error); /* Check Status */ switch(status) { case NGEXI_EXECUTABLE_STATUS_CALCULATING: /* Do nothing, go next */ break; case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: case NGEXI_EXECUTABLE_STATUS_PULL_WAIT: case NGEXI_EXECUTABLE_STATUS_END: NGI_SET_ERROR(error, NG_ERROR_INVALID_STATE); ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "The session is already canceled. (status=%d)\n", status); return 0; default: NGI_SET_ERROR(error, NG_ERROR_INVALID_STATE); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected state: %d.\n", status); return 0; } /* Set the callback ID to Protocol */ result = ngiProtocolSetCallbackID(protocol, callbackID, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't set the callback ID.\n"); return 0; } /* Get the argument pointer */ va_start(ap, error); /* Get the Method Information */ methodID = ngexiContextGetMethodID(context); rmInfo = ngexiRemoteMethodInformationGet( context, methodID, log, error); if (rmInfo == NULL) { ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "Remote Method Information is not found.\n"); return 0; } for (i = 0; i < rmInfo->ngrmi_nArguments; i++) { if (rmInfo->ngrmi_arguments[i].ngai_dataType == NG_ARGUMENT_DATA_TYPE_CALLBACK) { if (count == callbackID) { break; } count += 1; } } if ((i == rmInfo->ngrmi_nArguments) && (count != callbackID)) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Callback ID %d is not valid.\n", callbackID); return 0; } rmInfoCallback = rmInfo->ngrmi_arguments[i].ngai_callback; /* Get the argument */ ngexiContextGetMethodArgument(&ngexiContext, &arg); if (arg == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't get the argument.\n"); return 0; } /* Construct the Argument */ callbackArg = ngiArgumentConstruct( rmInfoCallback->ngrmi_arguments, rmInfoCallback->ngrmi_nArguments, log, error); if (callbackArg == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't construct the argument.\n"); return 0; } result = ngiArgumentElementInitializeVarg( callbackArg, rmInfoCallback->ngrmi_nArguments, ap, NGI_ARGUMENT_DELIVERY_C, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Argument Element.\n"); return 0; } /* Initialize the Subscript Value of Argument */ result = ngiArgumentInitializeSubscriptValue( callbackArg, arg, rmInfoCallback->ngrmi_arguments, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Argument Element.\n"); return 0; } /* Check the Subscript Value of Argument */ result = ngiArgumentCheckSubscriptValue(callbackArg, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Subscript Value of Argument is not valid.\n"); goto error; } /* Set the Argument for Callback*/ ngexiContextSetCallbackArgument(context, callbackArg); /* Set the argument to protocol */ result = ngiProtocolSetCallbackArgument(protocol, callbackArg, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't set the argument to protocol.\n"); return 0; } /* Send Notify Invoke Callback */ result = ngexiProtocolNotifyInvokeCallback( context, protocol, callbackID, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't set executable ID to protocol.\n"); return 0; } /* Increment the number of times to which the callback was called */ context->ngc_sessionInfo.ngsi_callbackNtimesCalled++; finished = 0; while (finished == 0) { /* Wait the status */ result = ngexiContextExecutableStatusWait( context, &waitStatus[0], NGI_NELEMENTS(waitStatus), &status, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't wait the status.\n"); goto error; } switch (status) { case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: /* Do not reply. reply is treated by ngexStubGetRequest */ finished = 1; break; case NGEXI_EXECUTABLE_STATUS_CALCULATING: finished = 1; break; case NGEXI_EXECUTABLE_STATUS_END: default: /* log */ string = ngexiContextExecutableStatusStringGet(status, log, NULL); if (string == NULL) { string = "Not valid status"; } ngLogWarn(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected status \"%s\":%d was found.\n", string, status); goto error; } } /* Release the argument pointer */ va_end(ap); /* Release the argument to protocol */ result = ngiProtocolReleaseCallbackArgument(protocol, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't release the argument to protocol.\n"); return 0; } /* Release the callback ID to protocol */ result = ngiProtocolReleaseCallbackID(protocol, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't release the callback ID to protocol.\n"); return 0; } /* Check canceled */ switch (status) { case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: return 0; default: break; } /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "The callback was successfully done (id=%d).\n", callbackID); /* Success */ return 1; /* Error occurred */ error: /* Release the argument pointer */ va_end(ap); /* Release the argument to protocol */ result = ngiProtocolReleaseCallbackArgument(protocol, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't release the argument to protocol.\n"); } /* Release the callback ID to protocol */ result = ngiProtocolReleaseCallbackID(protocol, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PROTOCOL, fName, "Can't release the callback ID to protocol.\n"); } /* Initialize the IDs */ ngiProtocolInitializeID(protocol); return 0; }
static int ngexlStubInitialize( int argc, char *argv[], ngRemoteClassInformation_t *rcInfo, int rank, int *error) { int result; ngCommLogPairInfo_t pairInfo; char host[NGI_HOST_NAME_MAX]; ngLogConstructArgument_t logCarg; int logCargInitialized = 0; ngLogInformation_t *logInfo = NULL; ngLog_t *log = NULL; static const char fName[] = "ngexlStubInitialize"; /* Is argc valid? */ if (argc <= 1) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "The argc %d is smaller equal 1.\n", argc); return 0; } /* Is argv valid? */ if (argv == NULL) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "The argv is NULL.\n"); return 0; } /* Is argv[1] valid? */ if (argv[1] == NULL) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "The argv[1] is NULL.\n"); return 0; } if (rank < 0) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "rank %d is a negative number.\n", rank); return 0; } if (rank == 0) { /* Initialize the Signal Manager */ result = ngexiSignalManagerInitialize(NULL, error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Signal Manager.\n"); return 0; } /* Initialize the Context */ result = ngexiContextInitialize( &ngexiContext, argc, argv, rcInfo, rank, error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Context.\n"); return 0; } ngexiContextInitialized = 1; log = ngexiContext.ngc_log; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Ninf-G Executable Context was created.\n"); /* Negotiation */ result = ngexiProcessNegotiation(&ngexiContext, log, error); if (result == 0) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Negotiation failed.\n"); return 0; } /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Executable ID = %d.\n", ngexiContext.ngc_commInfo.ngci_executableID); /* Change log file name to executableID numbered file */ result = ngiLogExecutableIDchanged(log, ngexiContext.ngc_commInfo.ngci_executableID, error); if (result == 0) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Can't change the log file name.\n"); return 0; } /* Construct the Communication Log */ if (ngexiContext.ngc_lmInfo.nglmi_commLogEnable != 0) { /* Create the message */ logInfo = &ngexiContext.ngc_lmInfo.nglmi_commLogInfo; result = ngLogConstructArgumentInitialize(&logCarg, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize log construct argument.\n"); goto error; } logCargInitialized = 1; if (logInfo->ngli_filePath == NULL) { logCarg.nglca_output = NG_LOG_STDERR; } else { logCarg.nglca_output = NG_LOG_FILE; logCarg.nglca_nFiles = logInfo->ngli_nFiles; logCarg.nglca_maxFileSize = logInfo->ngli_maxFileSize; logCarg.nglca_overWriteDir = logInfo->ngli_overWriteDir; logCarg.nglca_appending = 0;/* false */ logCarg.nglca_filePath = ngiStrdup( logInfo->ngli_filePath, log, error); if (logCarg.nglca_filePath == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't copy string.\n"); goto error; } if (logInfo->ngli_suffix != NULL) { logCarg.nglca_suffix = ngiStrdup( logInfo->ngli_suffix, log, error); if (logCarg.nglca_suffix == NULL) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't copy string.\n"); goto error; } } } result = ngiHostnameGet(&host[0], sizeof (host), NULL, error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Can't get the host name.\n"); goto error; } pairInfo.ngcp_localAppName = "Executable"; pairInfo.ngcp_localHostname = host; pairInfo.ngcp_remoteAppName = "Client"; pairInfo.ngcp_remoteHostname = ngexiContext.ngc_commInfo.ngci_hostname; ngexiContext.ngc_commLog = ngCommLogConstructForExecutable( &pairInfo, ngexiContext.ngc_commInfo.ngci_executableID, &logCarg, log, error); if (ngexiContext.ngc_commLog == NULL) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Can't create the Communication Log.\n"); goto error; } /* Register the Communication Log */ result = ngiCommunicationLogRegister( ngexiContext.ngc_communication, ngexiContext.ngc_commLog, ngexiContext.ngc_log, error); if (result == 0) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Can't register the Communication Log.\n"); goto error; } result = ngLogConstructArgumentFinalize(&logCarg, log, error); logCargInitialized = 0; if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't get the host name.\n"); goto error; } } /* Switch stdout/stderr output if necessary */ result = ngexiContextSwitchStdoutStderr( &ngexiContext, ngexiContext.ngc_commInfo.ngci_executableID, error); if (result == 0) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Can't switch stdout/stderr output.\n"); return 0; } /* log */ ngLogInfo(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Initialize was successful.\n"); /* Register the I/O Callback */ result = ngexiProtocolRegisterCallback(&ngexiContext, error); if (result == 0) { ngLogError(ngexiContext.ngc_log, NG_LOGCAT_NINFG_PURE, fName, "Can't register I/O callback.\n"); return 0; } } else { /* rank != 0 */ /* Initialize the Context */ result = ngexiContextInitialize( &ngexiContext, argc, argv, rcInfo, rank, error); if (result == 0) { ngLogError(NULL, NG_LOGCAT_NINFG_PURE, fName, "Can't initialize the Context.\n"); return 0; } ngexiContextInitialized = 1; } /* Success */ return 1; error: if (logCargInitialized != 0) { result = ngLogConstructArgumentFinalize(&logCarg, log, NULL); logCargInitialized = 0; if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't get the host name.\n"); goto error; } } return 0; }
static int ngexlStubGetRequest(int *methodID, int *error) { int result; int finished; char *string; ngLog_t *log; int returnCode; ngexiContext_t *context; ngiProtocol_t *protocol; ngexiExecutableStatus_t status; static const char fName[] = "ngexlStubGetRequest"; static ngexiExecutableStatus_t waitStatus[] = { NGEXI_EXECUTABLE_STATUS_CALCULATING, NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED, NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED, NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED, NGEXI_EXECUTABLE_STATUS_END, }; /* Initialize the local variables */ context = &ngexiContext; protocol = context->ngc_protocol; log = context->ngc_log; returnCode = NGEXI_FAIL; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "GetRequest started.\n"); /* Check the arguments */ if (methodID == NULL) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "methodID is NULL.\n"); return NGEXI_FAIL; } /* Get status */ status = ngexiContextExecutableStatusGet(context, error); /* Check Status */ if ((status < NGEXI_EXECUTABLE_STATUS_IDLE) || (status > NGEXI_EXECUTABLE_STATUS_END)) { NGI_SET_ERROR(error, NG_ERROR_INVALID_STATE); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected state: %d.\n", status); return NGEXI_FAIL; } finished = 0; while (finished == 0) { /* Wait the status */ result = ngexiContextExecutableStatusWait( context, &waitStatus[0], NGI_NELEMENTS(waitStatus), &status, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't wait the status.\n"); goto error; } switch (status) { case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: /* Process request */ result = ngexiProtocolReplyByStatus( context, protocol, status, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't send the reply.\n"); goto error; } if (status == NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED) { returnCode = NGEXI_EXIT; finished = 1; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "GetRequest is returned by EXIT.\n"); } break; case NGEXI_EXECUTABLE_STATUS_CALCULATING: *methodID = ngexiContextGetMethodID(context); returnCode = NGEXI_INVOKE_METHOD; finished = 1; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "GetRequest is returned by Invoke method. MethodID = %d.\n", *methodID); break; case NGEXI_EXECUTABLE_STATUS_END: default: /* log */ string = ngexiContextExecutableStatusStringGet(status, log, NULL); if (string == NULL) { string = "Not valid status"; } ngLogWarn(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected status \"%s\":%d was found.\n", string, status); goto error; } } /* Success */ return returnCode; /* Error occurred */ error: ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "return by abnormal error.\n"); /* Failed */ return NGEXI_FAIL; }
static int ngexlStubGetArgument(int argumentNo, void *data, int *error) { int result; int methodID; ngLog_t *log; ngiArgument_t *arg; ngiArgumentPointer_t src, dest; ngRemoteMethodInformation_t *rmInfo; ngexiContext_t *context; static const char fName[] = "ngexlStubGetArgument"; static const ngexiExecutableStatus_t reqStatus[] = { NGEXI_EXECUTABLE_STATUS_CALCULATING, }; /* Initialize the local variables */ context = &ngexiContext; log = context->ngc_log; /* Get methodID */ methodID = ngexiContextGetMethodID(context); /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Getting argument %3d for method %d from Context.\n", argumentNo, methodID); /* Check the status */ result = ngexiContextExecutableStatusCheck( context, &reqStatus[0], NGI_NELEMENTS(reqStatus), log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Invalid state.\n"); return 0; } /* Get the Method Information */ rmInfo = ngexiRemoteMethodInformationGet( context, methodID, log, error); if (rmInfo == NULL) { ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "Remote Method Information is not found.\n"); return 0; } /* Get the Argument Data */ ngexiContextGetMethodArgument(context, &arg); if (arg == NULL) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "Argument Data is NULL.\n"); return 0; } if (arg->nga_nArguments < 0) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "The number of Argument Data is smaller than zero.\n"); return 0; } if (arg->nga_nArguments > rmInfo->ngrmi_nArguments) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "The number of Argument Data %d is greater than maximum %d.\n", arg->nga_nArguments, rmInfo->ngrmi_nArguments); return 0; } if (argumentNo < 0) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "The requested Argument No. %d is smaller equal zero.\n", argumentNo); return 0; } if (argumentNo >= rmInfo->ngrmi_nArguments) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "The number of Argument Data %d is greater than maximum %d.\n", argumentNo, rmInfo->ngrmi_nArguments); return 0; } /* Is argument scalar variable? */ src = arg->nga_argument[argumentNo].ngae_pointer; if (arg->nga_argument[argumentNo].ngae_dataType == NG_ARGUMENT_DATA_TYPE_FILENAME) { if (arg->nga_argument[argumentNo].ngae_tmpFileNameTable == NULL) { *(char **)data = NULL; } else if (arg->nga_argument[argumentNo].ngae_nDimensions <= 0) { *(char **)data = arg->nga_argument[argumentNo].ngae_tmpFileNameTable[0]; } else { *(char ***)data = arg->nga_argument[argumentNo].ngae_tmpFileNameTable; } } else if (arg->nga_argument[argumentNo].ngae_dataType == NG_ARGUMENT_DATA_TYPE_STRING) { if (src.ngap_string == NULL) { *(char **)data = NULL; } else if (arg->nga_argument[argumentNo].ngae_nDimensions <= 0) { *(char **)data = src.ngap_stringArray[0]; } else { *(char ***)data = src.ngap_stringArray; } } else if (arg->nga_argument[argumentNo].ngae_nDimensions != 0) { *(void **)data = src.ngap_void; } else { dest.ngap_void = data; result = ngiGetArgumentData( arg->nga_argument[argumentNo].ngae_dataType, src, dest, log, error); if (result == 0) { ngLogFatal(log, NG_LOGCAT_NINFG_PURE, fName, "Can't get the Argument Data.\n"); return 0; } } /* log */ ngLogDebug(log, NG_LOGCAT_NINFG_PURE, fName, "Getting argument %3d for method %d finished.\n", argumentNo, methodID); /* Success */ return 1; }
/** * Socket: accept */ ngrcSocket_t * ngrcSocketAccept( ngrcSocket_t *listener, bool *canceled) { ngrcSocket_t *sock = NULL; int fd = -1; fd_set rfds; int result; ngLog_t *log; int fdmax; struct sockaddr_in addr; ngiSockLen_t addr_len = sizeof(addr); NGEM_FNAME(ngrcSocketAccept); NGEM_ASSERT(listener != NULL); NGEM_ASSERT(canceled != NULL); *canceled = false; log = ngemLogGetDefault(); while (fd < 0) { FD_ZERO(&rfds); FD_SET(listener->ngs_fd, &rfds); FD_SET(NGEM_PIPE_IN(listener->ngs_pipe), &rfds); fdmax = NGEM_MAX(listener->ngs_fd, NGEM_PIPE_IN(listener->ngs_pipe)) + 1; result = select(fdmax, &rfds, NULL, NULL, NULL); if (result < 0) { if (errno == EINTR) { continue; } ngLogError(log, NGRC_LOGCAT_GT, fName, "select: %s\n", strerror(errno)); goto error; } if (FD_ISSET(NGEM_PIPE_IN(listener->ngs_pipe), &rfds)) { /* Not continue */ *canceled = true; return NULL; } if (FD_ISSET(listener->ngs_fd, &rfds)) { fd = accept(listener->ngs_fd, (struct sockaddr *)&addr, &addr_len); if (result < 0) { if (errno == EAGAIN) {continue;} ngLogError(log, NGRC_LOGCAT_GT, fName, "accept: %s\n", strerror(errno)); if (errno == EBADF) { return NULL; } continue; } } } sock = ngrclSocketCreate(fd); if (sock == NULL) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't create socket.\n"); goto error; } return sock; error: if (fd >= 0) { result = close(fd); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); } } return NULL; }
static int ngexlIsCanceled(int *error) { ngLog_t *log; ngexiContext_t *context; int result, signalBlocked; ngexiExecutableStatus_t status; static const char fName[] = "ngexlIsCanceled"; /* Initialize the local variables */ context = &ngexiContext; log = context->ngc_log; signalBlocked = 0; /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "Checking if the session was canceled.\n"); /* Block the signal for NonThread */ result = ngexiHeartBeatSendBlock( context, NGEXI_HEARTBEAT_SEND_BLOCK_READ, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't block signal.\n"); goto error; } signalBlocked = 1; /* Pass the control for I/O callback. although NonThread */ result = ngiThreadYield(context->ngc_event, log, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "thread yield failed.\n"); goto error; } /* Unblock the signal */ signalBlocked = 0; result = ngexiHeartBeatSendBlock( context, NGEXI_HEARTBEAT_SEND_UNBLOCK_READ, error); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't unblock signal.\n"); goto error; } /* Get status */ status = ngexiContextExecutableStatusGet(context, error); /* Check Status */ switch(status) { case NGEXI_EXECUTABLE_STATUS_CALCULATING: /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "The session was not canceled.\n"); /* Not canceled */ return 0; case NGEXI_EXECUTABLE_STATUS_CANCEL_REQUESTED: case NGEXI_EXECUTABLE_STATUS_RESET_REQUESTED: case NGEXI_EXECUTABLE_STATUS_EXIT_REQUESTED: case NGEXI_EXECUTABLE_STATUS_PULL_WAIT: case NGEXI_EXECUTABLE_STATUS_END: /* Do not reply. reply is treated by ngexStubGetRequest */ break; default: NGI_SET_ERROR(error, NG_ERROR_INVALID_STATE); ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Unexpected state: %d\n", status); goto error; } /* log */ ngLogInfo(log, NG_LOGCAT_NINFG_PURE, fName, "The session was canceled.\n"); /* Canceled */ return 1; /* Error occurred */ error: ngLogWarn(log, NG_LOGCAT_NINFG_PURE, fName, "return by abnormal error\n"); /* Unblock the signal */ if (signalBlocked != 0) { signalBlocked = 0; result = ngexiHeartBeatSendBlock( context, NGEXI_HEARTBEAT_SEND_UNBLOCK_READ, NULL); if (result == 0) { ngLogError(log, NG_LOGCAT_NINFG_PURE, fName, "Can't unblock signal.\n"); } } /* Failed */ return 0; /* Attention : same as not canceled */ }
/** * Socket: Create listener. */ ngrcSocket_t * ngrcSocketCreateListener( bool tcpNodelay) { ngLog_t *log; int result; int fd = -1; struct sockaddr_in addr; ngrcSocket_t *sock = NULL; int nodelay; NGEM_FNAME(ngrcSocketCreateListener); log = ngemLogGetDefault(); /* TODO: Local socket */ fd = socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "socket: %s.\n", strerror(errno)); goto error; } memset(&addr, '\0', sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = htons(0); /* any */ result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "bind: %s.\n", strerror(errno)); goto error; } if (tcpNodelay) { nodelay = 1; result = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "setsockopt(TCP_NODELAY): %s.\n", strerror(errno)); goto error; } } /* backlog is 1 because Remote Communication Proxy handles * a connection only.*/ result = listen(fd, 1); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "listen: %s.\n", strerror(errno)); goto error; } sock = ngrclSocketCreate(fd); if (sock == NULL) { ngLogError(log, NGRC_LOGCAT_GT, fName, "Can't create socket.\n"); goto error; } return sock; error: if (fd >= 0) { result = close(fd); if (result < 0) { ngLogError(log, NGRC_LOGCAT_GT, fName, "close: %s.\n", strerror(errno)); } } return NULL; }
/** * Socket:Write * if nwrite < size, Canceled. */ ngemResult_t ngrcSocketWrite( ngrcSocket_t *sock, void *buf, size_t size, size_t *nwrite) { fd_set rfds; fd_set wfds; int result; ngLog_t *log; int fdmax; ssize_t nw = 0; size_t sum = 0; NGEM_FNAME(ngrcSocketWrite); NGEM_ASSERT(sock != NULL); NGEM_ASSERT(buf != NULL); NGEM_ASSERT(size > 0); NGEM_ASSERT(nwrite != NULL); *nwrite = 0; log = ngemLogGetDefault(); while (sum < size) { FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(sock->ngs_fd, &wfds); FD_SET(NGEM_PIPE_IN(sock->ngs_pipe), &rfds); fdmax = NGEM_MAX(sock->ngs_fd, NGEM_PIPE_IN(sock->ngs_pipe)) + 1; result = select(fdmax, &rfds, &wfds, NULL, NULL); if (result < 0) { if (errno == EINTR) { continue; } ngLogError(log, NGRC_LOGCAT_GT, fName, "select: %s\n", strerror(errno)); return NGEM_FAILED; } if (FD_ISSET(NGEM_PIPE_IN(sock->ngs_pipe), &rfds)) { /* Not continue */ *nwrite = sum; return NGEM_SUCCESS; } if (FD_ISSET(sock->ngs_fd, &wfds)) { nw = write(sock->ngs_fd, ((char *)buf)+sum, size-sum); if (nw < 0) { if (errno == EAGAIN) {continue;} ngLogError(log, NGRC_LOGCAT_GT, fName, "write: %s\n", strerror(errno)); return NGEM_FAILED; } sum += nw; NGEM_ASSERT(sum <= size); } } *nwrite = sum; return NGEM_SUCCESS; }