19 #include <glib-2.0/glib.h> 22 #include <sys/types.h> 31 static int io_event_callback(sd_event_source *src,
int fd, uint32_t revents,
void *closure);
32 static vector<string> split(
const string& str,
char sep);
34 static sd_event_source *evsrc;
35 static GMainLoop *loop = NULL;
36 static void call_test();
40 cout <<
"verb "<<
"key:arg" << endl;
41 cout <<
"example: connect sourceID 100 sinkID 100" << endl;
42 cout <<
"verb list:" << endl;
47 static void *event_loop_run(
void *args)
52 static int io_event_callback(sd_event_source *src,
int fd, uint32_t revents,
void *closure)
57 static void call_test()
61 cout <<
"input verb and argments" << endl;
66 cin.getline(line,
sizeof(line));
72 vector<string> v_command = split(command,
' ');
77 size_t num = v_command.size();
79 cout <<
"If command contains args, please input <key,value> in argument part" << endl;
83 struct json_object* j_obj = json_object_new_object();
84 for(
int i = 1;i < (v_command.size()) ;++i){
85 struct json_object* val = json_object_new_string(v_command[i+1].c_str());
86 json_object_object_add(j_obj, v_command[i].c_str(), val);
90 sm->
call(v_command[0], j_obj);
92 vector<string>().swap(v_command);
93 string().swap(command);
97 static void onRep(
struct json_object* reply_contents)
99 const char* str = json_object_to_json_string(reply_contents);
100 cout <<
"[CB onRep]: " << str << endl;
101 json_object_put(reply_contents);
104 static void onEv(
const string&
event,
struct json_object* event_contents)
106 const char* str = json_object_to_json_string(event_contents);
107 cout <<
"[CB onEvent]: event" <<
event.c_str() <<
" contents:" << str << endl;
108 json_object_put(event_contents);
111 static vector<string> split(
const string& str,
char sep)
114 stringstream ss(str);
116 while( getline(ss, buffer, sep) ) {
123 int main(
int argc,
char **argv)
128 printf(
"Please input port num in first argument, and token in second argument");
135 if( (av ==
"-h") || (av ==
"--help"))
142 string port_string(argv[1]);
143 string token(argv[2]);
145 long port = strtol(port_string.c_str(),&endptr,10);
148 if( (port > 20000) || (port < 0) )
150 printf(
"input under 20000(temporary number)");
155 printf(
"not number");
159 cout <<
"Call test for libsoundmanager" << endl;
163 ret = pthread_create(&thread_id, NULL, event_loop_run, NULL);
166 ret = sd_event_default(&sloop);
169 printf(
"failed to create event loop");
173 sd_event_add_io(sloop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
175 sd_event_run(sloop, 30000000);
int call(const std::string &verb, struct json_object *arg)
void register_callback(void(*event_cb)(const std::string &event, struct json_object *event_contents), void(*reply_cb)(struct json_object *reply_contents))
int main(int argc, char **argv)