mirror of
https://github.com/brmlab/brmlife.git
synced 2025-08-02 09:53:38 +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
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
|||
CFLAGS=-Wall -O3 -g
|
||||
|
||||
OBJS=main.o map.o agent.o
|
||||
OBJS=main.o map.o agent.o connection.o
|
||||
|
||||
brmlife: $(OBJS)
|
||||
$(CXX) -o $@ $^
|
||||
|
|
5
agent.h
5
agent.h
|
@ -3,12 +3,15 @@
|
|||
|
||||
#include "map.h"
|
||||
|
||||
class connection;
|
||||
|
||||
class agent {
|
||||
public:
|
||||
int id;
|
||||
class tile *tile;
|
||||
class connection &conn;
|
||||
|
||||
agent(int id_, class tile &tile_) : id (id_), tile (&tile_)
|
||||
agent(int id_, class tile &tile_, class connection &conn_) : id (id_), tile (&tile_), conn (conn_)
|
||||
{
|
||||
put_at(tile_);
|
||||
};
|
||||
|
|
15
connection.cc
Normal file
15
connection.cc
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "agent.h"
|
||||
#include "connection.h"
|
||||
|
||||
void
|
||||
connection::senses(char around[4])
|
||||
{
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf), "around %c%c%c%c\n\n", around[0], around[1], around[2], around[3]);
|
||||
write(fd, buf, strlen(buf));
|
||||
}
|
17
connection.h
Normal file
17
connection.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef BRMLIFE__CONNECTION_H
|
||||
#define BRMLIFE__CONNECTION_H
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "map.h"
|
||||
|
||||
class connection {
|
||||
public:
|
||||
int fd;
|
||||
|
||||
connection(int fd_) : fd(fd_) {}
|
||||
|
||||
void senses(char around[4]);
|
||||
};
|
||||
|
||||
#endif
|
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