summaryrefslogtreecommitdiff
path: root/trunk/glagen/algo_distribue/network/data/Data.hh
blob: 3b630ff0fbb364a6d4bc7a2b469af0d83166ec89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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_