source: nanoscale/branches/1.0/client.c @ 5049

Last change on this file since 5049 was 1111, checked in by gah, 16 years ago

nanovis/heightmap update

File size: 2.3 KB
Line 
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <string.h>
5#include <signal.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <sys/wait.h>
9#include <sys/select.h>
10#include <sys/time.h>
11#include <netinet/in.h>
12#include <getopt.h>
13
14#include "clientlib.h"
15
16/*
17 * min()/max() macros that also do
18 * strict type-checking.. See the
19 * "unnecessary" pointer comparison.
20 */
21#define min(x,y) ({                             \
22            typeof(x) _x = (x);                 \
23            typeof(y) _y = (y);                 \
24            (void) (&_x == &_y);                \
25            _x < _y ? _x : _y; })
26
27#define max(x,y) ({                             \
28            typeof(x) _x = (x);                 \
29            typeof(y) _y = (y);                 \
30            (void) (&_x == &_y);                \
31            _x > _y ? _x : _y; })
32
33void help(const char *argv0)
34{
35    fprintf(stderr,
36            "Syntax: %s addr:port load\n",
37            argv0);
38    exit(1);
39}
40
41int connect_fd = -1;
42
43void
44activity(int signum)
45{
46    write(connect_fd, "hello\n\r", 7);
47    write(1,".",1);
48}
49
50
51int
52main(int argc, char *argv[])
53{
54    int status;
55    int connect_port = -1;
56
57    if (argc != 3) {
58        help(argv[0]);
59    }
60
61    char addr[100];
62    if (sscanf(argv[1], "%100[^:\n]:%d\n", addr, &connect_port) != 2) {
63        help(argv[0]);
64    }
65    int memory = strtoul(argv[2],0,0);
66
67    connect_fd = connect_renderer(addr, connect_port, memory);
68
69    if (connect_fd == -1) {
70        return 1;
71    }
72
73    printf("Connection accepted.\n");
74
75#ifdef DEBUGGING
76    if (signal(SIGALRM, activity) == SIG_ERR) {
77        perror("signal");
78    }
79
80    struct itimerval itvalue = {
81        {0,500000}, {0,500000}
82    };
83    status = setitimer(ITIMER_REAL, &itvalue, NULL);
84#endif
85
86    fd_set rfds;
87    FD_ZERO(&rfds);
88    while(1) {
89        char buffer[100];
90        FD_SET(0,&rfds);
91        FD_SET(connect_fd,&rfds);
92        status = select(connect_fd+1, &rfds, NULL, NULL, NULL);
93        if (FD_ISSET(0,&rfds)) {
94            status = read(0,buffer,sizeof(buffer));
95            if (status <= 0) {
96                fprintf(stderr,"Goodbye\n");
97                exit(0);
98            } else {
99                status = write(connect_fd, buffer, status);
100                if (status < 0) {
101                    perror("write");
102                }
103            }
104        } else if (FD_ISSET(connect_fd,&rfds)) {
105            status = read(connect_fd,buffer,sizeof(buffer));
106            if (status <= 0) {
107                fprintf(stderr,"Server closed connection\n");
108                exit(0);
109            } else {
110                status = write(1, buffer, status);
111                if (status < 0) {
112                    perror("write");
113                }
114            }
115        } else {
116            fprintf(stderr,"Strange case for select: %d\n", status);
117            exit(1);
118        }
119    }
120
121    return 0;
122}
123
Note: See TracBrowser for help on using the repository browser.