source: trunk/vizservers/nanoscale/clientlib.c @ 952

Last change on this file since 952 was 409, checked in by kennell, 17 years ago

Added nanoscale for visualization server load distribution.

File size: 2.1 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <sys/time.h>
8#include <netinet/in.h>
9#include <netdb.h>
10
11#include "clientlib.h"
12
13int connect_renderer(char *hostname, int connect_port, int memory)
14{
15  int val;
16  int status;
17  struct sockaddr_in connect_addr;
18  int connect_fd = -1;
19
20  struct hostent *hostent = gethostbyname(hostname);
21  if (hostent == NULL) {
22    fprintf(stderr, "Unable to resolve '%s'\n", hostname);
23    return -1;
24  }
25
26  if (hostent->h_addrtype != AF_INET) {
27    fprintf(stderr, "Don't know how to deal with address family for '%s'\n",
28            hostname);
29    return -1;
30  }
31
32  int addr = *(int *) hostent->h_addr_list[0];
33
34 restart:
35
36  // Bind this address to the socket.
37  connect_addr.sin_family = AF_INET;
38  connect_addr.sin_port = htons(connect_port);
39  connect_addr.sin_addr.s_addr = addr; //htonl(inet_network(addr));
40
41  int msg;
42  do {
43    // Create a socket for connecting...
44    connect_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
45    if (connect_fd < 0) {
46      perror("socket");
47      return -1;
48    }
49
50    // If program is killed, drop the socket address reservation immediately.
51    val = 1;
52    status = setsockopt(connect_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
53    if (status < 0) {
54      perror("setsockopt");
55      // Not fatal.  Keep on going.
56    }
57
58    // Connect.
59    status = connect(connect_fd, (struct sockaddr *)&connect_addr,
60                     sizeof(connect_addr));
61    if (status < 0) {
62      perror("connect");
63      goto restart;
64    }
65
66    msg = htonl(memory);
67    status = write(connect_fd, &msg, sizeof(msg));
68    if (status <= 0) {
69      perror("write");
70      return -1;
71    }
72
73    status = read(connect_fd, &msg, sizeof(msg));
74    if (status <= 0) {
75      perror("read");
76      return -1;
77    }
78
79    if (msg != 0) {
80      connect_addr.sin_addr.s_addr = msg;
81      fprintf(stderr,"Connection redirected to %s\n",
82              inet_ntoa(connect_addr.sin_addr));
83      status = close(connect_fd);
84      if (status < 0) {
85        perror("shutdown");
86      }
87    }
88  } while(msg != 0);
89
90  return connect_fd;
91}
92
Note: See TracBrowser for help on using the repository browser.