This commit is contained in:
Philipp Moritz
2016-08-16 15:00:01 -07:00
parent 504044c033
commit 7a18347b5c
4 changed files with 53 additions and 32 deletions
+31 -24
View File
@@ -48,6 +48,8 @@ int add_client(plasma_store_state* s, int fd) {
return curr_id++;
}
// remove the client at index i by swapping it with the
// client at index num_clients-1 and zeroing the latter out
void remove_client(plasma_store_state* s, int i) {
memcpy(&s->waiting[i], &s->waiting[s->num_clients-1], sizeof(struct pollfd));
memset(&s->waiting[s->num_clients-1], 0, sizeof(struct pollfd));
@@ -107,6 +109,7 @@ int create_buffer(int64_t size) {
// create a new object buffer in the hash table
void create_object(int conn, plasma_request* req) {
LOG_INFO("creating object"); // TODO(pcm): add object_id here
int fd = create_buffer(req->size);
if (fd < 0) {
LOG_ERR("could not create shared memory buffer");
@@ -145,6 +148,7 @@ void get_object(int conn, plasma_request* req) {
// seal an object that has been created in the hash table
void seal_object(int conn, plasma_request* req) {
LOG_INFO("sealing object"); // TODO(pcm): add object_id here
object_table_entry *entry;
HASH_FIND(handle, open_objects, &req->object_id, sizeof(plasma_id), entry);
if (!entry) {
@@ -179,6 +183,9 @@ void process_event(int conn, plasma_request* req) {
case PLASMA_SEAL:
seal_object(conn, req);
break;
default:
LOG_ERR("invalid request %d", req->type);
exit(-1);
}
}
@@ -195,32 +202,32 @@ void event_loop(int socket) {
}
for (int i = 0; i < state.num_clients; ++i) {
if (state.waiting[i].revents == 0)
continue;
continue;
if (state.waiting[i].fd == socket) {
while (1) {
// handle new incoming connections
int new_socket = accept(socket, NULL, NULL);
if (new_socket < 0) {
if (errno != EWOULDBLOCK) {
LOG_ERR("accept failed");
exit(-1);
}
break;
}
int client_id = add_client(&state, new_socket);
LOG_INFO("adding new client with id %d", client_id);
}
while (1) {
// handle new incoming connections
int new_socket = accept(socket, NULL, NULL);
if (new_socket < 0) {
if (errno != EWOULDBLOCK) {
LOG_ERR("accept failed");
exit(-1);
}
break;
}
int client_id = add_client(&state, new_socket);
LOG_INFO("adding new client with id %d", client_id);
}
} else {
int r = read(state.waiting[i].fd, &req, sizeof(plasma_request));
if (r == -1) {
LOG_ERR("read error");
continue;
} else if (r == 0) {
LOG_INFO("client with id %d disconnected", state.client_id[i]);
remove_client(&state, i);
} else {
process_event(state.waiting[i].fd, &req);
}
int r = read(state.waiting[i].fd, &req, sizeof(plasma_request));
if (r == -1) {
LOG_ERR("read error");
continue;
} else if (r == 0) {
LOG_INFO("client with id %d disconnected", state.client_id[i]);
remove_client(&state, i);
} else {
process_event(state.waiting[i].fd, &req);
}
}
}
}