initial version of hashmap works
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
#ifndef __HASHMAP_C__
|
#ifndef __HASHMAP_C__
|
||||||
#define __HASHMAP_C__
|
#define __HASHMAP_C__
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
#define HASHMAP_MAX_ITEMS 10000
|
#define HASHMAP_MAX_CLIENTS 10000
|
||||||
|
|
||||||
struct hashmap {
|
struct hashmap {
|
||||||
int sockfd;
|
int sockfd;
|
||||||
@@ -11,6 +12,8 @@ struct hashmap {
|
|||||||
};
|
};
|
||||||
|
|
||||||
int hash(int sockfd);
|
int hash(int sockfd);
|
||||||
|
void hm_init(struct hashmap *map);
|
||||||
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas);
|
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas);
|
||||||
|
struct hashmap *hm_lookup(struct hashmap *map, int sockfd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -26,6 +26,5 @@ void epoll_ctl_add(int epfd, int sockfd, uint32_t events);
|
|||||||
void epoll_ctl_del(int epfd, int sockfd);
|
void epoll_ctl_del(int epfd, int sockfd);
|
||||||
void setnonblocking(int sockfd);
|
void setnonblocking(int sockfd);
|
||||||
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
|
||||||
void print_client_ip(struct sockaddr_in *sin);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,5 +12,6 @@
|
|||||||
|
|
||||||
// print every IPs of a hostname
|
// print every IPs of a hostname
|
||||||
void print_ips(const char *hostname, const char *port);
|
void print_ips(const char *hostname, const char *port);
|
||||||
|
void get_client_ip(struct sockaddr_storage *sa, char *ip);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ $$ hash(\text{key}) = \text{key} \mod \text{table\_dim} $$
|
|||||||
|
|
||||||
- String keys:
|
- String keys:
|
||||||
$$ hash(key) = \sum_{i=0}^{len(key) - 1} ascii\_value(key[i]) * prime\_number$$
|
$$ hash(key) = \sum_{i=0}^{len(key) - 1} ascii\_value(key[i]) * prime\_number$$
|
||||||
|
|
||||||
|
**TODO**
|
||||||
@@ -1,9 +1,24 @@
|
|||||||
#include "hashmap.h"
|
#include "hashmap.h"
|
||||||
|
|
||||||
int hash(int sockfd) { return sockfd % HASHMAP_MAX_ITEMS; }
|
int hash(int sockfd) { return sockfd % HASHMAP_MAX_CLIENTS; }
|
||||||
|
|
||||||
|
void hm_init(struct hashmap *map) {
|
||||||
|
memset(map, 0, sizeof(struct hashmap) * HASHMAP_MAX_CLIENTS);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
|
||||||
|
map[i].sockfd = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas) {
|
void hm_insert(struct hashmap *map, int sockfd, struct sockaddr_storage *sas) {
|
||||||
int index = hash(sockfd);
|
int index = hash(sockfd);
|
||||||
map[index].sockfd = sockfd;
|
map[index].sockfd = sockfd;
|
||||||
map[index].sas = *sas;
|
map[index].sas = *sas;
|
||||||
|
|
||||||
|
// I should check the collisions
|
||||||
|
}
|
||||||
|
|
||||||
|
struct hashmap *hm_lookup(struct hashmap *map, int sockfd) {
|
||||||
|
int index = hash(sockfd);
|
||||||
|
return &map[index];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
fprintf(stdout, BOLD GREEN "[client] Running cws...\n" RESET);
|
fprintf(stdout, BOLD GREEN "[client] Running client...\n" RESET);
|
||||||
|
|
||||||
int ret = test_client_connection(argv[1], argv[2]);
|
int ret = test_client_connection(argv[1], argv[2]);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
server = files('main.c', 'server.c', 'utils.c')
|
server = files('main.c', 'server.c', 'utils.c', 'hashmap.c')
|
||||||
client = files('mainc.c', 'client.c')
|
client = files('mainc.c', 'client.c')
|
||||||
43
src/server.c
43
src/server.c
@@ -1,6 +1,8 @@
|
|||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
#include "hashmap.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
int start_server(const char *hostname, const char *service) {
|
int start_server(const char *hostname, const char *service) {
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
@@ -50,6 +52,9 @@ void handle_clients(int sockfd) {
|
|||||||
struct sockaddr_storage their_sa;
|
struct sockaddr_storage their_sa;
|
||||||
socklen_t theirsa_size = sizeof their_sa;
|
socklen_t theirsa_size = sizeof their_sa;
|
||||||
|
|
||||||
|
struct hashmap clients[HASHMAP_MAX_CLIENTS];
|
||||||
|
hm_init(clients);
|
||||||
|
|
||||||
int epfd = epoll_create1(0);
|
int epfd = epoll_create1(0);
|
||||||
setnonblocking(sockfd);
|
setnonblocking(sockfd);
|
||||||
epoll_ctl_add(epfd, sockfd, EPOLLIN | EPOLLET);
|
epoll_ctl_add(epfd, sockfd, EPOLLIN | EPOLLET);
|
||||||
@@ -70,28 +75,31 @@ void handle_clients(int sockfd) {
|
|||||||
if (revents[i].data.fd == sockfd) {
|
if (revents[i].data.fd == sockfd) {
|
||||||
// new client
|
// new client
|
||||||
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
|
client_fd = handle_new_client(sockfd, &their_sa, &theirsa_size);
|
||||||
|
|
||||||
setnonblocking(client_fd);
|
setnonblocking(client_fd);
|
||||||
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
epoll_ctl_add(epfd, client_fd, EPOLLIN);
|
||||||
|
hm_insert(clients, client_fd, &their_sa);
|
||||||
print_client_ip((struct sockaddr_in *)&their_sa);
|
|
||||||
|
|
||||||
int bytes_sent = send(client_fd, msg, msg_len, 0);
|
int bytes_sent = send(client_fd, msg, msg_len, 0);
|
||||||
fprintf(stdout, BLUE "[server] Sent %d bytes\n" RESET, bytes_sent);
|
fprintf(stdout, "[server] Sent %d bytes\n", bytes_sent);
|
||||||
} else {
|
} else {
|
||||||
// incoming data
|
// incoming data
|
||||||
client_fd = revents[i].data.fd;
|
client_fd = revents[i].data.fd;
|
||||||
// fprintf(stdout, "[%d] EPOLLIN\n", client_fd);
|
|
||||||
int bytes_read = recv(client_fd, data, sizeof data, 0);
|
int bytes_read = recv(client_fd, data, sizeof data, 0);
|
||||||
|
|
||||||
if (bytes_read == 0) {
|
if (bytes_read == 0) {
|
||||||
// client disconnect
|
// client disconnected
|
||||||
fprintf(stdout, BLUE "[server] Client disconnection\n");
|
char ip[INET_ADDRSTRLEN];
|
||||||
|
struct hashmap *client = hm_lookup(clients, client_fd);
|
||||||
|
get_client_ip(&client->sas, ip);
|
||||||
|
|
||||||
|
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||||
epoll_ctl_del(epfd, client_fd);
|
epoll_ctl_del(epfd, client_fd);
|
||||||
close(client_fd);
|
close(client_fd);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
data[strlen(data) - 1] = '\0';
|
data[strcspn(data, "\n")] = '\0';
|
||||||
fprintf(stdout, "[server] Bytes read (%d): %s\n", bytes_read, data);
|
fprintf(stdout, "[server] Bytes read (%d): %s\n", bytes_read, data);
|
||||||
if (strcmp(data, "stop") == 0) {
|
if (strcmp(data, "stop") == 0) {
|
||||||
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET);
|
fprintf(stdout, GREEN BOLD "[server] Stopping...\n" RESET);
|
||||||
@@ -104,6 +112,10 @@ void handle_clients(int sockfd) {
|
|||||||
|
|
||||||
free(revents);
|
free(revents);
|
||||||
close(epfd);
|
close(epfd);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < HASHMAP_MAX_CLIENTS; ++i) {
|
||||||
|
close(clients[i].sockfd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
|
void epoll_ctl_add(int epfd, int sockfd, uint32_t events) {
|
||||||
@@ -138,25 +150,10 @@ int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *
|
|||||||
|
|
||||||
if (client_fd == -1) {
|
if (client_fd == -1) {
|
||||||
if (errno != EWOULDBLOCK) {
|
if (errno != EWOULDBLOCK) {
|
||||||
fprintf(stderr,
|
fprintf(stderr, RED BOLD "[server] accept(): %s\n" RESET, strerror(errno));
|
||||||
RED BOLD
|
|
||||||
"[server] "
|
|
||||||
"accept(): "
|
|
||||||
"%s\n" RESET,
|
|
||||||
strerror(errno));
|
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return client_fd;
|
return client_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_client_ip(struct sockaddr_in *sin) {
|
|
||||||
char ip[INET_ADDRSTRLEN];
|
|
||||||
inet_ntop(AF_INET, &sin->sin_addr, ip, INET_ADDRSTRLEN);
|
|
||||||
fprintf(stdout,
|
|
||||||
BLUE
|
|
||||||
"[server] Incoming "
|
|
||||||
"client, IP: %s\n" RESET,
|
|
||||||
ip);
|
|
||||||
}
|
|
||||||
@@ -34,3 +34,9 @@ void print_ips(const char *hostname, const char *port) {
|
|||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void get_client_ip(struct sockaddr_storage *sa, char *ip) {
|
||||||
|
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &sin->sin_addr, ip, INET_ADDRSTRLEN);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user