Skip to content

Commit

Permalink
Impelemented the connect thread that emits a peer thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Dec 21, 2008
1 parent f3ef8a6 commit fb52c36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/base/bang-com.c
Expand Up @@ -13,6 +13,13 @@
#include<sys/socket.h>
#include<unistd.h>

/**
* \page Master-Slave Model
*
* A master thread should service requests from its slave-peer threads, and then request
* things of them. (These requests would be made by the modules).
*/

///Represents one of our peers.
typedef struct {
pthread_t *thread;
Expand Down
42 changes: 41 additions & 1 deletion src/base/bang-net.c
Expand Up @@ -35,7 +35,7 @@ void* BANG_server_thread(void *port) {

//check to see if we got available addresses
if (getaddrinfo(NULL,(char*)port,&hints,&result) != 0) {
BANG_send_signal(BANG_GADDRINFO_FAIL,NULL);
BANG_send_signal(BANG_GADDRINFO_FAIL,port);
return NULL;
}

Expand Down Expand Up @@ -76,3 +76,43 @@ void* BANG_server_thread(void *port) {
close(sock);
return NULL;
}


void* BANG_connect_thread(void *addr) {
int *sock = (int*) calloc(1,sizeof(int));
struct addrinfo hints;
struct addrinfo *result, *rp;

//sets the hints of getaddrinfo so it know what kind of address we want
//basic template of code from "man 2 getaddrinfo" section
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; //don't about ipv4 or ipv6
hints.ai_socktype = SOCK_STREAM;//tcp
hints.ai_flags = AI_PASSIVE; //for wildcard IP address
hints.ai_protocol = 0; //any protocol
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;

//check to see if we got available addresses
if (getaddrinfo(NULL,(char*)addr,&hints,&result) != 0) {
BANG_send_signal(BANG_GADDRINFO_FAIL,addr);
return NULL;
}


for (rp = result; rp != NULL; rp = rp->ai_next) {
*sock = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol);

if (*sock == -1) {
///TODO:Make this signal send out something more useful
BANG_send_signal(BANG_CONNECT_FAIL,addr);
} else if (connect(*sock,rp->ai_addr,rp->ai_addrlen) == 0) {
BANG_send_signal(BANG_PEER_CONNECTED,sock);
break;
}
}

freeaddrinfo(result);
return NULL;
}
1 change: 1 addition & 0 deletions src/base/bang-types.h
Expand Up @@ -20,6 +20,7 @@ enum BANG_signals {
BANG_BIND_SUC = 0,
BANG_BIND_FAIL,
BANG_GADDRINFO_FAIL,
BANG_CONNECT_FAIL,
BANG_LISTEN_FAIL,
BANG_PEER_CONNECTED
} BANG_signal;
Expand Down

0 comments on commit fb52c36

Please sign in to comment.