client.c
author viric@llimona
Sun, 25 Mar 2007 04:55:18 +0200
changeset 23 96fcebb68510
parent 22 afdc8410633f
child 27 886bdb2f4632
permissions -rw-r--r--
More TODO for the next versions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     1
#include <assert.h>
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
     2
#include <stdio.h>
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     3
#include "msg.h"
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     4
#include "main.h"
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     5
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
     6
static void c_end_of_job(int errorlevel);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
     7
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     8
void c_new_job(const char *command)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     9
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    10
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    11
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    12
    m.type = NEWJOB;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    13
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    14
    /* global */
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    15
    m.u.newjob.command_size = strlen(command) + 1; /* add null */
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    16
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    17
    /* Send the message */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    18
    send_msg(server_socket, &m);
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    19
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    20
    /* Send the command */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 11
diff changeset
    21
    send_bytes(server_socket, command, m.u.newjob.command_size);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    22
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    23
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    24
void c_wait_server_commands(const char *my_command, int store_output)
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    25
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    26
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    27
    int res;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    28
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    29
    while (1)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    30
    {
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    31
        res = recv(server_socket, &m, sizeof(m), 0);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    32
        if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    33
        {
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    34
            perror("read");
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    35
            exit(-1);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    36
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    37
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    38
        if (res == 0)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    39
            break;
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    40
        if (res != sizeof(m))
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    41
        {
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    42
            fprintf(stderr, "c: recv() message size wrong: %i instead of %i\n",
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    43
                res, (int) sizeof(m));
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    44
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    45
        assert(res == sizeof(m));
4
viric@llimona
parents: 3
diff changeset
    46
        msgdump(&m);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    47
        if (m.type == NEWJOB_OK)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    48
            ;
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    49
        if (m.type == RUNJOB)
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    50
        {
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    51
            int errorlevel;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    52
            /* This will send RUNJOB_OK */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    53
            errorlevel = run_job(my_command, store_output);
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
    54
            c_end_of_job(errorlevel);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    55
            break;
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    56
        }
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    57
    }
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    58
}
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    59
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    60
void c_wait_server_lines()
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    61
{
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    62
    struct msg m;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    63
    int res;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    64
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    65
    while (1)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    66
    {
21
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    67
        res = recv_msg(server_socket, &m);
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    68
        if(res == -1)
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    69
        {
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    70
            perror("read");
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    71
            exit(-1);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
    72
        }
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    73
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    74
        if (res == 0)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    75
            break;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    76
        assert(res == sizeof(m));
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    77
        if (m.type == LIST_LINE)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    78
        {
21
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    79
            char * buffer;
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    80
            buffer = (char *) malloc(m.u.line_size);
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    81
            recv_bytes(server_socket, buffer, m.u.line_size);
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    82
            printf("%s", buffer);
a797f96a022f Lines for list doesn't have limit.
viric@llimona
parents: 20
diff changeset
    83
            free(buffer);
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    84
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    85
    }
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    86
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    87
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    88
void c_list_jobs()
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    89
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    90
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    91
    int res;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    92
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    93
    m.type = LIST;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    94
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    95
    res = send(server_socket, &m, sizeof(m), 0);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    96
    if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    97
        perror("send");
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    98
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    99
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   100
void c_send_runjob_ok(int store_output, const char *ofname)
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   101
{
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   102
    struct msg m;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   103
    int res;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   104
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   105
    /* Prepare the message */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   106
    m.type = RUNJOB_OK;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   107
    m.u.runjob_ok.store_output = store_output;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   108
    if (store_output)
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   109
        m.u.runjob_ok.ofilename_size = strlen(ofname) + 1;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   110
    else
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   111
        m.u.runjob_ok.ofilename_size = 0;
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   112
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   113
    send_msg(server_socket, &m);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   114
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   115
    /* Send the filename */
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   116
    if (store_output)
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   117
        send_bytes(server_socket, ofname, m.u.runjob_ok.ofilename_size);
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   118
}
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 21
diff changeset
   119
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   120
static void c_end_of_job(int errorlevel)
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   121
{
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   122
    struct msg m;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   123
    int res;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   124
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   125
    m.type = ENDJOB;
19
5efc347cca8d The finished jobs store the errorlevel, and can be listed.
viric@llimona
parents: 18
diff changeset
   126
    m.u.errorlevel = errorlevel;
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   127
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   128
    res = send(server_socket, &m, sizeof(m),0);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   129
    if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   130
        perror("send");
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   131
}
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   132
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   133
int c_shutdown_server()
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   134
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   135
    struct msg m;
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   136
    int res;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   137
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   138
    m.type = KILL;
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   139
    res = send(server_socket, &m, sizeof(m), 0);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   140
    assert(res != -1);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   141
}
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   142
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   143
int c_clear_finished()
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   144
{
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   145
    struct msg m;
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   146
    int res;
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   147
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   148
    m.type = CLEAR_FINISHED;
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   149
    res = send(server_socket, &m, sizeof(m), 0);
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   150
    assert(res != -1);
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 19
diff changeset
   151
}