// File: /workspace/github_c_makefile_star2_O1/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 %edi, %edi
callq 0x4c693
movq %rax, %rbx
callq 0x3e7d1
movq 0x8(%rbx), %r14
movq %rax, (%r14)
leaq 0x5636(%rip), %rdi      # 0x5186c
callq 0x3380
movq %rax, 0x8(%r14)
leaq 0x260e2(%rip), %rdi     # 0x72328
callq 0x4c494
movq $0x0, (%rsp)
cmpl $0x0, 0x26136(%rip)     # 0x72390
je 0x4c3a5
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movb $0x1, %r13b
leaq 0x7e46(%rip), %r14      # 0x540b6
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x3240
movq %r14, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x3140
movq %r14, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x3140
callq 0x4c15a
testb $0x1, %r13b
je 0x4c2c2
xorl %edi, %edi
callq 0x3240
movslq %r12d, %rcx
subq %rcx, %rax
cmpq $0x6fe, %rax            # imm = 0x6FE
jl 0x4c2c2
xorl %edi, %edi
callq 0x3240
movq %rax, %r12
jmp 0x4c2c8
testb $0x1, %bpl
je 0x4c2e5
movq (%rbx), %rcx
movq 0x8(%rbx), %rdi
leaq 0x7b0e(%rip), %rsi      # 0x53de4
leaq 0x7b0d(%rip), %rdx      # 0x53dea
callq 0x4f4cc
movl %eax, %r15d
testl %r15d, %r15d
je 0x4c31d
xorl %r13d, %r13d
leaq 0x7b19(%rip), %rdi      # 0x53e0d
movl %r15d, %esi
xorl %eax, %eax
callq 0x3140
movb $0x1, %bpl
movl $0x1, %edi
callq 0x33a0
cmpl $0x0, 0x2607e(%rip)     # 0x72390
jne 0x4c275
jmp 0x4c3a5
movq (%rbx), %rax
testq %rax, %rax
je 0x4c2ea
movq (%rax), %rsi
testq %rsi, %rsi
je 0x4c2ea
cmpq $0x0, 0x8(%rax)
je 0x4c2ea
leaq 0x7ab6(%rip), %rdi      # 0x53df1
xorl %eax, %eax
callq 0x3140
movq (%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x7aaf(%rip), %rdi      # 0x53dff
xorl %eax, %eax
callq 0x3140
leaq 0x25fca(%rip), %r15     # 0x72328
movq %r15, %rdi
movq %rsp, %rsi
callq 0x4c5d3
cmpl $0x1, %eax
ja 0x4c38f
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x4c37f
movq %rbx, %rdi
callq 0x4c1a0
movq %r15, %rdi
movq %rsp, %rsi
callq 0x4c5d3
cmpl $0x2, %eax
jb 0x4c36e
movq (%rsp), %rdi
callq 0x4c519
movb $0x1, %r13b
xorl %r15d, %r15d
xorl %ebp, %ebp
jmp 0x4c301
movq %rbx, %rdi
callq 0x4c6e4
leaq 0x25f74(%rip), %rdi     # 0x72328
callq 0x4c4d5
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq