X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fstack%2Fsamples%2Flinux%2FSimpleClientServer%2Focserverslow.cpp;h=2e45cbce2b602d402c0d7fb8bc3f271d6af3a93a;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=d6d805aea9de0073fc7f19ad5d57574ca2d53603;hpb=a5ba37c49bbd420bfadd646c8b08cce18003a821;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverslow.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverslow.cpp index d6d805a..2e45cbc 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverslow.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserverslow.cpp @@ -18,19 +18,34 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "iotivity_config.h" #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_WINDOWS_H +#include +#endif #include +#ifdef HAVE_PTHREAD_H #include +#endif +#ifdef HAVE_SYS_TIME_H #include +#endif +#include #include #include "ocstack.h" #include "oic_malloc.h" #include "oic_string.h" #include "logger.h" +#if defined (__TIZENRT__) +#include +#else #include "cJSON.h" +#endif #include "ocserverslow.h" #include "ocpayload.h" #include "payload_logging.h" @@ -38,11 +53,11 @@ volatile sig_atomic_t gQuitFlag = 0; static std::list gRequestList; -static constexpr unsigned int SLOW_RESPONSE_DELAY_SEC = 5; +BOOST_STATIC_CONSTEXPR unsigned int SLOW_RESPONSE_DELAY_SEC = 5; static LEDResource LED; -static constexpr unsigned int SAMPLE_MAX_NUM_POST_INSTANCE = 2; +BOOST_STATIC_CONSTEXPR unsigned int SAMPLE_MAX_NUM_POST_INSTANCE = 2; static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE]; //char *gResourceUri= const_cast("/a/led"); @@ -54,17 +69,17 @@ OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) { LEDResource *currLEDResource = &LED; - OC_LOG(INFO, TAG, "Entering constructResponse"); + OIC_LOG(INFO, TAG, "Entering constructResponse"); if (ehRequest->resource == gLedInstance[0].handle) { - OC_LOG(INFO, TAG, "handle 0"); + OIC_LOG(INFO, TAG, "handle 0"); currLEDResource = &gLedInstance[0]; gResourceUri = const_cast("a/led/0"); } else if (ehRequest->resource == gLedInstance[1].handle) { - OC_LOG(INFO, TAG, "handle 1"); + OIC_LOG(INFO, TAG, "handle 1"); currLEDResource = &gLedInstance[1]; gResourceUri = const_cast("a/led/1"); } @@ -73,7 +88,7 @@ OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) { if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { - OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation")); + OIC_LOG(ERROR, TAG, ("Incoming payload not a representation")); return nullptr; } @@ -96,7 +111,7 @@ OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) if (!response) { - OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed."); + OIC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed."); } OCRepPayloadSetUri (response, gResourceUri); @@ -108,13 +123,13 @@ OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) void ProcessGetPutRequest (OCEntityHandlerRequest *ehRequest) { - OC_LOG(INFO, TAG, "Entering ProcessGetPutRequest"); + OIC_LOG(INFO, TAG, "Entering ProcessGetPutRequest"); OCRepPayload *getResp = constructResponse(ehRequest); if(!getResp) { - OC_LOG(ERROR, TAG, "Failed to construct response"); + OIC_LOG(ERROR, TAG, "Failed to construct response"); return; } @@ -135,15 +150,15 @@ void ProcessGetPutRequest (OCEntityHandlerRequest *ehRequest) // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "Error sending response"); + OIC_LOG(ERROR, TAG, "Error sending response"); } - free(getResp); + OCRepPayloadDestroy(getResp); } OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandlerRequest) { - OC_LOG(INFO, TAG, "Copying received request for slow response"); + OIC_LOG(INFO, TAG, "Copying received request for slow response"); OCEntityHandlerRequest *copyOfRequest = (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest)); @@ -159,7 +174,7 @@ OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandlerRequest copyOfRequest->query = OICStrdup(entityHandlerRequest->query); if(!copyOfRequest->query) { - OC_LOG(ERROR, TAG, "Copy failed due to allocation failure"); + OIC_LOG(ERROR, TAG, "Copy failed due to allocation failure"); OICFree(copyOfRequest); return NULL; } @@ -178,31 +193,46 @@ OCEntityHandlerRequest *CopyRequest(OCEntityHandlerRequest *entityHandlerRequest if (copyOfRequest) { - OC_LOG(INFO, TAG, "Copied client request"); + OIC_LOG(INFO, TAG, "Copied client request"); } else { - OC_LOG(ERROR, TAG, "Error copying client request"); + OIC_LOG(ERROR, TAG, "Error copying client request"); } return copyOfRequest; } +#if !defined(SIGALRM) +void AlarmHandler(int sig); +int WINAPI AlarmThread(void *seconds) +{ + sleep((unsigned int)seconds); + AlarmHandler(0); + return 0; +} + +void alarm(unsigned int seconds) +{ + CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AlarmThread, (void*)seconds, 0, NULL); +} +#endif + OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, - OCEntityHandlerRequest *entityHandlerRequest, void* callbackParam) + OCEntityHandlerRequest *entityHandlerRequest, void* /*callbackParam*/) { OCEntityHandlerResult result = OC_EH_ERROR; OCEntityHandlerRequest *request = NULL; - OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); + OIC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag); if (flag & OC_REQUEST_FLAG) { - OC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG"); + OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG"); if (entityHandlerRequest) { - OC_LOG_V (INFO, TAG, "request query %s from client", + OIC_LOG_V (INFO, TAG, "request query %s from client", entityHandlerRequest->query); - OC_LOG_PAYLOAD (INFO, TAG, entityHandlerRequest->payload); + OIC_LOG_PAYLOAD (INFO, entityHandlerRequest->payload); // Make deep copy of received request and queue it for slow processing request = CopyRequest(entityHandlerRequest); @@ -210,7 +240,7 @@ OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, if (request) { - OC_LOG(INFO, TAG, "Scheduling slow response for received request"); + OIC_LOG(INFO, TAG, "Scheduling slow response for received request"); gRequestList.push_back(request); // Indicate to the stack that this is a slow response result = OC_EH_SLOW; @@ -219,14 +249,14 @@ OCEntityHandlerResult OCEntityHandlerCb (OCEntityHandlerFlag flag, } else { - OC_LOG(ERROR, TAG, "Error queuing request for slow response"); + OIC_LOG(ERROR, TAG, "Error queuing request for slow response"); // Indicate to the stack that this is a slow response result = OC_EH_ERROR; } } else { - OC_LOG(ERROR, TAG, "Invalid request"); + OIC_LOG(ERROR, TAG, "Invalid request"); result = OC_EH_ERROR; } } @@ -246,12 +276,14 @@ void handleSigInt(int signum) // slow response when fired void AlarmHandler(int sig) { +#ifdef SIGALRM if (sig == SIGALRM) +#endif { - OC_LOG (INFO, TAG, "Server starting slow response"); + OIC_LOG (INFO, TAG, "Server starting slow response"); if (gRequestList.empty()) { - OC_LOG (INFO, TAG, "No requests to service"); + OIC_LOG (INFO, TAG, "No requests to service"); return; } @@ -260,17 +292,17 @@ void AlarmHandler(int sig) gRequestList.pop_front(); if (entityHandlerRequest->method == OC_REST_GET) { - OC_LOG (INFO, TAG, "Received OC_REST_GET from client"); + OIC_LOG (INFO, TAG, "Received OC_REST_GET from client"); ProcessGetPutRequest (entityHandlerRequest); } else if (entityHandlerRequest->method == OC_REST_PUT) { - OC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); + OIC_LOG (INFO, TAG, "Received OC_REST_PUT from client"); ProcessGetPutRequest (entityHandlerRequest); } else { - OC_LOG_V (INFO, TAG, "Received unsupported method %d from client", + OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client", entityHandlerRequest->method); } // Free the request @@ -286,37 +318,39 @@ void AlarmHandler(int sig) } } -int main(int argc, char* argv[]) +int main(int /*argc*/, char** /*argv[]*/) { - OC_LOG(DEBUG, TAG, "OCServer is starting..."); + OIC_LOG(DEBUG, TAG, "OCServer is starting..."); if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "OCStack init error"); + OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } // Declare and create the example resource: LED createLEDResource(gResourceUri, &LED, false, 42); +#ifdef SIGALRM // Initialize slow response alarm signal(SIGALRM, AlarmHandler); +#endif // Break from loop with Ctrl-C - OC_LOG(INFO, TAG, "Entering ocserver main loop..."); + OIC_LOG(INFO, TAG, "Entering ocserver main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "OCStack process error"); + OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } sleep(2); } - OC_LOG(INFO, TAG, "Exiting ocserver main loop..."); + OIC_LOG(INFO, TAG, "Exiting ocserver main loop..."); // Free requests if (!gRequestList.empty()) @@ -332,7 +366,7 @@ int main(int argc, char* argv[]) if (OCStop() != OC_STACK_OK) { - OC_LOG(ERROR, TAG, "OCStack process error"); + OIC_LOG(ERROR, TAG, "OCStack process error"); } return 0; @@ -342,7 +376,7 @@ int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, { if (!uri) { - OC_LOG(ERROR, TAG, "Resource URI cannot be NULL"); + OIC_LOG(ERROR, TAG, "Resource URI cannot be NULL"); return -1; } @@ -355,7 +389,7 @@ int createLEDResource (char *uri, LEDResource *ledResource, bool resourceState, OCEntityHandlerCb, NULL, OC_DISCOVERABLE|OC_OBSERVABLE); - OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res)); + OIC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res)); return 0; }