Commit a691aa42 authored by Dustin L. Howett's avatar Dustin L. Howett

Remove the global 'server' variable and keep a std::list of servers instead....

Remove the global 'server' variable and keep a std::list of servers instead. Add a 'conn' console command for connecting to another server.
parent 32d1a5b9
......@@ -23,10 +23,12 @@ void *app_handle;
#include "commands.h"
#include "server.h"
#include <list>
static char *progname;
static int control_fd = -1;
static struct server *server;
static std::list<struct server *> servers;
#define MAX(a,b) (a>b?a:b)
......@@ -50,26 +52,32 @@ void do_copyover(char *requester) {
free(fn);
if(requester) // Yes, we're actually queueing this for when the copyover comes back and the socket is ready. Clever?!
ircprintf(server, "PRIVMSG %s :--- COPYOVER COMPLETE ---", requester);
//if(requester) // Yes, we're actually queueing this for when the copyover comes back and the socket is ready. Clever?!
//ircprintf(server, "PRIVMSG %s :--- COPYOVER COMPLETE ---", requester);
uint8_t *tag_data = NULL;
uint32_t tags_size = 0;
uint8_t *server_data = NULL;
uint8_t ntags = servers.size() + 1;
struct tag_info *copyovertags = (struct tag_info *)calloc(ntags, sizeof(struct tag_info));
int i = 0;
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
struct server *server = *iter;
uint32_t server_size = 0;
server_data = server_freeze(server, &server_size);
struct tag_info copyovertags[] = {
{ 'SERV', server_size, server_data, },
{ 'CTLD', sizeof(control_fd), &control_fd, },
};
uint8_t *server_data = server_freeze(server, &server_size);
copyovertags[i].type = 'SERV';
copyovertags[i].len = server_size;
copyovertags[i].bufp = server_data;
++i;
}
copyovertags[i].type = 'CTLD';
copyovertags[i].len = sizeof(control_fd);
copyovertags[i].bufp = &control_fd;
++i;
tag_data = taglist_to_dady(copyovertags, sizeof(copyovertags)/sizeof(struct tag_info), &tags_size);
tag_data = taglist_to_dady(copyovertags, ntags, &tags_size);
write(fd, tag_data, tags_size);
lseek(fd, 0, SEEK_SET);
free(server_data);
free(tag_data);
char *len_str;
......@@ -81,10 +89,11 @@ void do_copyover(char *requester) {
static int _load_copyover_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, void *cbdata) {
switch(tagtype) {
case 'SERV':
server = server_thaw(data, len);
case 'SERV': {
struct server *server = server_thaw(data, len);
servers.push_back(server);
break;
case 'CTLD':
} case 'CTLD':
memcpy(&control_fd, data, len);
_D("Got control descriptor %d.\n", control_fd);
break;
......@@ -105,7 +114,7 @@ void init_copyover(int argc, char **argv) {
free(data);
}
void init_irc(int argc, char **argv) {
void _new_server(char *hostname) {
struct sockaddr_in server_addr;
int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
if(socket_fd < 0) {
......@@ -115,14 +124,14 @@ void init_irc(int argc, char **argv) {
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(6667);
if((server_addr.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE) {
if((server_addr.sin_addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
struct addrinfo *res = NULL;
struct addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
getaddrinfo(argv[1], NULL, &hint, &res);
getaddrinfo(hostname, NULL, &hint, &res);
server_addr.sin_addr.s_addr = ((struct sockaddr_in *)(res[0].ai_addr))->sin_addr.s_addr;
freeaddrinfo(res);
}
......@@ -132,8 +141,8 @@ void init_irc(int argc, char **argv) {
exit(1);
}
server = server_alloc();
server->hostname = strdup(argv[1]);
struct server *server = server_alloc();
server->hostname = strdup(hostname);
server->fd = socket_fd;
control_fd = 0;
......@@ -141,6 +150,11 @@ void init_irc(int argc, char **argv) {
ircprintf(server, "USER 1 2 3 4"); // TODO: YEAH
ircprintf(server, "NICK oligosII");
servers.push_back(server);
}
void init_irc(int argc, char **argv) {
_new_server(argv[1]);
}
void ircprintf(struct server *server, const char *format, ...) {
......@@ -227,29 +241,41 @@ void _write_irc(struct server *server) {
void event_loop() {
fd_set read_fds, write_fds;
int nfds = 0;
if(server->fd > -1)
nfds = MAX(nfds, server->fd);
if(control_fd > -1)
nfds = MAX(nfds, control_fd);
nfds++;
while(true) {
int nfds = 0;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
if(server->fd > -1) {
FD_SET(server->fd, &read_fds);
FD_SET(server->fd, &write_fds);
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
int fd = (*iter)->fd;
if(fd > -1) {
FD_SET(fd, &read_fds);
FD_SET(fd, &write_fds);
}
nfds = MAX(nfds, fd);
}
if(control_fd > -1)
if(control_fd > -1) {
FD_SET(control_fd, &read_fds);
nfds = MAX(nfds, control_fd);
}
nfds++;
int r = select(nfds, &read_fds, &write_fds, NULL, NULL);
if(r == -1) {
_D("Error selecting?\n");
exit(1);
}
for(std::list<struct server *>::const_iterator iter = servers.begin(), end = servers.end(); iter != end; ++iter) {
struct server *server = *iter;
if(FD_ISSET(server->fd, &read_fds)) {
_read_irc(server);
}
if(server->nbytes_write > 0 && FD_ISSET(server->fd, &write_fds)) {
_D("Socket ready for writing and we have %llu bytes of data.\n", server->nbytes_write);
_write_irc(server);
}
}
if(FD_ISSET(control_fd, &read_fds)) {
char buf[1024];
int r = read(control_fd, buf, 1024);
......@@ -259,15 +285,13 @@ void event_loop() {
do_copyover(NULL);
} else if(strncmp(buf, "quit", 4) == 0) {
exit(0);
} else if(strncmp(buf, "send ", 5) == 0) {
ircprintf(server, "%s", buf+5);
} else if(strncmp(buf, "conn ", 5) == 0) {
_new_server(buf+5);
//} else if(strncmp(buf, "send ", 5) == 0) {
//ircprintf(server, "%s", buf+5);
}
}
if(server->nbytes_write > 0 && FD_ISSET(server->fd, &write_fds)) {
_D("Socket ready for writing and we have %llu bytes of data.\n", server->nbytes_write);
_write_irc(server);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment