summaryrefslogtreecommitdiffstats
path: root/hw2/trans-loop.c
blob: f7dd96b54fb65e23152d3cc4a7427a57ed476065 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "xwrap.h"
#include "l4logger.h"
#include "ump-sched.h"
#include "trans-loop.h"

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int ump_trans_sender (LbsLogger* lbs_log,
    char** dest, char** files, char** agents) {

    int i;
    int files_count;
    int agents_count;

    lbs_logger_string (lbs_log, "Dump sender arguments:");
    lbs_logger_format (lbs_log, "dest_addr = %s", dest[0]);
    lbs_logger_format (lbs_log, "dest_port = %s", dest[1]);

    for (i = 0; strcmp (files[i], "--"); i++) {
        lbs_logger_format (lbs_log, "files[%d] = %s", i, files[i]);
    }
    files_count = i;
    lbs_logger_format (lbs_log, "files_count = %d", files_count);

    for (i = 0; agents[i] != NULL; i += 2) {
        lbs_logger_format (lbs_log, "agents[%d].addr = %s", i, agents[i]);
        lbs_logger_format (lbs_log, "agents[%d].port = %s", i, agents[i + 1]);
    }
    agents_count = i / 2;
    lbs_logger_format (lbs_log, "agents_count = %d", agents_count);

    UmpSched* sched = ump_sched_new (lbs_log, UMP_SCHED_TYPE_SENDER,
        agents, agents_count);
    if (sched == NULL) {
        lbs_logger_string (lbs_log, "Cannot construct schedular (terminated)");
        return 2;
    }

    for (i = 0; i < files_count; i++) {
        lbs_logger_format (lbs_log, "==> File %d: %s", i, files[i]);
        uint16_t filename_len = strlen (files[i]);
        int fd = open (files[i], O_RDONLY);
        if (fd < 0) {
            lbs_logger_format (lbs_log, "open: %s", strerror (errno));
            continue;
        }

        if (ump_sched_connect (sched, dest) < 0) {
            lbs_logger_string (lbs_log, "Connot connect to remote host");
            continue;
        }

        lbs_logger_string (lbs_log, "--> Connected to remote host!");
        lbs_logger_format (lbs_log, "--> Send %u bytes (filename length)", sizeof (uint16_t));
        ump_sched_send (sched, &filename_len, sizeof (uint16_t));
        lbs_logger_format (lbs_log, "--> Send %u bytes (filename)", filename_len);
        ump_sched_send (sched, files[i], filename_len);

        char buf[512];
        size_t read_size;
        while ((read_size = read (fd, buf, 512)) > 0) {
            lbs_logger_format (lbs_log, "--> Send %u bytes (data)", read_size);
            ump_sched_send (sched, buf, read_size);
        }

        lbs_logger_string (lbs_log, "--> Clean up!!");
        ump_sched_send (sched, NULL, 0);
        close (fd);
        lbs_logger_string (lbs_log, "--> Shutting down connection ...");
        ump_sched_shutdown (sched);
    }

    ump_sched_free (sched);
    return 0;
}

int ump_trans_receiver (LbsLogger* lbs_log, char** bind_ptr) {

    lbs_logger_string (lbs_log, "Dump receiver arguments:");
    lbs_logger_format (lbs_log, "bind_addr = %s", bind_ptr[0]);
    lbs_logger_format (lbs_log, "bind_port = %s", bind_ptr[1]);

    UmpSched* sched = ump_sched_new (lbs_log, UMP_SCHED_TYPE_RECEIVER, NULL, 0);
    if (sched == NULL) {
        lbs_logger_string (lbs_log, "Cannot construct schedular (terminated)");
        return 2;
    }

    if (ump_sched_listen (sched, bind_ptr) < 0) {
        lbs_logger_format (lbs_log, "Cannot listen on host (terminated)");
        ump_sched_free (sched);
        return 3;
    }

    while (true) {
        if (ump_sched_accept (sched) < 0) {
            lbs_logger_string (lbs_log, "==> Bad connection");
            continue;
        }

        lbs_logger_string (lbs_log, "==> New connection accepted");

        char buf[sizeof (UmpPkt) * UMP_SCHED_PKT_COUNT];
        char* buf_moved = buf;
        ssize_t buf_size;
        int state = 1;

        uint16_t filename_length;
        char* filename = NULL;
        char* filename_pos;
        int fd;

        while ((buf_size = ump_sched_receive (sched, buf)) >= 0) {
            lbs_logger_format (lbs_log, "==> Get %d bytes!", buf_size);

re_eval:
            switch (state) {
                case 1: // filename length
                    if (buf_size >= 2) {
                        memcpy (&filename_length, buf, 2);
                        buf_moved += 2;
                        buf_size -= 2;
                        filename = xmalloc (filename_length + 1);
                        filename_pos = filename;
                        state++;
                        lbs_logger_format (lbs_log,
                            "!! Filename length = %" PRIu16, filename_length);
                        goto re_eval;
                    } else {
                        memcpy (&filename_length, buf_moved, 1);
                        buf_moved++;
                        if (buf_moved - buf >= 2) {
                            buf_size -= 2;
                            filename = xmalloc (filename_length + 1);
                            filename_pos = filename;
                            state++;
                            lbs_logger_format (lbs_log,
                                "!! Filename length = %" PRIu16, filename_length);
                            goto re_eval;
                        }
                    }
                    break;
                case 2: // filename
                    if (filename_pos + buf_size > filename + filename_length) {
                        int buf_cp = filename + filename_length - filename_pos;
                        memcpy (filename_pos, buf_moved, buf_cp);
                        buf_moved += buf_cp;
                        buf_size -= buf_cp;
                        filename[filename_length] = '\0';
                        state++;
                        lbs_logger_format (lbs_log,
                            "!! File name = %s", filename);
                        for (int i = 0; i < filename_length; i++) {
                            if (filename[i] == '/') {
                                filename[i] = '-';
                            }
                        }
                        lbs_logger_format (lbs_log,
                            "!! File name = %s (converted)", filename);
                        fd = open (filename, O_CREAT | O_EXCL | O_WRONLY, 0666);
                        if (fd < 0) {
                            lbs_logger_format (lbs_log,
                                "Cannot create file: %s", strerror (errno));
                        }
                        goto re_eval;
                    } else {
                        memcpy (filename_pos, buf_moved, buf_size);
                        filename_pos += buf_size;
                        buf_moved = buf;
                    }
                    break;
                case 3: // data
                    if (fd >= 0) {
                        lbs_logger_format (lbs_log,
                            "!! Write %d bytes to file", buf_size);
                        write (fd, buf_moved, buf_size);
                        buf_moved = buf;
                    }
                    break;
            }
        }

        if (filename != NULL) {
            free (filename);
        }

        if (fd >= 0) {
            close (fd);
        }

        lbs_logger_string (lbs_log, "==> Connection closed");
    }

    ump_sched_free (sched);
    return 0;
}