Project

General

Profile

Revision 902

Added by Tudor Achim over 15 years ago

python thingy

View differences:

trunk/code/projects/mapping/python/netserv.c
1
#include "netserv.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <strings.h>
5
#include <string.h>
6
#include <unistd.h>
7
#include <sys/types.h> 
8
#include <sys/socket.h>
9
#include <netinet/in.h>
10

  
11
int make_sock(int portno)
12
{
13
  
14
  int sockfd, newsockfd, clilen;
15
  struct sockaddr_in serv_addr, cli_addr;
16
  
17
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
18
  if (sockfd < 0)
19
    error("ERROR opening socket");
20
  bzero((char *) &serv_addr, sizeof(serv_addr));
21
  serv_addr.sin_family = AF_INET;
22
  serv_addr.sin_addr.s_addr = INADDR_ANY;
23
  serv_addr.sin_port = htons(portno);
24
  if (bind(sockfd, (struct sockaddr *) &serv_addr,
25
	   sizeof(serv_addr)) < 0) 
26
    error("ERROR on binding");
27
  listen(sockfd,5);
28
  clilen = sizeof(cli_addr);
29
  newsockfd = accept(sockfd, 
30
		     (struct sockaddr *) &cli_addr, 
31
		     (socklen_t *) &clilen);
32
  if (newsockfd < 0) 
33
    error("ERROR on accept");
34
  return newsockfd;
35
}
trunk/code/projects/mapping/python/server.c
1
/* A simple server in the internet domain using TCP
2
   The port number is passed as an argument */
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <strings.h>
6
#include <string.h>
7
#include <unistd.h>
8
#include <sys/types.h> 
9
#include <sys/socket.h>
10
#include <netinet/in.h>
11

  
12
#include "netserv.h"
13
#include "robots.h"
14
#include "wireless.h"
15

  
16
void donothin()
17
{
18
     return;
19
}
20

  
21

  
22
int main(int argc, char *argv[])
23
{
24
     char buffer[256];
25
     char outbuf[256];
26
     int n, newsockfd, len;
27
     Packet *received;
28

  
29
  
30
     if (argc < 2) {
31
	  fprintf(stderr,"usage: %s <port number>\n", argv[0]);
32
	  exit(1);
33
     }
34
     newsockfd = make_sock(atoi(argv[1]));
35

  
36
     make_listener();
37
     printf("Robot listener created.\n");
38

  
39
     bzero(buffer,256);
40

  
41
     while (1)
42
     {
43
	  wl_do();
44
	  
45
	  /* this is a completely useless queue, since wl_do only reads one packet at a time */
46
	  received = dequeue();
47
	  /* oh well */
48

  
49
	  if (received)
50
	  {
51
	       fprintf(stdout, "len: %d\n", buffer[1]);
52
	       buffer[0] = (char) received->source;
53
	       buffer[1] = (char) received->length-2;
54
	       buffer[2] = (char) received->type;
55
	       memcpy(buffer+3, received->packet, received->length);
56
	       
57
	       n = write(newsockfd, buffer, 200);
58
	       free(received);
59
	       bzero(buffer, 256);
60
	  }
61
     }
62
     return 0; 
63
}
64

  
65
void error(char *msg)
66
{
67
     printf(msg);
68
     exit(1);
69
}
trunk/code/projects/mapping/python/robots.c
1
#include <stdlib.h>
2
#include <wireless.h>
3
#include <string.h>
4
#include <stdio.h>
5
#include "robots.h"
6

  
7
typedef struct Node {
8
     Packet *info;
9
     struct Node *next;
10
     struct Node *prev;
11
} Node;
12

  
13
Node *thead = NULL;
14

  
15
void enqueue(char typ, int src, unsigned char *wrds,int len);
16

  
17
void make_listener()
18
{ 
19
     PacketGroupHandler *receiver;
20
     printf("wl_init() returned %d\n", wl_init());
21
     wl_set_channel(0xE);
22
     receiver = malloc(sizeof(PacketGroupHandler));
23
     receiver->groupCode = GROUP;
24
     receiver->handle_response = NULL;
25
     receiver->timeout_handler = NULL;
26
     receiver->handle_receive = enqueue;
27
     wl_register_packet_group(receiver);
28
}
29

  
30

  
31
void enqueue(char typ, int src, unsigned char *wrds,int len)
32
{
33
     Node *new = malloc(sizeof(Node));
34
     new->info = malloc(sizeof(Packet));
35
     new->info->source = src;
36
     new->info->length = len;
37
     new->info->type = typ;
38
     new->info->packet = malloc((strlen(wrds)+1)*sizeof(char));
39
     strcpy(new->info->packet, wrds);
40

  
41
     if (!thead)
42
	  thead = new;
43
     else
44
     {
45
	  new->next = thead;
46
	  thead->prev = new;
47
	  thead = new;
48
     }
49
}
50

  
51
Packet * dequeue()
52
{
53
     Node *iter;
54
     Packet *ret;
55
     if (!thead)
56
	  return NULL;
57
     for (iter = thead; iter->next != NULL; iter = iter->next)
58
	  ;
59
     ret = iter->info;
60
     if (iter->prev)
61
	  (iter->prev)->next = NULL;
62
     else
63
	  thead = NULL;
64
     free (iter);
65
     return ret;
66
}
trunk/code/projects/mapping/python/colony.py
1
#!/usr/bin/env python3.0
2
import sys, time
3
from socket import *
4

  
5
hostname='localhost'
6

  
7
def inp(str):
8
    return input(str).encode('ascii')
9

  
10
def main():
11

  
12
    funcs = {31 : 'print_range_info'}
13

  
14
    buf = []
15
    sock = socket()
16
    sock.connect((hostname, int(sys.argv[1])))
17
    
18
    while (True):
19
        buf = sock.recv(100)
20
        
21
        if (buf):
22
            type = buf[2]
23
            if type in funcs.keys():
24
                globals()[funcs[type]](buf)
25

  
26
        
27
def print_range_info(packet):
28
    print ('Info from %d' % packet[0])
29
    src = packet[0]
30
    len = packet[1]
31
    print(len)
32
    data = packet[3:]
33
    for i in range(len // 2):
34
        bot_id = data[i*2]
35
        max_range_id = data[i*2+1]
36
        print ('Max to bot %d is %d' % (bot_id, max_range_id))
37
    
38

  
39

  
40

  
41
if __name__=='__main__':
42

  
43
    if len(sys.argv) < 2 or not str.isdecimal(sys.argv[1]):
44
        print("Usage: %s <portno>" % sys.argv[0])
45
        sys.exit()
46

  
47
    main()
0 48

  
trunk/code/projects/mapping/python/netserv.h
1
#ifndef NETSERV
2
#define NETSERV
3

  
4
int make_sock(int portno);
5
void error(char *);
6

  
7
#endif
trunk/code/projects/mapping/python/Makefile
1
CC=gcc
2
CFLAGS=-W -Wall -lpthread -g
3
INCS=-I../../libwireless/lib
4
COMPS=../../libwireless/lib
5

  
6

  
7
server : clean
8
	$(CC) $(CFLAGS) $(INCS) ../../libwireless/lib/*.c server.c netserv.c robots.c -o server
9

  
10
clean :
11
	rm -f server
trunk/code/projects/mapping/python/script.sh
1
cd src/
2

  
3
gcc -c *.c -I/usr/include/python2.5/ -I../../colony-wireless/code/lib/include/libwireless/ *.c
4

  
5
gcc -shared  *.o -o wireless.so
0 6

  
trunk/code/projects/mapping/python/robots.h
1
#ifndef ROBOTS
2
#define ROBOTS
3

  
4
typedef struct Packet
5
{
6
  int source;
7
  int length;
8
  char type;
9
  char *packet;
10
} Packet;
11

  
12
#define GROUP 1
13

  
14
void make_listener();
15
Packet *dequeue();
16

  
17
#endif

Also available in: Unified diff