Commit addf5f4d by Szeberényi Imre

v0

parents
#
# Makefile for dummy players
#
SRC = main.cpp player.cpp player.h test.sh Makefile mpimain.cpp README
MPICC = mpiCC
CXXFLAGS = -DDEBUG
#LDFLAGS = -lsocket -lnsl # for solaris opsys
all: main mpimain
clean:
rm -f *.log *.o main mpimain
tar:
tar -czvf client.tgz ${SRC}
player.o: player.cpp player.h
main.o: main.cpp player.h
main: main.o player.o
${CXX} ${CXXFLAGS} -o main main.o player.o ${LDFLAGS}
mpimain.o: mpimain.cpp player.h
${MPICC} ${CXXFLAGS} -c mpimain.cpp
mpimain: mpimain.o player.o
${MPICC} ${CXXFLAGS} -o mpimain mpimain.o player.o
####
Dummy palyaers for labs
####
player.h, player.cpp :
* Player's basic class. Implementing the server communication.
main.cpp :
* Implements a simple burglar and cop players.
* They are moving randomly and create log messages in *.log files.
test.sh :
* Test script for the simple players version.
mpimain.cpp :
* Simple MPI communication example.
* The silly policeman moving randomly and shouting to his radio, if
he sees the robber.
* The others can hear this, but they don't know where the robber is.
mptest.sh :
* Test script for the MPI version. Starts a burglar, and starts the
MPI processes.
Compile:
module load mpi
make
Starting the clients:
Futtatas:
Feltetelezzuk, hogy a laborgepen, amin dolgozunk a szervert elinditottuk. (szerver.bat)
A szerver fixen a 15623-as porton hallgat. Ide kell tunnelezni a klinenseket.
Hogy ne legyen portutkozes, az a javaslat hogy mindeki egyedi portot (RABLO_PORT) hasznaljon.
RABLO_PORT=15000+UID, ahol UID az adott felhasznalo UID-je (id -u)
let RABLO_PORT=15000+`id -u`
export RABLO_PORT
A laborgepet es a jatekosokat futtato gepet (pl. ural2) ssh tunnelen keresztuk kapcsoljuk ossze.
(putty:
Session->SSH->Tunnels
X Local ports accept connection from othrer hosts
Source port: RABLO_PORT_ERTEKE
Destination: localhost:15623
X Remote
X Ipv4
Add
)
Ha a javas server fut indithato a test.sh, vagy az mpitest.sh
A *.log-okban lathato a kommunikacio.
./test.sh: line 20: ./main: No such file or directory
/**
* \file: main.cpp
*
*/
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "player.h"
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
/// microsecond sleep
/// @param t - time to sleep in useconds
// @return -1 if signal has been received, 0 otherwise
int uSleep(int usec) {
struct timespec tim = { 0, usec*1000L }, trem;
return nanosleep(&tim, &trem);
}
int main(int argc, char ** argv) {
if(argc != 4) {
cerr << "Usage: " << argv[0] << " <hostname> <port> burglar|cop>" << endl;
return 1;;
}
// processing the command line arguments
std::stringstream ss(argv[2]);
int port;
ss >> port; // first argument is the port number
try {
// starting the player
Player pl(argv[1], port, std::string(argv[3]) == "burglar" ? Player::burglar : Player::cop);
cerr << argv[3] << ": " << pl.getx() <<" " << pl.gety() << endl;
while(1) {
uSleep(100); // just for slowing down
int r = rand() % (Player::right + 1);
Player::direction d;
if (r == 0) d = Player::up;
else if (r==1) d = Player::down;
else if (r==2) d = Player::left;
else d = Player::right;
char *buffer = pl.move(d);
#ifdef DEBUG
cout << "dir:" << r << " typ:" << pl.gettyp() << " ret:" << buffer;
#endif
delete[] buffer;
}
} catch (std::runtime_error &e) {
cerr << e.what() << endl;
}
return 0;
}
/**
* \file: mpimain.cpp
*
*
* Simple MPI communication example.
*
* The silly policeman is moving randomly and
* shouting to his radio: "Burglar" if he sees the robber.
* The others can hear this, but they don't know where
* the robber is because his fool does not say it.
*
* Compile:
* make mpimain
*
* Executing:
* mpitest.sh
*
*/
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <typeinfo>
#include <mpi.h>
#include "player.h"
using std::cin;
#define TAG_RADIO 1 // Radio communication
/// microsecond sleep
/// @param t - time to sleep in useconds
// @return -1 if signal has been received, 0 otherwise
int uSleep(int usec) {
struct timespec tim = { 0, usec*1000L }, trem;
return nanosleep(&tim, &trem);
}
/// debMsg class for debugging.
/// Collects the output and inserts to the stream when the flush() is called.
/// In this way the outputs of differnt parallel tasks will not mixed (hopefully).
class debMsg : public std::stringstream {
std::ostream& os;
public:
debMsg(std::ostream& s = std::cerr) :os(s) {}
void flush() {
(os << this->str()).flush();
this->str("");
}
~debMsg() { flush(); }
};
/// My endl
class endl_t {} endl;
/// My endline inserted to the stream
std::ostream& operator<<(std::ostream& os, endl_t) {
os.put('\n'); // newline
try {
dynamic_cast<debMsg&>(os).flush(); // convertable -> calls flush()
} catch (std::bad_cast) {
os.flush(); // can't cast
}
return os;
}
int main(int argc, char ** argv) {
debMsg cerr; // this is the own cerr, not from std!!!
try {
MPI::Init(argc, argv);
int rank = MPI::COMM_WORLD.Get_rank();
int size = MPI::COMM_WORLD.Get_size();
if(argc != 3) {
cerr << "Usage: " << argv[0] << " <hostname> <port>" << endl;
MPI::Finalize();
return 1;;
}
// processing the command line arguments
std::stringstream ss(argv[2]);
int port;
ss >> port; // first argument is the port number
// starting the policeman
Player pl(argv[1], port, Player::cop);
cerr << "Initial position of cop #" << rank
<< " (" << pl.getx() << "," << pl.gety() << ")" << endl;
while(1) {
uSleep(100); // just for slowing down the process
MPI::Status stat;
if (MPI::COMM_WORLD.Iprobe(MPI::ANY_SOURCE, TAG_RADIO, stat)) { // message is ready
int len = stat.Get_count(MPI::CHAR);
char buff[100];
MPI::COMM_WORLD.Recv(buff, len, MPI::CHAR, stat.Get_source(), TAG_RADIO);
cerr << "Cop #" << rank << " got a message from cop #"
<< stat.Get_source() << ": " << buff << endl;
}
int r = rand() % (Player::right + 1); // random movement
Player::direction d;
if (r == 0) d = Player::up;
else if (r == 1) d = Player::down;
else if (r == 2) d = Player::left;
else d = Player::right;
char *buffer = pl.move(d); // move command
// now we check the answer
for (char *p = buffer; *p; p++) {
if (*p == '1') { // It is a robber
// shouting to the radio:
char msg[] = "Burgar";
for (int i = 0; i < size; i++) { // to all
if (i != rank)
MPI::COMM_WORLD.Send(msg, sizeof(msg), MPI::CHAR, i, TAG_RADIO);
}
break; // we do not expect mot than one robber on the street
}
}
delete[] buffer;
}
MPI::Finalize();
} catch (std::runtime_error &err) {
cerr << err.what() << endl;
MPI::COMM_WORLD.Abort(1);
} catch (MPI::Exception &err) { // this generally not enabled
cerr << err.Get_error_string() << endl;
MPI::COMM_WORLD.Abort(1);
}
return 0;
}
#!/bin/bash
cleanup() {
kill -9 $BPID $CPIDS &>/dev/null
}
trap "cleanup" EXIT
# Computes the uniq port number for communication
if [ x$PORT = x ]; then
let PORT=15000+`id -u`
fi
echo PORT: $PORT
PORT=$PORT
HOST=localhost
COPS=5 # The number of cops
echo "The burglar appeared at the bank and robbed it."
./main $HOST $PORT burglar &>burglar.log &
BPID=$!
echo "The burglar began to escape."
sleep 5
echo "The policemen are going to the scene ..."
mpirun -np $COPS mpimain $HOST $PORT &>cops.log &
CPIDS=$!
sleep 30
echo "The burglar has been catched or time is ower."
trap "cleanup" EXIT
/**
* \file: palyer.cpp
*
*/
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include "player.h"
using namespace std;
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
Player::Player(const char *host, unsigned int port, player_t cop) {
s = socket(PF_INET, SOCK_STREAM, 0);
if (s < 0)
throw runtime_error("Couldn't open socket!");
struct hostent *hostinfo;
struct sockaddr_in name;
name.sin_family = AF_INET;
name.sin_port = htons (port);
hostinfo = gethostbyname (host);
if (hostinfo == NULL)
throw runtime_error("No nhostinfo!");
name.sin_addr = *(struct in_addr *) hostinfo->h_addr;
if (connect(s, (struct sockaddr *) &name, sizeof (name)) < 0) {
stringstream err;
err << "Can't connect to server " << host << " through port " << port;
throw runtime_error(err.str());
}
//send login message
me = cop;
char login[3] = "M\n";
if (cop) login[0] = 'N';
if(send(s, login, 2, MSG_NOSIGNAL) <= 0)
throw runtime_error("Unable to send");
cerr << "Login message sent" << endl;
//wait for initial pozition
char buffer[128];
int poz = 0;
do {
if (recv(s, &buffer[poz], 1, 0) <= 0)
throw runtime_error("Recv error");
} while(buffer[poz++] != '\n');
buffer[poz] = '\0';
stringstream ss;
ss << buffer;
ss >> x;
ss.ignore(1);
ss >> y;
}
char *Player::move(direction d) {
switch (d) {
case up:
if (send(s, "U", 1, MSG_NOSIGNAL) <= 0) throw runtime_error("Move: send error");
break;
case down:
if (send(s, "D", 1, MSG_NOSIGNAL) <= 0) throw runtime_error("Move: send error");
break;
case left:
if (send(s, "L", 1, MSG_NOSIGNAL) <= 0) throw runtime_error("Move: send error");
break;
case right:
if (send(s, "R", 1, MSG_NOSIGNAL) <= 0) throw runtime_error("Move: send error");
break;
default:
throw runtime_error("Illegal move cmd");
}
char * buffer = new char[128];
int poz = 0;
do {
if (recv(s, &buffer[poz], 1, 0) <= 0)
throw runtime_error("Move: recv error");
} while (buffer[poz++] != '\n');
buffer[poz]='\0';
return buffer;
}
/**
* \file: palyer.h
*
*/
#ifndef PLAYER_H
#define PLAYER_H
#include <stdexcept>
class Player
{
public:
typedef enum {up, down, left, right} direction;
typedef enum {burglar, cop} player_t;
// Create player and login to the server
Player(const char *host, unsigned int port, player_t cop);
// Move on the board
char *move(direction d);
// getters
int getx(){ return x; }
int gety(){ return y; }
player_t gettyp() { return me; }
// setters
void setx(int x) { this->x = x; }
void sety(int y) { this->y = y; }
private:
int x; // x pozition
int y; // y pozition
int s; // socket
player_t me; // type
};
#endif
#!/bin/bash
cleanup() {
kill -9 $BPID $CPIDS &>/dev/null
}
trap "cleanup" EXIT
# Cumoutes the uniq port number for communication
if [ x$PORT = x ]; then
let PORT=15000+`id -u`
fi
echo PORT: $PORT
PORT=$PORT
HOST=localhost
COPS=5 # The number of cops
echo "The burglar appeared at the bank and robbed it."
./main $HOST $PORT burglar &>burglar.log &
BPID=$!
echo "The burglar began to escape."
sleep 5
echo "The policemen are going to the scene ..."
for i in `seq 1 $COPS`
do
./main $HOST $PORT cop &>cop.$i.log &
echo "Cop #$i started to chase ..."
CPIDS="$CPIDS $!"
sleep 1
done
echo "No more cops are comming."
sleep 30
echo "The burglar has been catched or time is ower."
REM
REM
start java -jar "labirint_server.jar"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment