some fixes in lab2
This commit is contained in:
parent
a0d6e0bdf5
commit
160886c6c4
@ -6,13 +6,13 @@ LIBS := -lpthread
|
||||
SSOURCES := server.c
|
||||
SOBJECTS = $(addprefix $(BIN), $(SSOURCES:.c=.o))
|
||||
|
||||
SERVERNAME := otpserver
|
||||
SERVERNAME := server
|
||||
SERVERNAME := $(addprefix $(BIN), $(SERVERNAME))
|
||||
|
||||
CSOURCES := client.c
|
||||
COBJECTS = $(addprefix $(BIN), $(CSOURCES:.c=.o))
|
||||
|
||||
CLIENTNAME := otpclient
|
||||
CLIENTNAME := client
|
||||
CLIENTNAME := $(addprefix $(BIN), $(CLIENTNAME))
|
||||
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <stdint.h>
|
||||
#include <poll.h>
|
||||
|
||||
#define SOCKET_PATH "/tmp/otp_socket"
|
||||
#define SOCKET_PATH "/tmp/socket"
|
||||
|
||||
#define CMD_ECHO 1
|
||||
#define CMD_TIMER 2
|
||||
@ -25,7 +25,7 @@ void send_fd(int socket, int fd) {
|
||||
char buf[CMSG_SPACE(sizeof(fd))];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
struct iovec io = { .iov_base = (void*)"F", .iov_len = 1 };
|
||||
struct iovec io = { .iov_base = "F", .iov_len = 1 };
|
||||
|
||||
msg.msg_iov = &io;
|
||||
msg.msg_iovlen = 1;
|
||||
@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
|
||||
req.command = atoi(argv[2]);
|
||||
req.value = atoi(argv[3]);
|
||||
|
||||
write(sock, &req, sizeof(req));
|
||||
write(sock, &req, sizeof(struct request));
|
||||
|
||||
int response;
|
||||
read(sock, &response, sizeof(response));
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#define SOCKET_PATH "/tmp/otp_socket"
|
||||
#define SOCKET_PATH "/tmp/socket"
|
||||
#define MAX_CLIENTS 10
|
||||
|
||||
#define CMD_ECHO 1
|
||||
@ -71,18 +71,20 @@ void* worker_thread(void *arg) {
|
||||
continue;
|
||||
}
|
||||
|
||||
struct request *req = &(node->req);
|
||||
|
||||
char buffer[128];
|
||||
sprintf(buffer, "Processing request with priority %d", node->req.priority);
|
||||
sprintf(buffer, "Processing (p: %d, c: %d, v: %d)", req->priority, req->command, req->value);
|
||||
log_msg(buffer);
|
||||
|
||||
int response = 0;
|
||||
|
||||
if (node->req.command == CMD_ECHO) {
|
||||
response = node->req.value;
|
||||
if (req->command == CMD_ECHO) {
|
||||
response = req->value;
|
||||
write(node->client_fd, &response, sizeof(response));
|
||||
}
|
||||
else if (node->req.command == CMD_TIMER) {
|
||||
sleep(node->req.value);
|
||||
else if (req->command == CMD_TIMER) {
|
||||
sleep(req->value);
|
||||
uint64_t val = 1;
|
||||
write(node->event_fd, &val, sizeof(val));
|
||||
response = 0;
|
||||
@ -99,7 +101,7 @@ int recv_fd(int socket) {
|
||||
char buf[CMSG_SPACE(sizeof(int))];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
struct iovec io = { .iov_base = (void*)"F", .iov_len = 1 };
|
||||
struct iovec io = { .iov_base = "F", .iov_len = 1 };
|
||||
|
||||
msg.msg_iov = &io;
|
||||
msg.msg_iovlen = 1;
|
||||
@ -138,8 +140,8 @@ int main() {
|
||||
|
||||
int event_fd = recv_fd(client_fd);
|
||||
|
||||
struct request req;
|
||||
read(client_fd, &req, sizeof(req));
|
||||
struct request req = {0, 0, 0};
|
||||
read(client_fd, &req, sizeof(struct request));
|
||||
|
||||
client_request_t *node = malloc(sizeof(client_request_t));
|
||||
node->client_fd = client_fd;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user