mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 18:03:37 +02:00
Support for (read-only) TCP connection agent communication
This commit is contained in:
parent
a72882a743
commit
6b3a51139a
5 changed files with 75 additions and 12 deletions
48
main.cc
48
main.cc
|
@ -1,8 +1,15 @@
|
|||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "agent.h"
|
||||
#include "connection.h"
|
||||
#include "map.h"
|
||||
|
||||
int
|
||||
|
@ -12,19 +19,40 @@ main(int argc, char *argv[])
|
|||
|
||||
srandom(time(NULL));
|
||||
|
||||
class tile &agentpos = map.tile_at(random() % map.w, random() % map.h);
|
||||
class agent agent(0, agentpos);
|
||||
int lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in sin;
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(27753);
|
||||
sin.sin_addr.s_addr = INADDR_ANY;
|
||||
int optval = 1;
|
||||
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
bind(lfd, (struct sockaddr *) &sin, sizeof(sin));
|
||||
listen(lfd, 10);
|
||||
|
||||
map.print_map();
|
||||
std::cout << '\n';
|
||||
int cfd;
|
||||
while ((cfd = accept(lfd, NULL, NULL)) >= 0) {
|
||||
class connection conn(cfd);
|
||||
class tile &agentpos = map.tile_at(random() % map.w, random() % map.h);
|
||||
class agent agent(0, agentpos, conn);
|
||||
|
||||
agent.move_dir(1, 0);
|
||||
map.print_map();
|
||||
std::cout << '\n';
|
||||
while (true) {
|
||||
map.print_map();
|
||||
std::cout << '\n';
|
||||
|
||||
agent.move_dir(0, -1);
|
||||
map.print_map();
|
||||
std::cout << '\n';
|
||||
char around[4] = {
|
||||
agent.tile->tile_in_dir(0, -1).symbol(),
|
||||
agent.tile->tile_in_dir(1, 0).symbol(),
|
||||
agent.tile->tile_in_dir(0, 1).symbol(),
|
||||
agent.tile->tile_in_dir(-1, 0).symbol(),
|
||||
};
|
||||
conn.senses(around);
|
||||
|
||||
usleep(1000000);
|
||||
}
|
||||
|
||||
/* TODO: destroy agent cleanly */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue