File: robot.c

package info (click to toggle)
netmaze 0.81+jpg0.82-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,188 kB
  • ctags: 911
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 58; sh: 16
file content (169 lines) | stat: -rw-r--r-- 3,413 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* This is a demonstration on how to write robots.
 * This is the basecode.
 * Write your own: own_action()/init_robot() - functions (see dummy.c)
 * and compile the whole stuff with robot.o,allmove.o,network.o!
 */

#include "netmaze.h"

#include <sys/types.h>

#ifdef RS6000
 #include <sys/select.h>
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <memory.h>

#include "network.h"

#include "trigtab.h"

#define TRUE  1
#define FALSE 0

struct shared_struct shstr;
struct shared_struct *sm = &shstr;

void io_handler(int a);

/* extern: allmove.c */
extern void move_all(PLAYER*,int*);
extern void run_game(MAZE*,PLAYER*);
extern void myrandominit(long);

/* extern: user-defined-functions */
extern int own_action(void);
extern void start_robot(int);
extern int init_robot(void);

/* extern: network.c */
extern struct robot_functions robot;
extern void handle_socket(void);
extern void ident_player(void);

#ifdef HAVE_FDSET
fd_set readmask;
#else
struct fd_mask readmask;
#endif

static struct hostent *hp;            /* pointer to host info for remote host */
static struct sockaddr_in remoteaddr; /* server connect */

int own_socket;         /* streams socket descriptor */

int main(int argc,char **argv)
{
  int  ret;
  char hostname[256];

#ifdef USE_SIGVEC
  struct sigvec vec;
#else
  struct sigaction vec;
#endif

  if(argc != 2)
  {
    fprintf(stderr,"%s: usage: %s <serverhostname>\n",argv[0],argv[0]);
    exit(1);
  }
  strcpy(hostname,argv[1]);

  if(!init_robot())
  {
    fprintf(stderr,"%s: Can't initialize the robot.\n",argv[0]);
    exit(1);
  }
  robot.valid = TRUE;
  robot.action = own_action;
  robot.start  = start_robot;

#ifdef USE_SIGVEC
  vec.sv_handler = (void (*)(int)) io_handler;
  vec.sv_mask = 0;
  vec.sv_flags = 0;
  if ( sigvector(SIGIO, &vec, (struct sigvec *) 0) == -1)
    perror(" sigaction(SIGIO)");
#else
  vec.sa_handler = (void (*)(int)) io_handler;
 #ifdef RS6000 /* ibm rs/6000 */
   sigemptyset(&vec.sa_mask);
 #else
  vec.sa_mask = 0;
 #endif
  vec.sa_flags = 0;
  if ( sigaction(SIGIO, &vec, (struct sigaction *) 0) == -1)
    perror(" sigaction(SIGIO)");
#endif
  remoteaddr.sin_family = AF_INET;

  sm->sologame = FALSE;

  if ((hp = gethostbyname(hostname) ) == NULL)
  {
    fprintf(stderr, "netmaze: %s not found in /etc/hosts\n",hostname); exit(1);
  }

  remoteaddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

  if((own_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  {
    fprintf(stderr, "netmaze: unable to create socket\n"); exit(1);
  }

  remoteaddr.sin_port = htons(NETMAZEPORT);
  FD_ZERO(&readmask);
  FD_SET(own_socket,&readmask);

  if ( (ret=connect(own_socket,(struct sockaddr *) &remoteaddr, sizeof(struct sockaddr))) == -1)
  {
     perror("connect"); exit(1);
  }

  ident_player();
  printf("Connectet to %s. Connectcode: %d.\n",hostname,ret);

  for(;;) io_handler(0);
}

void io_handler(int a)
{
  int	numfds;

#ifdef HAVE_FDSET
  fd_set readmask1;
#else
  struct fd_mask readmask1;
#endif

  for(;;)
  {
    readmask1 = readmask;

    if((numfds = select(32,&readmask1,NULL,NULL,NULL))< 0)
      continue;

    if(numfds == 0) break;

    if(FD_ISSET(own_socket,&readmask1))
    {
      handle_socket();
    }
  }
}


/***********
 * dummy audio-functions
 */

void play_sound(int a) { }