Skip to content

Commit

Permalink
Add support for initializing and shutting down networking.
Browse files Browse the repository at this point in the history
This updates compinfo to use the new functions.
  • Loading branch information
alesliehughes committed Jun 17, 2010
1 parent e4a8bbe commit b6c0c0e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
37 changes: 27 additions & 10 deletions compinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ int main (int argc,char *argv[]) {
struct computer *computer;
int ncomputers;
enum operation op = OP_NONE;
int nRet = 0;

if(network_initialize() != 0) {
fprintf (stderr,"Could not initialize the network: %s\n", drerrno_str());
return 1;
}

while ((opt = getopt (argc,argv,"lndc:vh")) != -1) {
switch (opt) {
Expand All @@ -57,7 +63,7 @@ int main (int argc,char *argv[]) {
break;
case 'v':
show_version (argv);
exit (0);
goto cleanup;
case 'l':
op = OP_LIST;
break;
Expand All @@ -67,20 +73,23 @@ int main (int argc,char *argv[]) {
case '?':
case 'h':
usage();
exit (1);
nRet = 1;
goto cleanup;
}
}

if ((op == OP_NONE)) {
usage ();
exit (1);
nRet = 1;
goto cleanup;
}

set_default_env();

if (!common_environment_check()) {
fprintf (stderr,"Error checking the environment: %s\n",drerrno_str());
exit (1);
nRet = 1;
goto cleanup;
}

switch (op) {
Expand All @@ -90,14 +99,16 @@ int main (int argc,char *argv[]) {
case OP_LIST:
if ((ncomputers = request_computer_list (&computer,CLIENT)) == -1) {
fprintf (stderr,"ERROR: While trying to request the computer list: %s\n",drerrno_str());
exit (1);
nRet = 1;
goto cleanup;
}
print_computers (computer,ncomputers);
break;
case OP_NUMBER:
if ((ncomputers = request_computer_list (&computer,CLIENT)) == -1) {
fprintf (stderr,"ERROR: While trying to request the computer list: %s\n",drerrno_str());
exit (1);
nRet = 1;
goto cleanup;
}
printf ("%i\n",ncomputers);
break;
Expand All @@ -107,20 +118,26 @@ int main (int argc,char *argv[]) {
computer_init(computer);
if (!computer) {
fprintf (stderr,"ERROR: Not enough memory\n");
exit (1);
nRet = 1;
goto cleanup;
}
if (!request_comp_xfer(icomp,computer,CLIENT)) {
fprintf (stderr,"ERROR: While trying to request the computer transfer: %s\n",drerrno_str());
exit (1);
nRet = 1;
goto cleanup;
}
print_computer_details (computer);
} else {
fprintf (stderr,"You need to specify the computer id using -c <computer_id>\n");
exit(1);
nRet = 1;
goto cleanup;
}
}

exit (0);
cleanup:
network_shutdown();

return nRet;
}

void print_computers (struct computer *computer, int ncomputers) {
Expand Down
25 changes: 25 additions & 0 deletions libdrqueue/communications.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,3 +1268,28 @@ dr_socket_write (int fd, char *buf, uint32_t len) {

return nSent;
}

int network_initialize()
{
int iResult = 0;
#ifdef _WIN32
WSADATA wsaData;

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
drerrno = DRE_NETWORKINIT;
log_auto(L_ERROR, "WSAStartup failed: %d\n", iResult);
return 1;
}
#endif
return iResult;
}

int network_shutdown()
{
#ifdef _WIN32
return WSACleanup();
#endif
return 0;
}
3 changes: 3 additions & 0 deletions libdrqueue/communications.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ int recv_string (int sfd, char **str);
int send_blocked_host_list (int sfd, struct blocked_host *bh, uint32_t size, int do_checksize);
int recv_blocked_host_list (int sfd, struct blocked_host **bh, uint32_t *size, int do_checksize);

int network_initialize();
int network_shutdown();

#endif /* _COMMUNICATIONS_H_ */

0 comments on commit b6c0c0e

Please sign in to comment.