summaryrefslogtreecommitdiffstats
path: root/test/client_test/client_test_common.c
blob: 5b716302ebe851536b1cabaa596da365c7397661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <cluster_api.h>

#include "client_test_common.h"

#define CLUSTER_TEST_WAIT_TIME (10000)

static int g_dummyServerFd = -1;
static char g_dummyServerExeFilePath[256];

void setExeFilePath(const char *exeFilePath)
{
    char *pCh;

    // dummy_server in the same path as client_test.
    strcpy(g_dummyServerExeFilePath, exeFilePath);
    pCh = strrchr(g_dummyServerExeFilePath, '/');
    pCh = pCh != NULL ? pCh + 1: g_dummyServerExeFilePath;
    strcpy(pCh, "dummy_server");
}

void startDummyServer(pid_t child)
{
    if (child == 0) {
        int rc;
        char *argv[2] = {NULL, NULL};

        argv[0] = g_dummyServerExeFilePath;

        rc = execv(argv[0], argv);
        if (rc < 0) {
            ERRNO_LOG(execv);
        }
    }
    else {
        usleep(CLUSTER_TEST_WAIT_TIME);
    }
}

void stopDummyServer(void)
{
    if (g_dummyServerFd >= 0) {
        shutdown(g_dummyServerFd, SHUT_RDWR);
        close(g_dummyServerFd);
    }
    unlink(CLUSTER_TEST_SENDDATA_FILE);
    sleep(1);
}

void connectToDummyServer(void)
{
    int rc;
    int fd;
    struct sockaddr_un unixAddr;
    int len;

    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        ERRNO_LOG(socket);
        return;
    }
    unixAddr.sun_family = AF_UNIX;
    strcpy(unixAddr.sun_path, CLUSTER_TEST_DOMAIN_PATH);
    len = sizeof(unixAddr.sun_family)+strlen(unixAddr.sun_path);
    rc = connect(fd, (struct sockaddr *)&unixAddr, len);
    if (rc != 0) {
        ERRNO_LOG(connect);
        shutdown(fd, SHUT_RDWR);
        close(fd);
        return;
    }

    g_dummyServerFd = fd;
}

void requestSendData(IPC_DATA_IC_SERVICE_S *pSendData)
{
    FILE *fp = NULL;
    int dummy = 1;
    int size;

    fp = fopen(CLUSTER_TEST_SENDDATA_FILE, "wb");
    if (fp != NULL) {
        size = fwrite(pSendData, 1, sizeof(IPC_DATA_IC_SERVICE_S), fp);
        if (size < sizeof(IPC_DATA_IC_SERVICE_S)) {
            ERRNO_LOG(fwrite);
        }
        fclose(fp);

        size = write(g_dummyServerFd, &dummy, sizeof(dummy));
        if (size < 0) {
            ERRNO_LOG(write);
        }
        usleep(CLUSTER_TEST_WAIT_TIME);
    }
}