summaryrefslogtreecommitdiff
path: root/trunk/glagen/algo_distribue/network/Server.hh
blob: fdfecb463903e55e189303a126dbb4113747829d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Classe Server

#ifndef		SERVER_HH_
# define	SERVER_HH_

#include <list>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <poll.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

#include <assert.h>
#include "data/Data.hh"

extern char	gl_handle;

extern void	signal_alarm(int);

template<class TYPEDATA> 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<int>;
    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<int> 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<int>::const_iterator fd_client_tmp;
	std::list<int>::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<int>& list_fd) // retourne la liste des files descriptors a lire
  {
    struct pollfd poll_fd[_fd_client->size()];
    std::list<int>::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<class TYPEDATA>
  void	send_data(const int& fd, const Data<TYPEDATA>& data)
  {
    data.send(fd);
  }

  template<class TYPEDATA>
  void	send_data(const Data<TYPEDATA>& data,
		  const std::list<int>& fd_client)
  {
    std::list<int>::const_iterator fd = fd_client.begin();
    for (; fd != fd_client.end(); ++fd)
      data.send(*fd);
  }

  template<class TYPEDATA>
  void	received_data(Data<TYPEDATA>& data, const std::list<int>& fd_client)
  {
    std::list<int>::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<int>*	get_list_fd() const { return (_fd_client); } 

  unsigned int	get_nb_client() const { return (_fd_client->size()); }

private:
  std::list<int>		*_fd_client;
  struct sockaddr_in		*_sock_in;
  int				_port;
  unsigned int			_client_max;
  int				_fd;
};

#endif	// SERVER_HH_