vrmodel

changeset 5:9e260c091f75 tip

wrote some server code in the test input tool
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 15 Sep 2014 10:57:31 +0300
parents a32b151fb3c6
children
files inptools/test/src/main.c src/inpclient.cc
diffstat 2 files changed, 109 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/inptools/test/src/main.c	Thu Sep 11 00:08:23 2014 +0300
     1.2 +++ b/inptools/test/src/main.c	Mon Sep 15 10:57:31 2014 +0300
     1.3 @@ -15,7 +15,7 @@
     1.4  #include "proto.h"
     1.5  
     1.6  struct client {
     1.7 -	int s;
     1.8 +	int active;
     1.9  	struct sockaddr_in addr;
    1.10  	struct client *next;
    1.11  };
    1.12 @@ -24,10 +24,15 @@
    1.13  int handle_js(int fd);
    1.14  int start_srv(int port);
    1.15  int handle_net(int s);
    1.16 +int respond(int s, int rsp, struct sockaddr_in *sa);
    1.17 +int respond_msg(int s, struct message *msg, struct sockaddr_in *sa);
    1.18 +struct client *add_client(struct sockaddr_in *addr);
    1.19 +struct client *find_client(struct sockaddr_in *addr);
    1.20  
    1.21  static int opt_dport = DEF_PORT;
    1.22  static const char *opt_jspath = "/dev/input/js0";
    1.23  
    1.24 +static int sock = -1;
    1.25  static struct client *clients;
    1.26  
    1.27  static int js_num_axes;
    1.28 @@ -37,7 +42,7 @@
    1.29  
    1.30  int main(int argc, char **argv)
    1.31  {
    1.32 -	int js, sock;
    1.33 +	int js;
    1.34  	int maxfd;
    1.35  
    1.36  	if((js = open_js(opt_jspath)) == -1) {
    1.37 @@ -80,7 +85,7 @@
    1.38  		fprintf(stderr, "failed to open js device: %s: %s\n", jspath, strerror(errno));
    1.39  		return -1;
    1.40  	}
    1.41 -	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
    1.42 +	/*fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);*/
    1.43  
    1.44  	if(ioctl(fd, JSIOCGAXES, &axes) == -1) {
    1.45  		fprintf(stderr, "failed to get number of joystick axes\n");
    1.46 @@ -137,8 +142,9 @@
    1.47  		m.data.event.x = js_axis_state[0];
    1.48  		m.data.event.y = js_num_axes > 1 ? js_axis_state[1] : 0;
    1.49  		m.data.event.z = js_num_axes > 2 ? js_axis_state[2] : 0;
    1.50 +		m.data.event.bnmask = js_bn_state;
    1.51  
    1.52 -		sendto(c->s, &m, sizeof m, 0, (struct sockaddr*)&c->addr, sizeof c->addr);
    1.53 +		sendto(sock, &m, sizeof m, 0, (struct sockaddr*)&c->addr, sizeof c->addr);
    1.54  		c = c->next;
    1.55  	}
    1.56  	return 0;
    1.57 @@ -168,7 +174,106 @@
    1.58  	return s;
    1.59  }
    1.60  
    1.61 +
    1.62  int handle_net(int s)
    1.63  {
    1.64 +	struct message msg;
    1.65 +	struct sockaddr_in addr;
    1.66 +	socklen_t addrlen = sizeof addr;
    1.67 +	struct client *c;
    1.68 +
    1.69 +	if(recvfrom(s, &msg, sizeof msg, 0, (struct sockaddr*)&addr, &addrlen) < sizeof addr) {
    1.70 +		perror("failed to recieve message");
    1.71 +		return -1;
    1.72 +	}
    1.73 +	if(msg.magic != MAGIC) {
    1.74 +		fprintf(stderr, "received magic is not good\n");
    1.75 +		return -1;
    1.76 +	}
    1.77 +
    1.78 +	c = find_client(&addr);
    1.79 +
    1.80 +	switch(msg.type) {
    1.81 +	case REQ_DISCOVER:
    1.82 +		/* a client is looking for us */
    1.83 +		msg.type = RSP_OK;
    1.84 +		strcat(msg.data.raw, "test-input");
    1.85 +		if(respond_msg(s, &msg, &addr) == -1) {
    1.86 +			return -1;
    1.87 +		}
    1.88 +		break;
    1.89 +
    1.90 +	case REQ_START:
    1.91 +		if(!c) {
    1.92 +			if(!(c = add_client(&addr))) {
    1.93 +				respond(s, RSP_ERR, &addr);
    1.94 +				return -1;
    1.95 +			}
    1.96 +		}
    1.97 +		c->active = 1;
    1.98 +		if(respond(s, RSP_OK, &addr) == -1) {
    1.99 +			return -1;
   1.100 +		}
   1.101 +		break;
   1.102 +
   1.103 +	case REQ_STOP:
   1.104 +		if(c) {
   1.105 +			c->active = 0;
   1.106 +		}
   1.107 +		if(respond(s, RSP_OK, &addr) == -1) {
   1.108 +			return -1;
   1.109 +		}
   1.110 +		break;
   1.111 +
   1.112 +	default:
   1.113 +		break;
   1.114 +	}
   1.115 +
   1.116  	return 0;
   1.117  }
   1.118 +
   1.119 +int respond(int s, int rsp, struct sockaddr_in *sa)
   1.120 +{
   1.121 +	struct message m;
   1.122 +	memset(&m, 0, sizeof m);
   1.123 +	m.magic = MAGIC;
   1.124 +	m.type = rsp;
   1.125 +
   1.126 +	return respond_msg(s, &m, sa);
   1.127 +}
   1.128 +
   1.129 +int respond_msg(int s, struct message *msg, struct sockaddr_in *sa)
   1.130 +{
   1.131 +	if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)sa, sizeof *sa) == -1) {
   1.132 +		perror("failed to send response");
   1.133 +		return -1;
   1.134 +	}
   1.135 +	return 0;
   1.136 +}
   1.137 +
   1.138 +struct client *add_client(struct sockaddr_in *addr)
   1.139 +{
   1.140 +	struct client *c;
   1.141 +
   1.142 +	if(!(c = calloc(1, sizeof *c))) {
   1.143 +		perror("failed to add client");
   1.144 +		return 0;
   1.145 +	}
   1.146 +	c->addr = *addr;
   1.147 +	c->next = clients;
   1.148 +	clients = c;
   1.149 +	return c;
   1.150 +}
   1.151 +
   1.152 +struct client *find_client(struct sockaddr_in *addr)
   1.153 +{
   1.154 +	struct client *c = clients;
   1.155 +	while(c) {
   1.156 +		if(c->addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
   1.157 +				c->addr.sin_port == addr->sin_port) {
   1.158 +			return c;
   1.159 +		}
   1.160 +		c = c->next;
   1.161 +	}
   1.162 +	return 0;
   1.163 +}
     2.1 --- a/src/inpclient.cc	Thu Sep 11 00:08:23 2014 +0300
     2.2 +++ b/src/inpclient.cc	Mon Sep 15 10:57:31 2014 +0300
     2.3 @@ -121,17 +121,6 @@
     2.4  	}
     2.5  	setsockopt(s, SOL_SOCKET, SO_BROADCAST, &true_val, sizeof true_val);
     2.6  
     2.7 -	memset(&sa, 0, sizeof sa);
     2.8 -	sa.sin_family = AF_INET;
     2.9 -	sa.sin_port = htons(opt_dport);
    2.10 -	sa.sin_addr.s_addr = htonl(INADDR_ANY);
    2.11 -
    2.12 -	if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
    2.13 -		perror("failed to bind datagram socket");
    2.14 -		close(s);
    2.15 -		return -1;
    2.16 -	}
    2.17 -
    2.18  	do {
    2.19  		fd_set rdset;
    2.20