client.c
author lbatlle@npdl268.bpo.hp.com
Fri, 23 Mar 2007 14:34:16 +0100
changeset 9 9acd8ae3190c
parent 8 03339adb7014
child 11 bb94b9890001
permissions -rw-r--r--
First usable version!
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
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
     6
static char *client_command = 0;
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
     7
static void c_end_of_job();
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
     8
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     9
void c_new_job(const char *command)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    10
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    11
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    12
    int res;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    13
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    14
    m.type = NEWJOB;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    15
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    16
    /* global */
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    17
    client_command = command;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    18
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    19
    strcpy(m.u.command, command);
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    20
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    21
    res = send(server_socket, &m, sizeof(m), 0);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    22
    if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    23
    {
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    24
        perror("c: send");
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    25
        exit(-1);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    26
    }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    27
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    28
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    29
void c_wait_server_commands()
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    30
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    31
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    32
    int res;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    33
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    34
    while (1)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    35
    {
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    36
        res = recv(server_socket, &m, sizeof(m), 0);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    37
        if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    38
        {
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    39
            perror("read");
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    40
            exit(-1);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    41
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    42
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    43
        if (res == 0)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    44
            break;
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    45
        if (res != sizeof(m))
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    46
        {
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    47
            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
    48
                res, (int) sizeof(m));
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    49
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    50
        assert(res == sizeof(m));
4
viric@llimona
parents: 3
diff changeset
    51
        msgdump(&m);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    52
        if (m.type == NEWJOB_OK)
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    53
            ;
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    54
        if (m.type == RUNJOB)
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    55
        {
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    56
            run_job(client_command);
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    57
            c_end_of_job();
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    58
            break;
5
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
    }
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    61
    close(server_socket);
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    62
}
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    63
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    64
void c_wait_server_lines()
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    65
{
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    66
    struct msg m;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    67
    int res;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    68
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    69
    while (1)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    70
    {
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    71
        res = recv(server_socket, &m, sizeof(m),0);
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    72
        if(res == -1)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    73
            perror("read");
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    74
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    75
        if (res == 0)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    76
            break;
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    77
        assert(res == sizeof(m));
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    78
        msgdump(&m);
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    79
        if (m.type == LIST_LINE)
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    80
        {
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    81
            printf("%s", m.u.line);
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 4
diff changeset
    82
        }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    83
    }
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    84
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    85
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    86
void c_list_jobs()
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    87
{
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    88
    struct msg m;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    89
    int res;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    90
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    91
    m.type = LIST;
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    92
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    93
    res = send(server_socket, &m, sizeof(m), 0);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    94
    if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    95
        perror("send");
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    96
}
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    97
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
    98
static void c_end_of_job()
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
    99
{
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   100
    struct msg m;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   101
    int res;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   102
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   103
    m.type = ENDJOB;
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   104
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   105
    res = send(server_socket, &m, sizeof(m),0);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   106
    if(res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   107
        perror("send");
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   108
}
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   109
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   110
int c_shutdown_server()
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   111
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   112
    struct msg m;
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   113
    int res;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   114
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   115
    m.type = KILL;
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   116
    res = send(server_socket, &m, sizeof(m), 0);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   117
    assert(res != -1);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   118
}