From 56cc59cf44ec64440ba4d1c0d005196195c758e6 Mon Sep 17 00:00:00 2001 From: hugues Date: Sat, 25 Mar 2006 15:07:51 +0000 Subject: Nettoyage du repository glagen git-svn-id: file:///usr/local/opt/svn/repos/glagen@12 0f7e0d06-a6f9-0310-a55f-d5f984f55e4c --- algo_distribue/Distribue.cc | 1 + algo_distribue/Distribue.hh | 97 +++++++++ algo_distribue/Makefile | 51 +++++ algo_distribue/main.cc | 113 ++++++++++ algo_distribue/network/Client.cc | 1 + algo_distribue/network/Client.hh | 113 ++++++++++ algo_distribue/network/Makefile | 62 ++++++ algo_distribue/network/Server.cc | 1 + algo_distribue/network/Server.hh | 218 +++++++++++++++++++ algo_distribue/network/data/Data.cc | 1 + algo_distribue/network/data/Data.hh | 78 +++++++ algo_distribue/network/data/Data_exemple.cc | 1 + algo_distribue/network/data/Data_exemple.hh | 66 ++++++ algo_distribue/network/data/Data_string.cc | 1 + algo_distribue/network/data/Data_string.hh | 60 ++++++ algo_distribue/network/main_client.cc | 54 +++++ algo_distribue/network/main_server.cc | 73 +++++++ algo_distribue/tools/matrix.cc | 3 + algo_distribue/tools/matrix.hh | 324 ++++++++++++++++++++++++++++ algo_distribue/tree/Makefile | 45 ++++ algo_distribue/tree/main.cc | 99 +++++++++ algo_distribue/tree/node.cc | 11 + algo_distribue/tree/node.hh | 78 +++++++ algo_distribue/tree/tree.cc | 11 + algo_distribue/tree/tree.hh | 51 +++++ 25 files changed, 1613 insertions(+) create mode 100644 algo_distribue/Distribue.cc create mode 100644 algo_distribue/Distribue.hh create mode 100644 algo_distribue/Makefile create mode 100644 algo_distribue/main.cc create mode 100644 algo_distribue/network/Client.cc create mode 100644 algo_distribue/network/Client.hh create mode 100644 algo_distribue/network/Makefile create mode 100644 algo_distribue/network/Server.cc create mode 100644 algo_distribue/network/Server.hh create mode 100644 algo_distribue/network/data/Data.cc create mode 100644 algo_distribue/network/data/Data.hh create mode 100644 algo_distribue/network/data/Data_exemple.cc create mode 100644 algo_distribue/network/data/Data_exemple.hh create mode 100644 algo_distribue/network/data/Data_string.cc create mode 100644 algo_distribue/network/data/Data_string.hh create mode 100644 algo_distribue/network/main_client.cc create mode 100644 algo_distribue/network/main_server.cc create mode 100644 algo_distribue/tools/matrix.cc create mode 100644 algo_distribue/tools/matrix.hh create mode 100644 algo_distribue/tree/Makefile create mode 100644 algo_distribue/tree/main.cc create mode 100644 algo_distribue/tree/node.cc create mode 100644 algo_distribue/tree/node.hh create mode 100644 algo_distribue/tree/tree.cc create mode 100644 algo_distribue/tree/tree.hh (limited to 'algo_distribue') diff --git a/algo_distribue/Distribue.cc b/algo_distribue/Distribue.cc new file mode 100644 index 0000000..09efc50 --- /dev/null +++ b/algo_distribue/Distribue.cc @@ -0,0 +1 @@ +#include "Distribue.hh" diff --git a/algo_distribue/Distribue.hh b/algo_distribue/Distribue.hh new file mode 100644 index 0000000..ff3b76d --- /dev/null +++ b/algo_distribue/Distribue.hh @@ -0,0 +1,97 @@ +// Classe Distribue + + + +#ifndef DISTRIBUE_HH_ +# define DISTRIBUE_HH_ + +#include "tree/tree.hh" +#include "tree/node.hh" +#include "network/data/Data.hh" +#include "network/Server.hh" +#include "tools/matrix.hh" + +template class Distribue +{ +public: + Distribue(const Tree& tree) + : _tree(tree), + _size(tree.get_node_root().get_number_node()), + _mat(new Matrix (_size, _size)) + { + unsigned int ptr1 = 0, ptr2 = 0; + std::list< Node > ptr_node = *new std::list< Node >; + ptr_node.push_back(tree.get_node_root()); + while (ptr_node.size()) + { + std::list< Node >::iterator childs = ptr_node.begin(); + std::list< Node >::iterator child = + childs->get_childs()->begin(); + for (; child != childs->get_childs()->end(); ++child) + { + ptr_node.push_back(*child); + (*_mat)(++ptr2, ptr1) = 1; + } + ptr_node.pop_front(); + ptr1++; + } + } + + void depend(std::list& data, const unsigned int& ptr) + { + data.push_back(_tree.get_node_root().get_node_course_width(ptr).get_data()); + for (unsigned int i = 0; i < _size; ++i) + if ((*_mat)(i, ptr)) + depend(data, i); + } + + void transfer(Server& server) + { + // Calcul du nombre de client necessaire + unsigned int j = 0; + std::list< list > list_data = + *new std::list< list >; + std::list data = *new std::list; + for (unsigned int i = 1; i < _size; ++i) + { + if ((*_mat)(i, 0)) // Ne prend que les noeuds fils du noeud root + { + ++j; + this->depend(data, i); + list_data.push_back(data); + data = *new std::list; + } + } + if (server.get_nb_client() < j) + { + std::cout << "Need more client" << std::endl; + exit(-1); + } + + std::list::const_iterator fd_client = + server.get_list_fd()->begin(); + data = *new std::list; + for (unsigned int i = 1; i < _size; ++i) + { + if ((*_mat)(i, 0)) // Ne prend que les noeuds fils du noeud root + { + ++j; + this->depend(data, i); + server.send_data(*fd_client++, Data(&data)); + data = *new std::list; + } + } + } + + Matrix& get_matrix() + { + return (*_mat); + } + +private: + Tree _tree; + unsigned int _size; + Matrix *_mat; +}; + +#endif // DISTRIBUTE_HH_ diff --git a/algo_distribue/Makefile b/algo_distribue/Makefile new file mode 100644 index 0000000..fb30eba --- /dev/null +++ b/algo_distribue/Makefile @@ -0,0 +1,51 @@ +## +## Makefile for in +## +## Made by meng-tih lam +## Login +## +## Started on Thu Feb 7 19:08:57 2002 meng-tih lam + +## + +NAME = glagen_divide +SRC_TOOLS = tools/matrix.cc +SRC_DATA = network/data/Data.cc network/data/Data_exemple.cc +SRC_TREE = tree/node.cc tree/tree.cc +SRC_NETWORK = network/Server.cc network/Client.cc +SRC_MAIN = Distribue.cc main.cc +SRCS = ${SRC_TOOLS} ${SRC_TREE} ${SRC_NETWORK}\ + ${SRC_DATA} ${SRC_MAIN} + +OBJS = $(SRCS:.cc=.o) + +ARCHI_i586 = -g2 -Wall -W -Werror -O3 +ARCHI_NetBSD = -g2 -Wall -W -Werror -O3 -I/usr/X11R6/include +ARCHI_sun4 = -g2 -Wall -W -Werror -O3 +ARCHI_alpha = -g2 -Wall -W -Werror -O3 + +LIB_i586 = +LIB_NetBSD = -L/usr/pkg/lib -L/usr/X11R6/lib -lGL -lGLU -lglut +LIB_alpha = +LIB_sun4 = + +CPPFLAGS = -I. +CC = g++ +RM = rm -f + +.PHONY: all clean deps + +all: $(OBJS) + $(CC) -o $(NAME) $(CFLAGS) $(OBJS)\ + $(LIB_${HOSTTYPE}) $(ARCHI_${HOSTTYPE}) + +.cc.o: + $(CC) $(ARCHI_${HOSTTYPE}) $(CPPFLAGS) -c $< -o $(<:.cc=.o) + +clean: + $(RM) $(OBJS) $(NAME) *~ \#*\# + +deps: + makedepend $(SRC) 2>/dev/null + +re: clean all diff --git a/algo_distribue/main.cc b/algo_distribue/main.cc new file mode 100644 index 0000000..e78d461 --- /dev/null +++ b/algo_distribue/main.cc @@ -0,0 +1,113 @@ +// Algo distribue + +// glagen_divide [n] +// n indique le nombre d'ordinateur en reseau exclut le serveur + +#include +#include +#include + +// Load Tree +#include "tree/node.hh" +#include "tree/tree.hh" + +// Load Network +#include "network/Server.hh" +#include "network/Client.hh" + +// Load Data +#include "network/data/Data_string.hh" + +// Load Tool +#include "tools/matrix.hh" + +#include "Distribue.hh" + +#include +#include + +// Prototype : test d'essais avec 2 clients +// On changera suivant les donnees passes par GTK +// Il faut que le nombre de client soit superieur ou egale +// au nombre de noeud fils par rapport au noeud principal +#define NB_CLIENT 2 + +char gl_handle; + +Tree *create_tree(); + +void help(char *prog_name) +{ + std::cout << prog_name << " [port]" << std::endl; + exit(0); +} + +void signal_alarm(int) +{ + gl_handle = gl_handle | 1; +} + +void check_args(int argc, char *argv1, char *argv2) +{ + int current; + + if (argc != 2) + { + std::cerr << "Error server : Error parameters" << std::endl; + help(argv1); + } + for (current = 0; argv2[current] != '\0'; current++) + if (argv2[current] < '0' || argv2[current] > '9') + { + std::cerr << "Error server : Error parameters" << std::endl; + help(argv1); + } +} + +int main(int argc, char *argv[]) +{ + // Exemple de creation d'un arbre (donnees string) + // On peut appele une classe abstraite dont ses fils sont + // les types de donnees des librairies (ex: Tree) + Tree* tree; + tree = create_tree(); + + // Calcul de l'algo distribue : calcul l'attribution des taches + // pour chaque client + Distribue network(*tree); + std::cout << "Matrice calcule" << std::endl + << network.get_matrix() << std::endl; + + // Execution du serveur + check_args(argc, argv[0], argv[1]); + Server server(atoi(argv[1]), NB_CLIENT); + + // Transfert des donnees + network.transfer(server); + + return (0); +} + + + +Tree *create_tree() +{ + string root = "Connection..."; + string a = "Est-ce que ca fonctionne ?"; + string b = "1212123"; + string c = "OK recu 5/5"; + Tree tree(root); + tree.add_node(a); + tree.add_node(b); + tree.add_node(c); + + Tree* main_tree = new Tree(string("Execution...")); + main_tree->add_tree(tree); + + Tree tree2(string("Une tentative de suicide")); + tree2.add_node(string("Ou ca?")); + tree2.add_node(string("A Epita")); + tree2.add_node(string("Ping pong ping")); + main_tree->add_tree(tree2); + return (main_tree); +} diff --git a/algo_distribue/network/Client.cc b/algo_distribue/network/Client.cc new file mode 100644 index 0000000..23ccb2c --- /dev/null +++ b/algo_distribue/network/Client.cc @@ -0,0 +1 @@ +#include "Client.hh" diff --git a/algo_distribue/network/Client.hh b/algo_distribue/network/Client.hh new file mode 100644 index 0000000..923b83e --- /dev/null +++ b/algo_distribue/network/Client.hh @@ -0,0 +1,113 @@ +// Classe Client + +#ifndef CLIENT_HH_ +# define CLIENT_HH_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "data/Data.hh" + +class Client +{ +public: + Client(char* host, const int& port) + { + char *c; + int sock; + struct sockaddr_in sa; + long addr; + struct hostent *host_info; + + if ((host_info = gethostbyname(host)) == 0) + this->error(); + c = host_info->h_addr; + addr = c[0] << 24 | c[1] << 16 | c[2] << 8 | c[3]; + if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) + this->error(); + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = htonl(addr); + if (connect(sock, (struct sockaddr *)&sa, sizeof (sa)) == -1) + this->error(); + _fd_server = sock; + } + + void error() + { + std::cerr << "Error client : "; + perror(""); + exit(errno); + } + + template + void send_data(const Data& data) const + { + data.send(_fd_server); + } + + template + void received_data(Data& data) + { + data.receive(_fd_server); + } + + void wait_signal() + { + unsigned char sig = 0; + read(_fd_server, &sig, sizeof(unsigned char)); + if (sig != 42) + { + std::cout << "Erreur de transmission" << std::endl; + exit(1); + } + std::cout << "En communication avec le serveur..." << std::endl; + } + + void send_signal() + { + unsigned char sig = 42; + write(_fd_server, &sig, sizeof(unsigned char)); + } + + int do_select() + { + fd_set rd; + char buf[256]; + int n; + + FD_ZERO(&rd); + FD_SET(0, &rd); + FD_SET(_fd_server, &rd); + if (select(_fd_server + 1, &rd, 0, 0, 0) == -1) + this->error(); + if (FD_ISSET(_fd_server, &rd)) + { + n = read(_fd_server, buf, 150); + if (n <= 0) + return (1); + write(1, buf, n); + fflush(0); + } + if (FD_ISSET(0, &rd)) + { + n = read(0, buf, 100); + write(_fd_server, buf, n); + } + return (0); + } + + +private: + int _fd_server; +}; + +#endif // CLIENT_HH_ diff --git a/algo_distribue/network/Makefile b/algo_distribue/network/Makefile new file mode 100644 index 0000000..18878df --- /dev/null +++ b/algo_distribue/network/Makefile @@ -0,0 +1,62 @@ +## +## Makefile for in +## +## Made by meng-tih lam +## Login +## +## Started on Thu Feb 7 19:08:57 2002 meng-tih lam + +## + +NAME_SERVER = glagen_server +NAME_CLIENT = glagen_client +SRC_DATA = data/Data.cc\ + data/Data_exemple.cc\ + data/Data_string.cc +SRC_SERVER = Server.cc main_server.cc +SRC_CLIENT = Client.cc main_client.cc + +OBJ_SERVER = $(SRC:.cc=.o) $(SRC_SERVER:.cc=.o) +OBJ_CLIENT = $(SRC:.cc=.o) $(SRC_CLIENT:.cc=.o) + +ARCHI_i586 = -g2 -Wall -W -Werror -O3 +ARCHI_NetBSD = -g2 -Wall -W -Werror -O3 -I/usr/X11R6/include +ARCHI_sun4 = -g2 -Wall -W -Werror -O3 +ARCHI_alpha = -g2 -Wall -W -Werror -O3 + +LIB_i586 = -lc +LIB_NetBSD = -L/usr/pkg/lib -L/usr/X11R6/lib -lGL -lGLU -lglut -lc +LIB_alpha = +LIB_sun4 = -lsocket -lnsl + +CPPFLAGS = -I. +CC = g++ +RM = rm -f + +.PHONY: all clean deps + +all: server client + +server: $(NAME_SERVER) + +client: $(NAME_CLIENT) + +$(NAME_SERVER): $(OBJ_SERVER) + $(CC) -o $(NAME_SERVER) $(CFLAGS) $(OBJ_SERVER)\ + $(LIB_${HOSTTYPE}) $(ARCHI_${HOSTTYPE}) + +$(NAME_CLIENT): $(OBJ_CLIENT) + $(CC) -o $(NAME_CLIENT) $(CFLAGS) $(OBJ_CLIENT)\ + $(LIB_${HOSTTYPE}) $(ARCHI_${HOSTTYPE}) + +.cc.o: + $(CC) $(ARCHI_${HOSTTYPE}) $(CPPFLAGS) -c $< + +clean: + $(RM) $(OBJ_SERVER) $(OBJ_CLIENT) $(NAME_SERVER) $(NAME_CLIENT)\ + *~ \#*\# + +deps: + makedepend $(SRC) 2>/dev/null + +re: clean all diff --git a/algo_distribue/network/Server.cc b/algo_distribue/network/Server.cc new file mode 100644 index 0000000..63f45ff --- /dev/null +++ b/algo_distribue/network/Server.cc @@ -0,0 +1 @@ +#include "Server.hh" diff --git a/algo_distribue/network/Server.hh b/algo_distribue/network/Server.hh new file mode 100644 index 0000000..fdfecb4 --- /dev/null +++ b/algo_distribue/network/Server.hh @@ -0,0 +1,218 @@ +// Classe Server + +#ifndef SERVER_HH_ +# define SERVER_HH_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "data/Data.hh" + +extern char gl_handle; + +extern void signal_alarm(int); + +template class Data; + +class Server +{ +public: + Server(const int& port, const unsigned int& nb_client_max) + { + _port = port; + _client_max = nb_client_max; + _fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + _sock_in = new struct sockaddr_in; + _sock_in->sin_family = PF_INET; + _sock_in->sin_port = htons(port); + _sock_in->sin_addr.s_addr = INADDR_ANY; + if ((bind(_fd, (struct sockaddr *)_sock_in, sizeof (*_sock_in)) == -1) + || (listen(_fd, 5) == -1)) + this->error(); + std::cout << "Server started. Listening on port " << _port << std::endl; + _fd_client = new std::list; + this->start_signal(); + this->start(); + } + + void start() + { + int fd_client; + socklen_t len; + struct sockaddr_in sock_client; + + len = sizeof (struct sockaddr_in); + while (gl_handle != -1 && this->_fd_client->size() < _client_max) + { + fd_client = 0; + if ((fd_client = accept(this->_fd, + (struct sockaddr *)&(sock_client), &len)) > 0) + gl_handle = gl_handle | 2; + std::list list_fd; + if (2 == (gl_handle & 2)) + { + this->accept_client(fd_client); + this->send_signal(fd_client); + std::cout << "Number of connected clients :" + << this->_fd_client->size() << std::endl; + } + gl_handle = 0; + this->test_client(); + } + } + + void test_client() + { + if (_fd_client->size()) + { + std::list::const_iterator fd_client_tmp; + std::list::const_iterator fd_client = _fd_client->begin(); + int i = 0; + while (fd_client != _fd_client->end()) + { + i++; + char c = 0; + errno = 0; + fd_client_tmp = fd_client; + read(*fd_client, &c, sizeof(char)); + ++fd_client; + if (errno == 0) + this->remove_client(*fd_client_tmp); + } + } + } + + void error() + { + std::cerr << "Error server : "; + perror(""); + exit(errno); + } + + void remove_client(const int& fd) + { + _fd_client->remove(fd); + std::cout << "Client in the file descriptor : " << fd + << " are disconnected" << std::endl; + std::cout << "Number of connected clients :" + << this->_fd_client->size() << std::endl; + } + + void wait_signal(std::list& list_fd) // retourne la liste des files descriptors a lire + { + struct pollfd poll_fd[_fd_client->size()]; + std::list::const_iterator fd = _fd_client->begin(); + for (int i = 0; fd != _fd_client->end(); ++fd, ++i) + { + poll_fd[i].fd = *fd; + poll_fd[i].events = POLLIN; + } + if (poll(poll_fd, _fd_client->size(), 0) < 0) + assert(0); + for (unsigned int i = 0; i < _fd_client->size(); ++i) + if (poll_fd[i].revents != 0) + { + unsigned char sig = 0; + unsigned int length = 0; + do + { + errno = 0; + length = read(poll_fd[i].fd, &sig, sizeof(unsigned char)); + } + while (errno == 4); + if (errno) + { + perror(""); + exit(errno); + } + if (length == 0) + remove_client(poll_fd[i].fd); + else + if (sig == 42) + { + list_fd.push_back(poll_fd[i].fd); + std::cout << "Reception signal sur le file descriptor :" + << poll_fd[i].fd << std::endl; + } + else + { + std::cout << "Erreur de reception sur le file descriptor :" + << poll_fd[i].fd << std::endl; + exit(2); + } + poll_fd[i].revents = 0; + } + } + + void send_signal(const int& fd) + { + unsigned char sig = 42; + write(fd, &sig, sizeof(unsigned char)); + } + + void start_signal() + { + struct sigaction alarm; + + alarm.sa_handler = signal_alarm; + alarm.sa_flags = 0; + sigemptyset(&(alarm.sa_mask)); + sigaddset(&(alarm.sa_mask), SIGALRM); + sigaction(SIGALRM, &alarm, 0); + ualarm(1, 100000); + } + + template + void send_data(const int& fd, const Data& data) + { + data.send(fd); + } + + template + void send_data(const Data& data, + const std::list& fd_client) + { + std::list::const_iterator fd = fd_client.begin(); + for (; fd != fd_client.end(); ++fd) + data.send(*fd); + } + + template + void received_data(Data& data, const std::list& fd_client) + { + std::list::const_iterator fd = fd_client.begin(); + for (; fd != fd_client.end(); ++fd) + data.receive(*fd); + } + + void accept_client(const int& fd) + { + std::cout << "Client connected on file descriptor: " << fd << std::endl; + _fd_client->push_back(fd); + } + + int get_fd() const { return (_fd); } + + int get_port() const { return (_port); } + + std::list* get_list_fd() const { return (_fd_client); } + + unsigned int get_nb_client() const { return (_fd_client->size()); } + +private: + std::list *_fd_client; + struct sockaddr_in *_sock_in; + int _port; + unsigned int _client_max; + int _fd; +}; + +#endif // SERVER_HH_ diff --git a/algo_distribue/network/data/Data.cc b/algo_distribue/network/data/Data.cc new file mode 100644 index 0000000..d3bbe6a --- /dev/null +++ b/algo_distribue/network/data/Data.cc @@ -0,0 +1 @@ +#include "Data.hh" diff --git a/algo_distribue/network/data/Data.hh b/algo_distribue/network/data/Data.hh new file mode 100644 index 0000000..3b630ff --- /dev/null +++ b/algo_distribue/network/data/Data.hh @@ -0,0 +1,78 @@ +// Classe Data + +// Dans le TYPEDATA, il faut l'implementation de toString (retourne un string) +// et de type (retourne unsigned short int) + +// Type 0: char +// Type 1: unsigned char +// Type 2: int +// Type 3: unsigned int +// Type 4: short int +// Type 5: unsigned short int +// Type 6: long int +// Type 7: unsigned long int +// Type 8: float +// Type 9: double +// Type 10: long double + +#ifndef DATA_HH_ +# define DATA_HH_ + +#include +#include +#include + +#include + +template class Data +{ +public: + Data() : _data(new std::list) {}; + + Data(std::list* data) : _data(data) {}; + + void error() + { + perror(""); + exit(errno); + } + + void add_data(const TYPEDATA& data) { _data->push_back(data); }; + + void send(const int& fd) const + { + std::list::iterator data = _data->begin(); + unsigned int size = _data->size(); + std::cout << "Taille a ecrire:" << size << std::endl; + write(fd, &size, sizeof(unsigned int)); + for (; data != _data->end(); ++data) + data->write_data(fd); + } + + void receive(const int& fd) + { + unsigned int size = 0; + do + { + errno = 0; + read(fd, &size, sizeof(unsigned int)); + } + while(errno == 4); + if (errno) + this->error(); + std::cout << "Taille lu: " << size << std::endl; + TYPEDATA data; + for (unsigned int i = 0; i < size; ++i) + { + data.read_data(fd); + _data->push_back(data); + } + } + + std::list* get_data() { return (_data); } + +private: + std::list* _data; +}; + +#endif // DATA_HH_ diff --git a/algo_distribue/network/data/Data_exemple.cc b/algo_distribue/network/data/Data_exemple.cc new file mode 100644 index 0000000..9578493 --- /dev/null +++ b/algo_distribue/network/data/Data_exemple.cc @@ -0,0 +1 @@ +#include "Data_exemple.hh" diff --git a/algo_distribue/network/data/Data_exemple.hh b/algo_distribue/network/data/Data_exemple.hh new file mode 100644 index 0000000..ca1291b --- /dev/null +++ b/algo_distribue/network/data/Data_exemple.hh @@ -0,0 +1,66 @@ +// Classe Data_exemple + +// Un exemple sur un point de coordonnees 3D + +#ifndef DATA_EXEMPLE_HH_ +# define DATA_EXEMPLE_HH_ + +#include +#include // Pour write/read + +#include + +class Data_exemple +{ +public: + Data_exemple() : _x(0), _y(0), _z(0) {}; + Data_exemple(int x, int y, int z) : _x(x), _y(y), _z(z) {}; + + void write_data(const int& fd) const + { + std::cout << "Donnees envoyes au serveur" << std::endl; + std::cout << _x << std::endl; + std::cout << _y << std::endl; + std::cout << _z << std::endl; + write(fd, &_x, sizeof(int)); + write(fd, &_y, sizeof(int)); + write(fd, &_z, sizeof(int)); + } + + void read_data(const int& fd) + { + do + { + errno = 0; + read(fd, &_x, sizeof(int)); + } + while (errno == 4); + + do + { + errno = 0; + read(fd, &_y, sizeof(int)); + } + while (errno == 4); + + do + { + errno = 0; + read(fd, &_z, sizeof(int)); + } + while (errno == 4); + + std::cout << "Reception message sur le file descriptor :" << fd + << std::endl; + std::cout << _x << std::endl; + std::cout << _y << std::endl; + std::cout << _z << std::endl; + } + +private: + int _x; + int _y; + int _z; +}; + +#endif // DATA_EXEMPLE diff --git a/algo_distribue/network/data/Data_string.cc b/algo_distribue/network/data/Data_string.cc new file mode 100644 index 0000000..9578493 --- /dev/null +++ b/algo_distribue/network/data/Data_string.cc @@ -0,0 +1 @@ +#include "Data_exemple.hh" diff --git a/algo_distribue/network/data/Data_string.hh b/algo_distribue/network/data/Data_string.hh new file mode 100644 index 0000000..a395725 --- /dev/null +++ b/algo_distribue/network/data/Data_string.hh @@ -0,0 +1,60 @@ +#ifndef DATA_STRING_HH_ +# define DATA_STRING_HH_ + +#include +#include +#include // Pour write/read + +#include + +class Data_string +{ +public: + Data_string() : _str("") {}; + Data_string(const string& str) : _str(str) {}; + + void write_data(const int& fd) const + { + + std::cout << "Donnees envoyes" << std::endl << _str << std::endl; + unsigned int size = _str.size(); + write(fd, &size, sizeof(unsigned int)); + for (unsigned int i = 0; i < size; ++i) + { + char car = _str[i]; + write(fd, &car, sizeof(char)); + } + } + + void read_data(const int& fd) + { + unsigned int size = 0; + do + { + errno = 0; + read(fd, &size, sizeof(size)); + } + while (errno == 4); + + _str = ""; + for (unsigned int i = 0; i < size; ++i) + { + char car; + do + { + errno = 0; + read(fd, &car, sizeof(char)); + } + while (errno == 4); + _str += car; + } + + std::cout << "Reception message sur le file descriptor :" << fd + << std::endl << _str << std::endl; + } + +private: + string _str; +}; + +#endif // DATA_STRING diff --git a/algo_distribue/network/main_client.cc b/algo_distribue/network/main_client.cc new file mode 100644 index 0000000..66cb083 --- /dev/null +++ b/algo_distribue/network/main_client.cc @@ -0,0 +1,54 @@ +// Main du client + +#include +#include +#include "Client.hh" +#include "data/Data_exemple.hh" +#include "data/Data_string.hh" + +void help(char *prog_name) +{ + std::cout << prog_name << " [hostname] [port]" << std::endl; + exit(0); +} + +void check_args(int argc, char **argv) +{ + int current; + + if (argc != 3) + { + std::cerr << "Error client : Error parameters" << std::endl; + help(argv[0]); + } + for (current = 0; argv[2][current] != '\0'; current++) + if (argv[2][current] < '0' || argv[2][current] > '9') + { + std::cerr << "Error client : Error parameters" << std::endl; + help(argv[0]); + } +} + +int main(int argc, char **argv) +{ + check_args(argc, argv); + Client client(argv[1], atoi(argv[2])); +// Data data; +// data.add_data(Data_exemple(10, 11, 13)); +// data.add_data(Data_exemple(1, 4, 81)); + client.wait_signal(); +// client.send_signal(); +// client.send_data(data); + +// Data data_string; +// client.received_data(data_string); + + // On devrait implementer un numero a chaque classe associe + // Je fais ca pour l'instant comme si on connait deja le type de classe + // c'est juste pour tester + Data data_string; + client.received_data(data_string); + + while (0 == client.do_select()); + return (0); +} diff --git a/algo_distribue/network/main_server.cc b/algo_distribue/network/main_server.cc new file mode 100644 index 0000000..4fafd09 --- /dev/null +++ b/algo_distribue/network/main_server.cc @@ -0,0 +1,73 @@ +// Main du reseau server + +#include +#include "Server.hh" +#include "data/Data_exemple.hh" +#include "data/Data_string.hh" + +char gl_handle; + +void help(char *prog_name) +{ + std::cout << prog_name << " [port]" << std::endl; + exit(0); +} + +void signal_alarm(int) +{ + gl_handle = gl_handle | 1; +} + +void check_args(int argc, char *argv1, char *argv2) +{ + int current; + + if (argc != 2) + { + std::cerr << "Error server : Error parameters" << std::endl; + help(argv1); + } + for (current = 0; argv2[current] != '\0'; current++) + if (argv2[current] < '0' || argv2[current] > '9') + { + std::cerr << "Error server : Error parameters" << std::endl; + help(argv1); + } +} + +int main(int argc, char* argv[]) +{ +// int fd_client; +// socklen_t len; +// struct sockaddr_in sock_client; + +// len = sizeof (struct sockaddr_in); + check_args(argc, argv[0], argv[1]); + Server server(atoi(argv[1]), 3); + server.get_list_fd(); +// server.start_signal(); +// while (gl_handle != -1) +// { +// fd_client = 0; +// if ((fd_client = accept(server.get_fd(), +// (struct sockaddr *)&(sock_client), &len)) > 0) +// gl_handle = gl_handle | 2; +// std::list list_fd; +// if (2 == (gl_handle & 2)) +// { +// server.accept_client(fd_client); +// server.send_signal(fd_client); +// } +// Data data; +// server.wait_signal(list_fd); +// server.received_data(data, list_fd); + +// Data data_string; +// Data_string ack("Aknowledge"); +// data_string.add_data(ack); +// server.send_data(data_string, list_fd); + +// gl_handle = 0; +// } +// return (0); +} diff --git a/algo_distribue/tools/matrix.cc b/algo_distribue/tools/matrix.cc new file mode 100644 index 0000000..7991769 --- /dev/null +++ b/algo_distribue/tools/matrix.cc @@ -0,0 +1,3 @@ + + +#include "matrix.hh" diff --git a/algo_distribue/tools/matrix.hh b/algo_distribue/tools/matrix.hh new file mode 100644 index 0000000..4894360 --- /dev/null +++ b/algo_distribue/tools/matrix.hh @@ -0,0 +1,324 @@ +// matrix.hh + +#include + +#ifndef MATRIX_HH_ +# define MATRIX_HH_ + +// la class TYPE ne peut etre que de type nombre (ex: double, int, long,...) + +template class Matrix +{ +public: + // Constructor + Matrix(const unsigned int i, const unsigned int j) + { + this->_i = i; + this->_j = j; + this->_lines = new _line[i]; // Create lines... + for (unsigned int k = 0; k < i; ++k) + this->_lines[k] = new TYPE[j]; // ...and create column + for (unsigned int k = 0; k < i; ++k) + for (unsigned int l = 0; l < j; ++l) + this->_lines[k][l] = 0; + } + + // Constructor by copy + Matrix(const Matrix& mat) + { + this->_i = mat._i; + this->_j = mat._j; + this->_lines = new _line[this->_i]; + for (unsigned int k = 0; k < this->_i; ++k) + { + this->_lines[k] = new TYPE[this->_j]; + for (unsigned int l = 0; l < this->_j; ++l) + this->_lines[k][l] = mat._lines[k][l]; + } + } + + // Destructor + ~Matrix() + { + if (this->_i != 0 || this->_j != 0) + { + for (unsigned int i = 0; i < this->_i; i++) + delete [] this->_lines[i]; + delete [] this->_lines; + } + } + + // Affected operator + Matrix& operator=(const Matrix& mat) + { + if (&mat != this) + { + if (mat._i != this->_i || mat._j != this->_j) // Test dim + { + this->~Matrix(); // Destroy... + this->_i = mat._i; + this->_j = mat._j; + this->_lines = new _line[this->_i]; // ...and realloc + for (unsigned int i = 0; i < this->_i; ++i) + this->_lines[i] = new TYPE[this->_j]; + } + for (unsigned int i = 0; i < this->_i; ++i) // Copy + for (unsigned int j = 0; j < this->_j; ++j) + this->_lines[i][j] = mat._lines[i][j]; + } + return (*this); + } + + // Add elements to matrix + Matrix& add_elems(const int& nb) + { + Matrix mat(this->_i + nb, this->_j + nb); + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < this->_j; j++) + mat(i, j) = this->_lines[i][j]; + *this = mat; + return (*this); + } + + // Add matrix to matrix + Matrix& add_mat(const Matrix& mat) + { + unsigned int i = this->_i; + unsigned int j = this->_j; + this->add_elems(mat._i); + for (unsigned int k = i; k < this->_i; k++) + for (unsigned int l = j; l < this->_j; l++) + this->_lines[k][l] = mat(k - i, l - j); + return (*this); + } + + // Add of matrix + Matrix operator+(const Matrix& mat) + { + Matrix mat_tmp(this->_i, mat._j); + if (this->_i != mat._i || this->_j != mat._j) + { + std::cerr << "Error of addiction of 2 matrix (dimensions)"\ + << std::endl; + return (mat_tmp); + } + for (unsigned int i = 0; i < this->_i; ++i) + for (unsigned int j = 0; j < this->_j; ++j) + mat_tmp._lines[i][j] = this->_lines[i][j] + mat._lines[i][j]; + return (mat_tmp); + } + + Matrix operator+(const Matrix& mat) const + { + Matrix mat_tmp(this->_i, mat._j); + if (this->_i != mat._i || this->_j != mat._j) + { + std::cerr << "Error of addiction of 2 matrix (dimensions)"\ + << std::endl; + return (mat_tmp); + } + for (unsigned int i = 0; i < this->_i; ++i) + for (unsigned int j = 0; j < this->_j; ++j) + mat_tmp._lines[i][j] = this->_lines[i][j] + mat._lines[i][j]; + return (mat_tmp); + } + + // Sub matrix + Matrix operator-(const Matrix& mat) + { + Matrix mat_tmp(this->_i, mat._j); + if (this->_i != mat._i || this->_j != mat._j) + { + std::cerr << "Error of substraction of 2 matrix (dimensions)"\ + << std::endl; + return (mat_tmp); + } + for (unsigned int i = 0; i < this->_i; ++i) + for (unsigned int j = 0; j < this->_j; ++j) + mat_tmp._lines[i][j] = this->_lines[i][j] - mat._lines[i][j]; + return (mat_tmp); + } + + Matrix operator-(const Matrix& mat) const + { + Matrix mat_tmp(this->_i, mat._j); + if (this->_i != this->_j || this->_i != mat._i || mat._i != mat._j) + { + std::cerr << "Error of substraction of 2 matrix (dimensions)"\ + << std::endl; + return (mat_tmp); + } + for (unsigned int i = 0; i < this->_i; ++i) + for (unsigned int j = 0; j < this->_j; ++j) + mat_tmp._lines[i][j] = this->_lines[i][j] - mat._lines[i][j]; + return (mat_tmp); + } + + // Multiplication of matrix + Matrix operator*(const Matrix& mat) + { + Matrix mat_tmp(this->_i, mat._j); + if (this->_j != mat._i) // Check dimension + { + std::cerr << "Error of produce of 2 matrix (dimensions)"\ + << std::endl; + return (mat_tmp); + } + for (unsigned int i = 0; i < this->_i; ++i) + { + for (unsigned int j = 0; j < mat._j; ++j) + { + TYPE res = 0; + // Produce lines and column of matrix + for (unsigned int k = 0; k < this->_j; ++k) + res += this->_lines[i][k] * mat._lines[k][j]; + mat_tmp._lines[i][j] = res; + } + } + return (mat_tmp); + } + + // Multiplication with real + Matrix& operator*(const TYPE& nbr) + { + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < this->_j; j++) + { + this->_lines[i][j] *= nbr; + } + return (*this); + } + + // Divide with real + Matrix& operator/(const TYPE& nbr) + { + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < this->_j; j++) + { + this->_lines[i][j] /= nbr; + } + return (*this); + } + + //Get dimension of matrix + const int get_dim() const + { + if (this->_i != this->_j) + { + std::cerr << "Matrix isn't nxn, cannot give dimension" << std::endl; + return (0); + } + return (this->_i); + } + + // Operator to change mat(i, j) + // to can write mat(i, j) = ... + TYPE& operator()(unsigned int i, unsigned int j) + { + return (this->_lines[i][j]); + } + + // Operator to access mat(i, j) + // to can write int x = mat(i, j) + TYPE operator()(unsigned int i, unsigned int j) const + { + return (this->_lines[i][j]); + } + + // Test matrix nul + const bool test_nul() const + { + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < this->_j; j++) + if (this->_lines[i][j] != 0) + return (0); + return (1); + } + + // Test diagonal matrix nul + const int test_diag() const + { + for (unsigned int i = 0; i < this->_i; i++) + if (this->_lines[i][i] != 0) + return (i); + return (-1); + } + + // Test diagonal matrix nul with element + const int test_diag(const unsigned int& j) const + { + for (unsigned int i = 0; i < this->_i; i++) + if (this->_lines[i][i] != 0 && i == j) + return (i); + return (-1); + } + + // Calculate trace + const TYPE trace() const + { + TYPE res = 0; + if (this->_i != this->_j) + { + std::cerr << "Matrix isn't nxn, cannot give trace" << std::endl; + return (0); + } + for (unsigned int i = 0; i < this->_i; i++) + res += this->_lines[i][i]; + return (res); + } + + // Transpose + Matrix& transpose() + { + if (this->_i != this->_j) + { + std::cerr << "Matrix isn't nxn, cannot transpose" << std::endl; + return (*this); + } + TYPE tmp = 0; + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < i; j++) + { + tmp = this->_lines[i][j]; + this->_lines[i][j] = this->_lines[j][i]; + this->_lines[j][i] = tmp; + } + return (*this); + } + + Matrix transpose() const + { + Matrix mat(this->_j, this->_i); + for (unsigned int i = 0; i < this->_i; i++) + for (unsigned int j = 0; j < this->_j; j++) + mat._lines[i][j] = this->_lines[j][i]; + return (mat); + } + + // Display matrix + void print(std::ostream &os) const + { + for (unsigned int i = 0; i < this->_i; ++i) + { + for (unsigned int j = 0; j < this->_j; ++j) + os << this->_lines[i][j] << " "; + os << std::endl; + } + } + +protected: + typedef TYPE* _line; + _line* _lines; + unsigned int _i; // Number of lines + unsigned int _j; // Number of column +}; + +template +inline std::ostream& operator<<(std::ostream& ostr, const Matrix& stream) +{ + stream.print(ostr); + return (ostr); +} + + +#endif // MATRIX_HH diff --git a/algo_distribue/tree/Makefile b/algo_distribue/tree/Makefile new file mode 100644 index 0000000..895117b --- /dev/null +++ b/algo_distribue/tree/Makefile @@ -0,0 +1,45 @@ +## +## Makefile for in +## +## Made by meng-tih lam +## Login +## +## Started on Thu Feb 7 19:08:57 2002 meng-tih lam + +## + +NAME = glagen_tree +SRCS = node.cc tree.cc main.cc + +OBJS = $(SRCS:.cc=.o) + +ARCHI_i586 = -g2 -Wall -W -Werror -O3 +ARCHI_NetBSD = -g2 -Wall -W -Werror -O3 -I/usr/X11R6/include +ARCHI_sun4 = -g2 -Wall -W -Werror -O3 +ARCHI_alpha = -g2 -Wall -W -Werror -O3 + +LIB_i586 = +LIB_NetBSD = -L/usr/pkg/lib -L/usr/X11R6/lib -lGL -lGLU -lglut +LIB_alpha = +LIB_sun4 = + +CPPFLAGS = -I. +CC = g++ +RM = rm -f + +.PHONY: all clean deps + +all: $(OBJS) + $(CC) -o $(NAME) $(CFLAGS) $(OBJS)\ + $(LIB_${HOSTTYPE}) $(ARCHI_${HOSTTYPE}) + +.cc.o: + $(CC) $(ARCHI_${HOSTTYPE}) $(CPPFLAGS) -c $< + +clean: + $(RM) $(OBJS) $(NAME) *~ \#*\# + +deps: + makedepend $(SRC) 2>/dev/null + +re: clean all diff --git a/algo_distribue/tree/main.cc b/algo_distribue/tree/main.cc new file mode 100644 index 0000000..3b6f02d --- /dev/null +++ b/algo_distribue/tree/main.cc @@ -0,0 +1,99 @@ +// +// main.cc for in +// +// Made by meng-tih lam +// Login +// +// Started on Fri Aug 16 02:41:45 2002 meng-tih lam +// + +#include +#include +#include + +#include "node.hh" +#include "tree.hh" + +// Pour construire un arbre, il faut d'abord faire les fils +// et les raccoller avec le pere avec la methode add_tree + +int test_tree() +{ + // ** Exemple avec la classe ** + // ** On peut mettre n'importe classe ** + + // * Support du sous-arbre * + string root = "Connection..."; + string a = "Est-ce que ca fonctionne ?"; + string b = "1212123"; + string c = "OK recu 5/5"; + Tree tree(root); + tree.add_node(a); + tree.add_node(b); + tree.add_node(c); + + // Affichage root de l'arbre + std::cout << "root: " << tree.get_node_root().get_data() << std::endl; + + // Affichage de ses fils + unsigned int nb_childs = tree.get_nb_childs(); + for (unsigned int i = 0; i < nb_childs; ++i) + std::cout << tree.get_child(i).get_data() << std::endl; + std::cout << std::endl << std::endl; + + + + // * Support Arbre principale * + Tree main_tree("Execution..."); + + // Voici la methode pour lier un arbre et un sous-arbre + main_tree.add_tree(tree); + + std::cout << "root main: " << main_tree.get_node_root().get_data() + << std::endl; + unsigned int nb_childs2 = main_tree.get_nb_childs(); + + // Ici arbre sur 2 niveaux (Bien sur qu'on peut faire en recursivite ^_^) + for (unsigned int i = 0; i < nb_childs2; ++i) + { + nb_childs = main_tree.get_child(i).get_nb_childs(); + std::cout << main_tree.get_child(i).get_data() << std::endl; + for (unsigned int j = 0; j < nb_childs; ++j) + std::cout << main_tree.get_child(i).get_child(j).get_data() + << std::endl; + } + std::cout << std::endl << std::endl; + + + + + // * Allez on rajoute pour compliquer les choses * + Tree tree2("Une tentative de suicide"); + tree2.add_node("Ou ca?"); + tree2.add_node("A Epita"); + tree2.add_node("Ping pong ping"); + main_tree.add_tree(tree2); + + std::cout << "root main: " << main_tree.get_node_root().get_data() + << std::endl; + nb_childs2 = main_tree.get_nb_childs(); + + // Ici arbre sur 2 niveaux (Bien sur qu'on peut faire en recursivite ^_^) + for (unsigned int i = 0; i < nb_childs2; ++i) + { + nb_childs = main_tree.get_child(i).get_nb_childs(); + std::cout << main_tree.get_child(i).get_data() << std::endl; + for (unsigned int j = 0; j < nb_childs; ++j) + std::cout << main_tree.get_child(i).get_child(j).get_data() + << std::endl; + } + + return (0); +} + + +int main() +{ + test_tree(); + return (0); +} diff --git a/algo_distribue/tree/node.cc b/algo_distribue/tree/node.cc new file mode 100644 index 0000000..a3ae387 --- /dev/null +++ b/algo_distribue/tree/node.cc @@ -0,0 +1,11 @@ +// +// node.cc for in +// +// Made by meng-tih lam +// Login +// +// Started on Fri Aug 16 02:40:40 2002 meng-tih lam +// Last update Fri Aug 16 02:41:05 2002 meng-tih lam +// + +#include "node.hh" diff --git a/algo_distribue/tree/node.hh b/algo_distribue/tree/node.hh new file mode 100644 index 0000000..b80a699 --- /dev/null +++ b/algo_distribue/tree/node.hh @@ -0,0 +1,78 @@ + +#ifndef NODE_HH_ +# define NODE_HH_ + +#include +// Rajouter l'include correspondant a la template de TYPENODE +// + implemente le std::cout abtrait pour les tests + +template class Tree; // Declaration prealable + +template class Node +{ + friend class Tree; + +public: + Node(const TYPENODE &d) : _link(new std::list), _data(d) {} + + void add_child(const TYPENODE& value) { _link->push_back(Node(value)); } + + void add_tree_node(const Node& value) { _link->push_back(value); } + + TYPENODE get_data() const { return (_data); } + + const unsigned int get_nb_childs() const { return (_link->size()); } + + std::list* get_childs() const { return (_link); } + + unsigned int get_number_node() const + { + unsigned int number_childs = _link->size(); + std::list::const_iterator child = _link->begin(); + unsigned int number_node = 1; + for (unsigned int tmp = 0; tmp != number_childs; ++tmp) + { + number_node += child->get_number_node(); + ++child; + } + return (number_node); + } + + // Renvoie le noeud fils suivant le numero a partir de 0 + Node get_child(const unsigned int& i) const + { + assert(i < _link->size()); + std::list::const_iterator child = _link->begin(); + for (unsigned int tmp = 0; tmp != i; ++tmp) + ++child; + return (*child); + } + + // Numerotation de l'arbre suivant un parcours en largeur de 0 a n + Node get_node_course_width(const int& ptr) const + { + int rptr = ptr; + std::list ptr_node = *new std::list; + ptr_node.push_back(*this); + while (ptr_node.size()) + { + std::list::iterator childs = ptr_node.begin(); + std::list::iterator child = childs->get_childs()->begin(); + for (; child != childs->get_childs()->end(); ++child) + { + ptr_node.push_back(*child); + if (--rptr == 0) + return (*child); + } + ptr_node.pop_front(); + } + return (*this); + } + +private: + + std::list *_link; // Pointeur vers le sous-arbre + TYPENODE _data; +}; + +#endif // NODE_HH_ diff --git a/algo_distribue/tree/tree.cc b/algo_distribue/tree/tree.cc new file mode 100644 index 0000000..047ca89 --- /dev/null +++ b/algo_distribue/tree/tree.cc @@ -0,0 +1,11 @@ +// +// tree.cc for in +// +// Made by meng-tih lam +// Login +// +// Started on Fri Aug 16 02:45:56 2002 meng-tih lam +// Last update Fri Aug 16 17:11:24 2002 meng-tih lam +// + +#include "tree.hh" diff --git a/algo_distribue/tree/tree.hh b/algo_distribue/tree/tree.hh new file mode 100644 index 0000000..86ce136 --- /dev/null +++ b/algo_distribue/tree/tree.hh @@ -0,0 +1,51 @@ + + +#ifndef TREE_HH_ +# define TREE_HH_ + +#include +#include +#include "node.hh" +// Rajouter l'include correspondant a la template de TYPENODE +// + implemente le std::cout abstrait pour les tests + +template class Tree +{ +public: + // Construit la racine de l'arbre avec l'info dans son contenu + Tree(const TYPENODE& value) : _root(new Node(value)) {} + + Tree(const Tree& value) : _root(value._root) {} + + // Ajoute un noeud fils (avec info) a l'arbre + void add_node(const TYPENODE& value) { _root->add_child(value); } + + // Renvoie la racine de l'arbre de type Node + Node& get_node_root() const { return (*_root); } + + // Ajoute un autre arbre a la racine de l'arbre courant + void add_tree(const Tree &value) + { + _root->add_tree_node(value.get_node_root()); + } + + // Renvoie la liste de Node de ses fils + std::list< Node >& get_childs() { return _root->get_childs(); } + + // Information du nombre de fils + const unsigned int get_nb_childs() const + { + return (_root->get_nb_childs()); + } + + // Renvoie le noeud fils suivant le numero a partir de 0 + Node get_child(const unsigned int& i) const + { + return (_root->get_child(i)); + } + +private: + Node *_root; +}; + +#endif // TREE_HH_ -- cgit v1.2.3