// File: /workspace/github_c_makefile_star2_O2/loversmile[P]sugarcrm/sugar_client.c
// Function: crmClient
void *crmClient(void *data)
{
    char *server = "http://192.168.124.27/crm/service/v4_1/soap.php";
    //char *server = "http://192.168.124.99/crm/service/v4_1/soap.php";
    
    //struct login_return = NULL;
    //login_return = initLoginReturn(login_return);
    int normal = 1;
    int time_now = 0;
    
    SGCRMInfoTask *info_task = NULL;
    info_task = initSGCRMInfoTask(info_task);
    //info_task->server_info = initSGServerInfo(info_task->server_info);
    info_task->server_info->soap = soap_new();
    info_task->server_info->soap_endpoint = strdup(server);
    
    queueInitialize(&request_crm_queue);
    
    SGCRMTask *crm_task = NULL;
    
    char *username = "admin";
    char *password = "123456";
    int login_ret = -1;
    int ret = -1;
    while(g_crm_task) {
        printf("%d\n", time(NULL));
        printf("%d\n", time_now);
        //This need a function to monitor crmtask
		monitorTaskAll();

        if (normal == 1 && (time(NULL) - time_now) >= 1790 )
        {
            time_now = time(NULL);
            //info_task->login_return = initSGLoginReturn(info_task->login_return);
            login_ret = SGCRMLogin(info_task->server_info, username, password, info_task->login_return); 
        } 
        else if (normal == 0)
        {
            //info_task->login_return = initSGLoginReturn(info_task->login_return);
            login_ret = SGCRMLogin(info_task->server_info, username, password, info_task->login_return);
        }

        if (login_ret == 0 && info_task->login_return && 
                info_task->login_return->session_id && 
                info_task->login_return->user_id)
        {
            printf("session = %s\n", info_task->login_return->session_id);
            printf("user    = %s\n", info_task->login_return->user_id);
            normal = 1;
        } 
        else 
        {
            printf("login error login_ret = %d\n", login_ret);
            normal = 0;
            sleep(1);
            continue;
        }
        
        while(DEQUEUE_SUCCESS_AND_CONTINUE == (ret = dequeueTask(&request_crm_queue, &crm_task)) || DEQUEUE_SUCCESS_AND_EMPTY == ret)
        {
            if (crm_task)
            {
                int res = executeRequest(info_task, crm_task);
                
            }
        }
        cleanSGCRMTask(crm_task);
        sleep(1);
		//break;
    }
	cleanSGCRMInfoTask(info_task);
    cleanQueue(&request_crm_queue);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x473a8
movq %rax, %rbx
callq 0x3acbd
movq 0x8(%rbx), %r14
movq %rax, (%r14)
leaq 0x58a5(%rip), %rdi      # 0x4c86c
callq 0x33a0
movq %rax, 0x8(%r14)
leaq 0x29361(%rip), %rdi     # 0x70338
callq 0x471eb
movq %rsp, %rax
andq $0x0, (%rax)
movb $0x1, %r14b
pushq $-0x1
popq %rbp
xorl %r13d, %r13d
cmpl $0x0, 0x293ad(%rip)     # 0x703a0
je 0x47112
xorl %edi, %edi
callq 0x3250
leaq 0x80af(%rip), %r15      # 0x4f0b6
movq %r15, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x3140
movq %r15, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x3140
callq 0x46efc
testb $0x1, %r14b
je 0x4704d
xorl %edi, %edi
callq 0x3250
movslq %r13d, %rcx
subq %rcx, %rax
cmpq $0x6fe, %rax            # imm = 0x6FE
jl 0x4704d
xorl %edi, %edi
callq 0x3250
movq %rax, %r13
jmp 0x47053
testb $0x1, %r12b
je 0x4706f
movq (%rbx), %rcx
movq 0x8(%rbx), %rdi
leaq 0x7d83(%rip), %rsi      # 0x4ede4
leaq 0x7d82(%rip), %rdx      # 0x4edea
callq 0x49eef
movl %eax, %ebp
testl %ebp, %ebp
je 0x47096
xorl %r14d, %r14d
leaq 0x7d90(%rip), %rdi      # 0x4ee0d
movl %ebp, %esi
xorl %eax, %eax
callq 0x3140
movb $0x1, %r12b
pushq $0x1
popq %rdi
callq 0x33c0
jmp 0x46fec
movq (%rbx), %rax
testq %rax, %rax
je 0x47073
movq (%rax), %rsi
testq %rsi, %rsi
je 0x47073
cmpq $0x0, 0x8(%rax)
je 0x47073
leaq 0x7d3d(%rip), %rdi      # 0x4edf1
xorl %eax, %eax
callq 0x3140
movq (%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x7d36(%rip), %rdi      # 0x4edff
xorl %eax, %eax
callq 0x3140
leaq 0x29261(%rip), %r14     # 0x70338
movq %rsp, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x472f7
movq (%rsp), %rsi
cmpl $0x1, %eax
ja 0x470fd
testq %rsi, %rsi
je 0x470da
movq %rbx, %rdi
callq 0x46f2b
jmp 0x470da
movq %rsi, %rdi
callq 0x47263
movb $0x1, %r14b
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x47089
movq %rbx, %rdi
callq 0x473ec
leaq 0x29217(%rip), %rdi     # 0x70338
callq 0x4721f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq