void t3_notify(struct curl_client_t* cc, void* args) { struct task_step_t* ts = (struct task_step_t*)(args); struct task_step_result_t* rt = NULL; struct task_step_t* t1 = NULL; t1_data_t* td = NULL; if (curl_client_err_code(cc) != CURLE_OK) { printf("task step[%x] asynchronous notify, fail\n", task_step_id(ts)); rt = task_step_result_init(TASK_RET_FAIL); task_step_finish(ts, rt); task_step_result_release(rt); } printf("task step[%x] asynchronous notify, complete\n", task_step_id(ts)); t1 = task_get_step(task_step_task(ts), id); assert(t1); td = (t1_data_t*)task_step_data(t1); td->result --; if (td->result > 0) { printf("need to loop %d times\n", td->result); rt = task_step_result_init(TASK_RET_NEXT); task_step_result_set_backward(rt); task_step_finish(ts, rt); task_step_result_release(rt); } else { rt = task_step_result_init(TASK_RET_SUCCESS); task_step_finish(ts, rt); task_step_result_release(rt); } }
static void t2_notify(curlc_t* cc, void* args) { task_step_t* ts = (task_step_t*)(args); if (curlc_ret(cc) != CURLE_OK) { printf("task step[%x] asynchronous notify, fail\n", task_step_id(ts)); task_step_resume(ts, TASK_RET_FAIL, 0); } else { printf("task step[%x] asynchronous notify, complete\n", task_step_id(ts)); task_step_resume(ts, TASK_RET_NEXT, 1); } }
int32_t t2_run(struct task_step_t* ts) { int32_t ret = curl_pool_add_get_req(cp, "www.baidu.com", t2_notify, ts, NULL); assert(ret == 0); printf("task step[%x] asynchronous running\n", task_step_id(ts)); return TASK_RET_RUNNING; }
void t2_notify(struct curl_client_t* cc, void* args) { struct task_step_t* ts = (struct task_step_t*)(args); struct task_step_result_t* rt = NULL; if (curl_client_err_code(cc) != CURLE_OK) { printf("task step[%x] asynchronous notify, fail\n", task_step_id(ts)); rt = task_step_result_init(TASK_RET_FAIL); task_step_finish(ts, rt); task_step_result_release(rt); } else { printf("task step[%x] asynchronous notify, complete\n", task_step_id(ts)); rt = task_step_result_init(TASK_RET_NEXT); task_step_finish(ts, rt); task_step_result_release(rt); } }
static int t3_run(struct task_step_t* ts) { int ret = curlp_add_get(_curl_pool, "www.soso.com", t3_notify, ts, NULL); if (ret < 0) { fprintf(stderr, "curl add step fail\n"); return TASK_RET_FAIL; } printf("task step[%x] asynchronous running\n", task_step_id(ts)); return TASK_RET_RUNNING; }
int main(int argc, char** argv) { struct heaptimer_t* timer; struct task_t* t; struct task_step_t* t1, *t2, *t3; struct timeval timeout; struct timeval tv; int32_t loop = argc > 1 ? atoi(argv[1]) : 3; cp = curl_pool_init(); assert(cp); timer = timer_init(); assert(timer); t = task_init(on_success, on_fail, (void*)&loop); assert(t); t1 = task_step_init(t1_run); assert(t1); task_push_back_step(t, t1); id = task_step_id(t1); t2 = task_step_init(t2_run); assert(t2); task_push_back_step(t, t2); t3 = task_step_init(t3_run); assert(t3); task_push_back_step(t, t3); timeout.tv_sec = 1; timeout.tv_usec = 0; task_run(t, timer, &timeout); while (1) { if (curl_pool_running_count(cp) > 0) { curl_pool_run(cp); } util_gettimeofday(&tv, NULL); timer_poll(timer, &tv); if (task_finished(t) == 0) { break; } } task_release(t); timer_release(timer); curl_pool_release(cp); return 0; }
int32_t t1_run(struct task_step_t* ts) { struct t1_data_t* td; void* arg; td = (t1_data_t*)MALLOC(sizeof(*td)); task_step_set_data(ts, td, t1_data_release); arg = task_data(task_step_task(ts)); assert(arg); td->result = *(int32_t*)arg; printf("task step[%x] run complete\n", task_step_id(ts)); return TASK_RET_NEXT; }
static void t3_notify(curlc_t* cc, void* args) { task_step_t* ts = (struct task_step_t*)(args); if (curlc_ret(cc) != CURLE_OK) { printf("task step[%x] asynchronous notify, fail\n", task_step_id(ts)); task_step_resume(ts, TASK_RET_FAIL, 0); } printf("task step[%x] asynchronous notify, complete\n", task_step_id(ts)); task_step_t* t1 = task_get_step(task_step_task(ts), _task_step_id); if (!t1) { fprintf(stderr, "task get step fail"); return; } t1_param_t* tsp = (t1_param_t*)task_step_param(t1); if (tsp->loop -- > 0) { printf("need to loop %d times\n", tsp->loop); task_step_resume(ts, TASK_RET_NEXT, -1); } else { task_step_resume(ts, TASK_RET_SUCCESS, 0); } }
static int t1_run(task_step_t* ts) { printf("task step[%x] run complete\n", task_step_id(ts)); return TASK_RET_NEXT; }
int test_logic_task(const char* param) { timerheap_t* timer = timer_create_heap(); if (!timer) { fprintf(stderr, "timer create fail\n"); return -1; } _curl_pool = curlp_create(); if (!_curl_pool) { fprintf(stderr, "curl create fail\n"); timer_release(timer); return -1; } task_t* t = task_create(on_success, on_fail, NULL); if (!t) { fprintf(stderr, "task create fail\n"); timer_release(timer); curlp_release(_curl_pool); return -1; } struct timeval timeout; struct timeval tv; t1_param_t p; p.loop = (param ? atoi(param) : 3); task_step_t* t1 = task_step_create(t1_run, (void*)&p); task_step_t* t2 = task_step_create(t2_run, NULL); task_step_t* t3 = task_step_create(t3_run, NULL); if (!t1 || !t2 || !t3) { fprintf(stderr, "task step create fail\n"); if (t1) task_step_release(t1); if (t2) task_step_release(t2); if (t3) task_step_release(t3); task_release(t); timer_release(timer); curlp_release(_curl_pool); return -1; } task_push_back_step(t, t1); _task_step_id = task_step_id(t1); task_push_back_step(t, t2); task_push_back_step(t, t3); timeout.tv_sec = 3; timeout.tv_usec = 0; task_run(t, timer, &timeout); while (1) { if (curlp_running_count(_curl_pool) > 0) { curlp_poll(_curl_pool); } gettimeofday(&tv, NULL); timer_poll(timer, &tv); if (task_is_finished(t) == 0) { break; } } task_release(t); timer_release(timer); curlp_release(_curl_pool); return 0; }