summaryrefslogtreecommitdiff
path: root/algo_distribue/network/data
diff options
context:
space:
mode:
Diffstat (limited to 'algo_distribue/network/data')
-rw-r--r--algo_distribue/network/data/Data.cc1
-rw-r--r--algo_distribue/network/data/Data.hh78
-rw-r--r--algo_distribue/network/data/Data_exemple.cc1
-rw-r--r--algo_distribue/network/data/Data_exemple.hh66
-rw-r--r--algo_distribue/network/data/Data_string.cc1
-rw-r--r--algo_distribue/network/data/Data_string.hh60
6 files changed, 207 insertions, 0 deletions
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 <list>
+#include <iostream>
+#include <stdio.h>
+
+#include <errno.h>
+
+template<class TYPEDATA> class Data
+{
+public:
+ Data() : _data(new std::list<TYPEDATA>) {};
+
+ Data(std::list<TYPEDATA>* 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<TYPEDATA>::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<TYPEDATA>* get_data() { return (_data); }
+
+private:
+ std::list<TYPEDATA>* _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 <iostream>
+#include <unistd.h> // Pour write/read
+
+#include <errno.h>
+
+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 <iostream>
+#include <string>
+#include <unistd.h> // Pour write/read
+
+#include <errno.h>
+
+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