diff options
author | Jinkun Jang <jinkun.jang@samsung.com> | 2013-03-13 01:51:41 +0900 |
---|---|---|
committer | Jinkun Jang <jinkun.jang@samsung.com> | 2013-03-13 01:51:41 +0900 |
commit | f01c3d64d586a3d19517384c15a81f10348dd004 (patch) | |
tree | 19a7ca4c84a61f8b704888274a086bf79b202e95 /src | |
parent | f0a5a09b0466227a44801207ef9b947048b014cd (diff) | |
download | libsf-common-f01c3d64d586a3d19517384c15a81f10348dd004.tar.gz libsf-common-f01c3d64d586a3d19517384c15a81f10348dd004.tar.bz2 libsf-common-f01c3d64d586a3d19517384c15a81f10348dd004.zip |
Tizen 2.1 base
Diffstat (limited to 'src')
-rwxr-xr-x | src/ccatalog.cpp | 28 | ||||
-rwxr-xr-x | src/cipc_worker.cpp | 21 | ||||
-rwxr-xr-x | src/cprocessor_module.cpp | 178 | ||||
-rwxr-xr-x | src/csock.cpp | 53 | ||||
-rwxr-xr-x | src/cworker.cpp | 109 |
5 files changed, 220 insertions, 169 deletions
diff --git a/src/ccatalog.cpp b/src/ccatalog.cpp index 9f6d285..9821988 100755 --- a/src/ccatalog.cpp +++ b/src/ccatalog.cpp @@ -297,6 +297,7 @@ bool ccatalog::unload(void) } + char *ccatalog::value(char *group, char *name) { group_t *grp; @@ -325,33 +326,6 @@ char *ccatalog::value(char *group, char *name) return NULL; } -char *ccatalog::value(char *group, char *name, void *handle) -{ - group_t *grp = (group_t*)handle; - - while (grp) { - if (!strcmp(group, grp->name)) { - break; - } - - grp = (group_t*)grp->next(); - } - - if (grp) { - desc_t *desc; - desc = grp->head; - - while (desc) { - if (!strcmp(name, desc->name)) { - return desc->value; - } - desc = (desc_t*)desc->next(); - } - } - - return NULL; -} - char *ccatalog::value(char *group, char *name, int idx) { group_t *grp; diff --git a/src/cipc_worker.cpp b/src/cipc_worker.cpp index f93074f..2799ed5 100755 --- a/src/cipc_worker.cpp +++ b/src/cipc_worker.cpp @@ -134,31 +134,28 @@ void *cipc_worker::started(void *data) inst->m_state = TERMINATE; pthread_mutex_unlock(&(inst->mutex_lock)); inst->m_func[STOP](inst->m_context); + delete inst; return NULL; } }while(state == START && inst->m_state == START); DBG("\n\n\n############Client worker thread END############\n\n\n"); + pthread_mutex_lock(&(inst->mutex_lock)); + inst->m_state = STOPPED; + pthread_mutex_unlock(&(inst->mutex_lock)); + delete inst; return NULL; } bool cipc_worker::stop(void) { ipc_worker_state_s state; - pthread_mutex_lock(&(mutex_lock)); - m_state = STOP; - pthread_mutex_unlock(&(mutex_lock)); - DBG("Stop function [Client worker]\n"); - + state = (ipc_worker_state_s)(int)m_func[STOP](m_context); - if (state == TERMINATE) { - pthread_mutex_lock(&(mutex_lock)); - m_state = TERMINATE; - pthread_mutex_unlock(&(mutex_lock)); - delete this; - return false; - } + pthread_mutex_lock(&(mutex_lock)); + m_state = TERMINATE; + pthread_mutex_unlock(&(mutex_lock)); return true; } diff --git a/src/cprocessor_module.cpp b/src/cprocessor_module.cpp index dd08236..7725477 100755 --- a/src/cprocessor_module.cpp +++ b/src/cprocessor_module.cpp @@ -57,10 +57,13 @@ #include <cfilter_module.h> #include <cprocessor_module.h> +#define BASE_GATHERING_INTERVAL 1000 +#define MS_TO_US 1000 + cprocessor_module::cprocessor_module() -: m_cb_head(NULL) -, m_cb_tail(NULL) -, m_worker(NULL) +: m_worker(NULL) +, m_interval_list_head(NULL) +, m_interval_list_tail(NULL) { ctype::set_type(SF_PLUGIN_PROCESSOR); @@ -107,28 +110,6 @@ bool cprocessor_module::stop(void) bool cprocessor_module::add_event_callback(void *(*handler)(cprocessor_module *, void *), void *data, bool (*rm_cb_data)(void *data)) { - event_callback_t *item; - - try { - item = new event_callback_t; - } catch (...) { - ERR("Failed to allocate memory\n"); - return false; - } - - item->handler = handler; - item->inst = this; - item->data = data; - item->rm_cb_data = rm_cb_data; - - if (m_cb_head == NULL || m_cb_tail == NULL) { - m_cb_tail = m_cb_head = item; - } else { - item->link(clist::AFTER, m_cb_tail); - m_cb_tail = item; - } - DBG("Event ADDED =======================\n"); - return true; } @@ -136,64 +117,13 @@ bool cprocessor_module::add_event_callback(void *(*handler)(cprocessor_module * bool cprocessor_module::rm_event_callback(void *(*handler)(cprocessor_module *, void*), void *data) { - event_callback_t *iterator; - event_callback_t *next; - bool found = false; - - iterator = m_cb_head; - while (iterator) { - next = (event_callback_t*)iterator->next(); - - if (iterator->handler == handler && iterator->data == data) { - event_callback_t *prev = (event_callback_t*)iterator->prev(); - - iterator->unlink(); - - if (iterator == m_cb_tail) { - m_cb_tail = prev; - } - - if (iterator == m_cb_head) { - m_cb_head = next; - } - - if (iterator->rm_cb_data) { - iterator->rm_cb_data(iterator->data); - } - - delete iterator; - iterator=NULL; - found = true; - break; - } - - iterator = next; - } - - return found; + return true; } void cprocessor_module::wakeup_all_client(void) { - event_callback_t *iterator; - event_callback_t *next; - - lock(); - iterator = m_cb_head; - while (iterator) { - next = (event_callback_t*)iterator->next(); - - if (this == iterator->inst) { - iterator->handler(this, iterator->data); - rm_event_callback(iterator->handler, iterator->data); - DBG("Wakeup all client $$$$$$$$$$$$$$$$$$$$$$$$4\n"); - } - - iterator = next; - } - unlock(); } void cprocessor_module::lock(void) @@ -208,4 +138,98 @@ void cprocessor_module::unlock(void) m_mutex.unlock(); } +int cprocessor_module::add_interval_to_list(int interval, unsigned long polling_interval) +{ + interval_list_t *interval_data = new interval_list_t; + interval_data->interval = interval; + + if(!m_interval_list_head && !m_interval_list_tail){ + m_interval_list_head = m_interval_list_tail = interval_data; + } else{ + interval_data->link(clist::AFTER, m_interval_list_tail); + m_interval_list_tail = interval_data; + } + + return (polling_interval > interval * MS_TO_US) ? 0 : -1; +} + +int cprocessor_module::del_interval_to_list(int interval, unsigned long polling_interval) +{ + interval_list_t *iterator = m_interval_list_head; + unsigned int min_interval = BASE_GATHERING_INTERVAL; + + //find interval passed by param and remove + while(iterator){ + if(interval == iterator->interval) + { + if(iterator->next() == NULL) + m_interval_list_tail = (interval_list_t*)iterator->prev(); + if(iterator->prev() == NULL) + m_interval_list_head = (interval_list_t*)iterator->next(); + iterator->unlink(); + delete iterator; + break; + } + iterator = (interval_list_t*)iterator->next(); + } + + //find min interval value in list + interval_list_t *iterator_min_search = m_interval_list_head; + + while(iterator_min_search){ + if(iterator_min_search->interval <= min_interval){ + min_interval = iterator_min_search->interval; + } + iterator_min_search = (interval_list_t*)iterator_min_search->next(); + } + + return min_interval; +} + + +int cprocessor_module::check_hz(int time_ms) +{ + if(time_ms >= POLL_1HZ_MS) + return POLL_1HZ; + else if (time_ms < POLL_1HZ_MS && time_ms >= POLL_5HZ_MS) + return POLL_5HZ; + else if (time_ms < POLL_5HZ_MS && time_ms >= POLL_10HZ_MS) + return POLL_10HZ; + else if (time_ms < POLL_10HZ_MS && time_ms >= POLL_20HZ_MS) + return POLL_20HZ; + else if (time_ms < POLL_20HZ_MS && time_ms >= POLL_25HZ_MS) + return POLL_25HZ; + else if (time_ms < POLL_25HZ_MS && time_ms >= POLL_50HZ_MS) + return POLL_50HZ; + else if (time_ms < POLL_50HZ_MS && time_ms >= POLL_100HZ_MS) + return POLL_100HZ; + else + { + DBG("Cannot support input time [%d]",time_ms); + return -1; + } +} + +int cprocessor_module::norm_interval(int time_ms) +{ + if(time_ms >= POLL_1HZ_MS) + return POLL_1HZ_MS; //1000ms + else if (time_ms < POLL_1HZ_MS && time_ms >= POLL_5HZ_MS) + return POLL_5HZ_MS; //200ms + else if (time_ms < POLL_5HZ_MS && time_ms >= POLL_10HZ_MS) + return POLL_10HZ_MS; //100ms + else if (time_ms < POLL_10HZ_MS && time_ms >= POLL_20HZ_MS) + return POLL_20HZ_MS; //50ms + else if (time_ms < POLL_20HZ_MS && time_ms >= POLL_25HZ_MS) + return POLL_25HZ_MS; //40ms + else if (time_ms < POLL_25HZ_MS && time_ms >= POLL_50HZ_MS) + return POLL_50HZ_MS; // 20ms + else if (time_ms < POLL_50HZ_MS && time_ms >= POLL_100HZ_MS) + return POLL_100HZ_MS;//10ms + else + { + DBG("Cannot support input time [%d]",time_ms); + return -1; + } +} //! End of a file diff --git a/src/csock.cpp b/src/csock.cpp index 8a14d89..5f189eb 100755 --- a/src/csock.cpp +++ b/src/csock.cpp @@ -49,7 +49,8 @@ extern int errno; - +const int MAX_CONNECT = 10; +const int DELAY_FOR_CONNECT = 10000; csock::csock(int handle, int mode) @@ -85,6 +86,7 @@ csock::csock(char *name, int mode, int port, int server) int domain; int type; sockaddr *sock_ptr = NULL; + struct timeval tv; m_start=NULL; m_running=NULL; @@ -212,6 +214,14 @@ csock::csock(char *name, int mode, int port, int server) } } } + else + { + tv.tv_sec = 3; + tv.tv_usec = 0; + + if( setsockopt(m_handle, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) + ERR("setsockopt fail"); + } } @@ -248,6 +258,7 @@ void *csock::client_ctx(void) bool csock::connect_to_server(void) { int type = (m_mode & SOCK_UDP) ? SOCK_DGRAM : SOCK_STREAM; + int i = 0; if (type == SOCK_STREAM) { int len; @@ -261,12 +272,23 @@ bool csock::connect_to_server(void) sock_ptr = (struct sockaddr*)&m_addr; } - if (connect(m_handle, sock_ptr, len) < 0) { - ERR("connect fail , m_handle : %d , sock_ptr : %p , len : %d ,%s\n",m_handle , sock_ptr , len, strerror(errno)); - close(m_handle); - m_handle = -1; - return false; + for(i = 0 ; i < MAX_CONNECT ; i++) + { + if (connect(m_handle, sock_ptr, len) == 0) + { + return true; + } + else + { + DBG("wait for accept worker"); + usleep(DELAY_FOR_CONNECT); + } } + + ERR("connect fail , m_handle : %d , sock_ptr : %p , len : %d ,%s\n",m_handle , sock_ptr , len, strerror(errno)); + close(m_handle); + m_handle = -1; + return false; } return true; @@ -428,6 +450,7 @@ bool csock::recv(void *buffer, int size) ssize_t recv_size; int total_recv_size = 0; + DBG("Recv message : data size is %d\n", size); if (m_handle < 0) { ERR("Invalid handle\n"); return false; @@ -438,6 +461,12 @@ bool csock::recv(void *buffer, int size) return true; } + if (size < 0) + { + ERR("invalid size of packet"); + return false; + } + if (m_mode & SOCK_UDP) { sockaddr *sock_ptr; socklen_t len; @@ -456,8 +485,8 @@ bool csock::recv(void *buffer, int size) DBG("recvfrom %s\n", (char*)buffer); } else { do { - recv_size = read(m_handle, - (char*)buffer + total_recv_size, size - total_recv_size); + recv_size = ::recv(m_handle, (char*)buffer + total_recv_size, size - total_recv_size, MSG_NOSIGNAL |MSG_WAITALL); + if (recv_size <= 0) { ERR("Error recv_size check fail , recv_size : %d\n",recv_size); close(m_handle); @@ -488,6 +517,12 @@ bool csock::send(void *buffer, int size) if (size == 0) { return true; } + + if (size < 0) + { + ERR("invalid size of packet"); + return false; + } if (m_handle < 0) { ERR("Invalid handle\n"); @@ -520,7 +555,7 @@ bool csock::send(void *buffer, int size) } else if (m_mode & SOCK_TCP) { DBG("TCP send enabled\n"); - send_size = write(m_handle, buffer, size); + send_size = ::send(m_handle, buffer, size, MSG_NOSIGNAL); if (send_size <= 0) { ERR("Error send_size check fail , send_size : %d\n",send_size); close(m_handle); diff --git a/src/cworker.cpp b/src/cworker.cpp index 09143fe..81e0c78 100755 --- a/src/cworker.cpp +++ b/src/cworker.cpp @@ -37,25 +37,44 @@ #include <cworker.h> #include <common.h> -cworker::cworker(void) -: m_state(STOPPED) -, m_context(NULL) -, mutex_lock(PTHREAD_MUTEX_INITIALIZER) + cworker::cworker(void) + : m_state(INITIAL) + , m_context(NULL) + , mutex_lock(PTHREAD_MUTEX_INITIALIZER) + , th_cond(PTHREAD_COND_INITIALIZER) { register int i; - + int ret; for (i = 0; i < ENUM_LAST; i ++) { m_func[i] = NULL; } - + + ret = pthread_mutex_init(&mutex_lock, NULL); + if (ret != 0) { + ERR("pthread_mutex_init : %s",strerror(errno)); + throw EINVAL; + } + + ret = pthread_cond_init(&th_cond, NULL); + if (ret != 0) { + ERR("pthread_cond_init : %s",strerror(errno)); + throw EINVAL; + } DBG("processor worker created\n"); } cworker::~cworker(void) { DBG("----------Processor WORKER TERMINATED--------\n"); - + + pthread_mutex_lock(&(mutex_lock)); + m_state = TERMINATE; + pthread_cond_signal(&th_cond); + + pthread_mutex_unlock(&(mutex_lock)); + + pthread_cond_destroy(&th_cond); if (m_func[TERMINATE]) m_func[TERMINATE](m_context); @@ -68,41 +87,41 @@ bool cworker::start(void) int ret = 0; pthread_mutex_lock(&(mutex_lock)); - if (m_state == START) { - ERR("Already started\n"); - pthread_mutex_unlock(&(mutex_lock)); - return false; - } - m_state = START; - pthread_mutex_unlock(&(mutex_lock)); - - DBG("cworker start\n"); - - ret = pthread_create(&m_thid, NULL, started, this); - - if(ret != 0) + if(m_state == INITIAL) { - pthread_mutex_lock(&(mutex_lock)); - m_state = STOP; pthread_mutex_unlock(&(mutex_lock)); - ERR("thread create fail\n"); - return false; - } - else - { - ret = pthread_detach(m_thid); + ret = pthread_create(&m_thid, NULL, started, this); + if(ret != 0) { - ERR("thread detach fail\n"); + pthread_mutex_lock(&(mutex_lock)); + m_state = INITIAL; + pthread_mutex_unlock(&(mutex_lock)); + ERR("thread create fail\n"); return false; } else { - DBG("Thread creation for Processor worker END\n"); + pthread_detach(m_thid); } + + pthread_mutex_lock(&(mutex_lock)); } - + else if (m_state == START) { + ERR("Already started\n"); + pthread_mutex_unlock(&(mutex_lock)); + return false; + } + + ret = pthread_cond_signal(&th_cond); + if (ret != 0) { + ERR("pthread_cond_wait : %s",strerror(errno)); + } + + m_state = START; + pthread_mutex_unlock(&(mutex_lock)); + return true; } @@ -118,22 +137,17 @@ bool cworker::terminate(void) void *cworker::started(void *data) { cworker *inst = (cworker*)data; - worker_state_s state; + worker_state_s state = STOPPED; do { state = (worker_state_s)(int)inst->m_func[STARTED](inst->m_context); - if (state == STOPPED) { - pthread_mutex_lock(&(inst->mutex_lock)); - inst->m_state = STOP; - pthread_mutex_unlock(&(inst->mutex_lock)); - ERR("Abnormal Situation: processor_plugin->working() returned STOPPED\n"); - return NULL; - } - }while(state == STARTED && inst->m_state == START); - DBG("\n\n\n#############Processor worker thread END###########\n\n\n"); - + if(state == STOPPED || inst->m_state == STOP) + inst->stopped(); + + }while(state != TERMINATE); + return NULL; } @@ -145,10 +159,17 @@ bool cworker::stop(void) pthread_mutex_unlock(&(mutex_lock)); return false; } - m_state = STOP; pthread_mutex_unlock(&(mutex_lock)); - DBG("Stop function for Processor worker END "); + + return true; +} + +bool cworker::stopped(void) +{ + pthread_mutex_lock(&mutex_lock); + pthread_cond_wait(&th_cond, &mutex_lock); + pthread_mutex_unlock(&mutex_lock); return true; } |