design_pattern_for_c  V 1.00
state_machine.c File Reference

Implement of StateMachine library API, defined in state_machine.h. More...

#include <stdlib.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <errno.h>
#include "state_machine.h"
#include "dp_util.h"
Include dependency graph for state_machine.c:

Go to the source code of this file.

Data Structures

struct  state_manager_list_data_t
 information of StateMachine, to use as list More...
 
struct  state_machine_msg_list_t
 information of state machine message list More...
 
struct  state_machine_t
 StateMachine class member definition. More...
 
struct  state_machine_msg_t
 message definition for multi thread More...
 

Typedefs

typedef struct state_manager_list_data_tStateManagerListData
 

Functions

StateMachineInfo state_machine_new (size_t event_num, const state_event_info_t *event_infos, EventTPoolManager threadpool)
 Create StateMachineInfo class. More...
 
int state_machine_update_machine (StateMachineInfo this, const state_event_info_t *event_info)
 update sate More...
 
void state_machine_set_state (StateMachineInfo this, int state)
 set state More...
 
int state_machine_get_current_state (StateMachineInfo this)
 get state More...
 
int state_machine_call_event (StateMachineInfo this, int event, void *arg, int arglen, void(*response)(int result))
 call event trigger More...
 
void state_machine_show (StateMachineInfo this)
 set state More...
 
void state_machine_free (StateMachineInfo this)
 free StateMachine class More...
 
private API for StateMachine
static int state_machine_add_new_states (StateMachine this, const state_event_info_t *event_info)
 Add new state. More...
 
static void state_machine_free_states_normally (StateMachine this)
 free normally More...
 
StateManagerListData state_machine_find_states (StateMachine this, int event)
 Find state. More...
 
static int state_machine_initial_thread (StateMachine this)
 for multi thread, initialize More...
 
static void state_machine_free_states_multithread (StateMachine this)
 for multi thread, free More...
 
static int state_machine_call_event_normally (StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
 for single thread, call event More...
 
static int state_machine_call_event_normal (StateMachine this, int event, void *arg)
 
static int state_machine_call_event_multithread (StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
 for multi thread, call event More...
 
static void state_machine_thread_main (int socketfd, int eventflag, void *event_arg)
 for multi thread, thread main callback for threadpool More...
 
static int state_machine_open_socket (StateMachine this)
 for multi thread, open socket More...
 
static void state_machine_close_socket (StateMachine this)
 for multi thread, close socket More...
 
static int state_machine_get_read (StateMachine this)
 for multi thread, get read sock More...
 
static int state_machine_write (StateMachine this, state_machine_msg_t *msg)
 for multi thread, write More...
 

private API for state_manager_t

#define SMACHINE_LOCK(this)   DPUTIL_LOCK(&this->msglist.lock);
 
#define SMACHINE_UNLOCK   DPUTIL_UNLOCK
 
typedef struct state_machine_msg_t state_machine_msg_t
 
typedef struct state_machine_msg_tStateMachineMsg
 
typedef struct state_machine_msg_list_t state_machine_msg_list_t
 
static StateManagerListData state_machine_manager_list_new (const state_event_info_t *event_info)
 Add new state. More...
 
static void state_machine_manager_list_free (StateManagerListData this)
 

Detailed Description

Implement of StateMachine library API, defined in state_machine.h.

Definition in file state_machine.c.

Macro Definition Documentation

◆ SMACHINE_LOCK

#define SMACHINE_LOCK (   this)    DPUTIL_LOCK(&this->msglist.lock);

Definition at line 66 of file state_machine.c.

◆ SMACHINE_UNLOCK

#define SMACHINE_UNLOCK   DPUTIL_UNLOCK

Definition at line 67 of file state_machine.c.

Typedef Documentation

◆ state_machine_msg_list_t

Definition at line 48 of file state_machine.c.

◆ state_machine_msg_t

Definition at line 40 of file state_machine.c.

◆ StateMachineMsg

Definition at line 40 of file state_machine.c.

◆ StateManagerListData

Definition at line 24 of file state_machine.c.

Function Documentation

◆ state_machine_add_new_states()

static int state_machine_add_new_states ( StateMachine  this,
const state_event_info_t event_info 
)
static

Add new state.

Definition at line 147 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_call_event()

int state_machine_call_event ( StateMachineInfo  this,
int  event,
void *  arg,
int  arglen,
void(*)(int result)  response 
)

call event trigger

Parameters
[in]thisStateMachineInfo class instance returned at state_machine_new
[in]eventevent id related to this function
[in]argevent argument
[in]arglenevent argument len
[in]responseresponse callback method. If you set is_multithread=true , you must set this response callback,
Return values
return_valueof method if you set by single thread mode
STATE_MNG_SUCCESSand result is in callback you set callback if you set by multi thread mode.

Definition at line 413 of file state_machine.c.

◆ state_machine_call_event_multithread()

static int state_machine_call_event_multithread ( StateMachine  this,
int  event,
void *  arg,
int  arglen,
void(*)(int result)  response 
)
static

for multi thread, call event

Definition at line 227 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_call_event_normal()

static int state_machine_call_event_normal ( StateMachine  this,
int  event,
void *  arg 
)
inlinestatic

Definition at line 217 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_call_event_normally()

static int state_machine_call_event_normally ( StateMachine  this,
int  event,
void *  arg,
int  arglen,
void(*)(int result)  response 
)
static

for single thread, call event

Definition at line 211 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_close_socket()

static void state_machine_close_socket ( StateMachine  this)
static

for multi thread, close socket

Definition at line 291 of file state_machine.c.

Here is the caller graph for this function:

◆ state_machine_find_states()

StateManagerListData state_machine_find_states ( StateMachine  this,
int  event 
)

Find state.

Definition at line 169 of file state_machine.c.

Here is the caller graph for this function:

◆ state_machine_free()

void state_machine_free ( StateMachineInfo  this)

free StateMachine class

Parameters
[in]thisStateMachine class instance returned at state_machine_new
Returns
none

Definition at line 438 of file state_machine.c.

Here is the caller graph for this function:

◆ state_machine_free_states_multithread()

static void state_machine_free_states_multithread ( StateMachine  this)
static

for multi thread, free

Definition at line 199 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_free_states_normally()

static void state_machine_free_states_normally ( StateMachine  this)
static

free normally

Definition at line 160 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_get_current_state()

int state_machine_get_current_state ( StateMachineInfo  this)

get state

Parameters
[in]thisStateMachineInfo class instance returned at state_machine_new
Returns
state

Definition at line 402 of file state_machine.c.

Here is the call graph for this function:

◆ state_machine_get_read()

static int state_machine_get_read ( StateMachine  this)
inlinestatic

for multi thread, get read sock

for multi thread, read

Definition at line 297 of file state_machine.c.

Here is the caller graph for this function:

◆ state_machine_initial_thread()

static int state_machine_initial_thread ( StateMachine  this)
static

for multi thread, initialize

Definition at line 181 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_manager_list_free()

static void state_machine_manager_list_free ( StateManagerListData  this)
static

Definition at line 131 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_manager_list_new()

static StateManagerListData state_machine_manager_list_new ( const state_event_info_t event_info)
static

Add new state.

Definition at line 113 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_new()

StateMachineInfo state_machine_new ( size_t  event_num,
const state_event_info_t event_infos,
EventTPoolManager  threadpool 
)

Create StateMachineInfo class.

Parameters
[in]event_numevent size
[in]event_infoslist of event state data
[in]threadpoolevent threadpool instance by creating event_threadpool API if you want to use state machine in other threads
Return values
!=NULLthis class handle
NULLerror

Definition at line 313 of file state_machine.c.

Here is the call graph for this function:

◆ state_machine_open_socket()

static int state_machine_open_socket ( StateMachine  this)
inlinestatic

for multi thread, open socket

Definition at line 284 of file state_machine.c.

Here is the caller graph for this function:

◆ state_machine_set_state()

void state_machine_set_state ( StateMachineInfo  this,
int  state 
)

set state

Parameters
[in]thisStateMachineInfo class instance returned at state_machine_new
[in]stateupdate state, if there is no state in set list, state is changed to latest order.
Returns
none

Definition at line 388 of file state_machine.c.

Here is the call graph for this function:

◆ state_machine_show()

void state_machine_show ( StateMachineInfo  this)

set state

Parameters
[in]thisStateMachine class instance returned at state_machine_new
Returns
none

Definition at line 425 of file state_machine.c.

Here is the call graph for this function:

◆ state_machine_thread_main()

static void state_machine_thread_main ( int  socketfd,
int  eventflag,
void *  event_arg 
)
static

for multi thread, thread main callback for threadpool

for multi thread, thread main

Definition at line 248 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ state_machine_update_machine()

int state_machine_update_machine ( StateMachineInfo  this,
const state_event_info_t event_info 
)

update sate

Parameters
[in]thisStateMachineInfo class instance returned at state_machine_new
[in]event_infoupdate list of event state data.
Return values
STATE_MNG_SUCCESSsuccess
otherfailed

Definition at line 360 of file state_machine.c.

Here is the call graph for this function:

◆ state_machine_write()

static int state_machine_write ( StateMachine  this,
state_machine_msg_t msg 
)
inlinestatic

for multi thread, write

Definition at line 302 of file state_machine.c.

Here is the call graph for this function:
Here is the caller graph for this function: