summaryrefslogtreecommitdiff
path: root/sock.cpp
blob: 009396f1b004b8d0724740afdfa8074f5b3932da (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// class sock implementation. the multiplex socket implementation has been token from the
// GNU C Library Examples and modified in order to fit in here ( POSIX threads etc. ).

#ifndef SOCK_CXX
#define SOCK_CXX

#include <unistd.h>

#include "sock.h"
#include "CHAT.h"
#include "CONF.h"
#include "MUTX.h"
#include "TOOL.h"

#include "chat.h"
#include "user.h"

using namespace std;


// the posix thread function. this one will called every time a new request socket
// was created succsessfull. in this function a new POSIX thread life begins.
// be carefull because of synchronization issues!!!!
void *sock::posix_thread_func( void *v_pointer )
{
#ifdef VERBOSE_
  pthread_mutex_lock  ( &MUTX::get().mut_stdout );
  cout << "*posix_thread_func( void *v_pointer )" << endl;
  pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

  // recasting the client thread object.
  thrd *t = (thrd*) v_pointer;

  // start parsing the client request and sending response's back.
  t-> run ();
 
  t->~thrd();
}

void *sock::posix_thread_func_( void *v_pointer )
{
#ifdef VERBOSE_
  pthread_mutex_lock  ( &MUTX::get().mut_stdout );
  cout << "*posix_thread_func_( void *v_pointer )" << endl;
  pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 user *p_user = (user*) v_pointer;

 string s_msg( "" );

 int i_sock = p_user->get_sock();
 pthread_mutex_lock  ( &(p_user->mut_message) );

 while( p_user->get_online() )
 {
  pthread_cond_wait( &(p_user->cond_message), &(p_user->mut_message) );
  s_msg = p_user->get_mess( );
  if ( 0 > send( i_sock, s_msg.c_str(), s_msg.size(), 0 ) )
   p_user->set_online( false );
 }
 pthread_mutex_unlock( &(p_user->mut_message) );

 // remove the user from its room.
 string s_user( p_user->get_name() );
 p_user->get_p_room()->del_elem( s_user );

 // post the room that the user has left the chat.
 p_user->get_p_room()->msg_post( new string( p_user->get_name().append( USERLEAV ) ) );  

 p_user->~user();
}

sock::sock()
{
#ifdef VERBOSE
 cout << "sock::sock()" << endl;
#endif

 this->b_run      = true;
 this->i_req      = 0;
 this->req_parser = new reqp();
}

void
#ifdef THRDMOD
sock::chat_stream( int i_sock, map_string &map_params )
#else
sock::chat_stream( int i_sock, map_string &map_params, queue<pthread_t> &thrd_pool )
#endif
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "sock::chat_stream( " << i_sock << ", map_string& )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 user* p_user = CHAT::get().get_user( map_params["nick"] );
 p_user->set_sock( i_sock );

#ifdef THRDMOD
 posix_thread_func_( (void*) p_user );
#else

#ifdef _VERBOSE
 cout << THREAD2 << endl; 
#endif
 posix_thread_func_( (void*) p_user );

 auto int i_fail = pthread_create( &thrd_pool.front(), NULL, posix_thread_func_, (void*) p_user ); 

 // remove this thread from thread pool because its now in use.
 thrd_pool.pop();

 // check if the thread started correctly.
 if ( i_fail )
 {
  cerr << "Thrd: error with return code " << i_fail << endl;
 }	

#endif
}

int
sock::make_socket( uint16_t i_port )
{
#ifdef VERBOSE_
 cout << "sock::make_socket( " << i_port << " )" << endl;
#endif

 int sock;
 struct sockaddr_in name;

 // create the server socket.
 sock = socket (PF_INET, SOCK_STREAM, 0);
 if (sock < 0)
 {
  cerr << "Sock: socket error" << endl;

  if ( ++i_port > MAXPORT )
   exit(-1);

  cerr << SOCKERR << i_port << endl;
  return make_socket( i_port );
 }

 // give the server socket a name.
 name.sin_family = AF_INET;
 name.sin_port = htons (i_port);
 name.sin_addr.s_addr = htonl (INADDR_ANY);

 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
 {
  cerr << "Sock: bind error" << endl;

  if ( ++i_port > MAXPORT )
   exit(-1);

  cout << SOCKERR << i_port << endl;
  return make_socket( i_port );
 }

 return sock;
}

int
sock::read_write( int i_sock )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "sock::read_write( " << i_sock << " )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 char c_req[2048];
 int i_bytes;
 i_bytes = read (i_sock, c_req, 2048);
 
 if (i_bytes < 0)
 {
  cerr << "Sock: read error " << endl;
 }

 else
 {
  // stores the request params.
  map_string map_params; 

  // get the s_rep ( HTML response which will be send imediatly to the client
  // and fill map_params with request values. 
  string s_rep = req_parser->parse( string( c_req ), map_params );

  // send s_rep to the client.
  send( i_sock, s_rep.c_str(), s_rep.size(), 0 );

  // prove if this is a request for a chat stream!
  if ( map_params["event"] == "stream" )
#ifdef THRDMOD
   chat_stream( i_sock, map_params );
#else
   chat_stream( i_sock, map_params, thrd_pool );
#endif

  return 0;
 }
 
 return -1;
}

void
sock::refill_thrd_pool( )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "sock::refill_thrd_pool( )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
#ifdef _VERBOSE
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << THRPOOL << i_thrd_pool_size << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 for (int i=0; i<i_thrd_pool_size; i++ ) 
 {
  pthread_t new_thread;
  thrd_pool.push( new_thread );
 }
}

int
sock::start()
{
#ifdef VERBOSE_
 cout << "sock::start( )" << endl;
#endif

 i_thrd_pool_size = TOOL::string2int( CONF::get().get_val( "THRDPOOL" ) );
 auto int i_port  = TOOL::string2int( CONF::get().get_val( "SRVRPORT" ) );

 int sock;
 fd_set active_fd_set, read_fd_set;
 int i;
 struct sockaddr_in clientname;
 size_t size;

#ifdef _VERBOSE
 cout << SOCKCRT << "localhost:" << i_port << endl;
#endif

 // create the server socket and set it up to accept connections.
 sock = make_socket ( i_port );

 if (listen (sock, 1) < 0)
 {
  cerr << "Sock: listen error" << endl;
  exit( EXIT_FAILURE );
 }

#ifdef _VERBOSE
 cout << SOCKRDY << endl;
#endif

 // initialize the set of active sockets. 
 FD_ZERO (&active_fd_set);
 FD_SET  (sock, &active_fd_set);

 while( b_run )
 {
  // block until input arrives on one or more active sockets.
  read_fd_set = active_fd_set;
  if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
  {
   cerr << "Sock: select error" << endl;
   exit( EXIT_FAILURE );
  }

  // service all the sockets with input pending. 
  for ( i = 0; i < FD_SETSIZE; i++ )
   if ( FD_ISSET (i, &read_fd_set) )
   {
    if ( i == sock )
    {
     // connection request on original socket.
     i_req++;
     int new_sock;
     size = sizeof (clientname);
     new_sock = accept (sock,
                        (struct sockaddr *) &clientname,
                        &size);

     if (new_sock < 0)
     {
      cerr << "Sock: accept error" << endl;
      close ( new_sock );
     }

#ifdef _VERBOSE
     cout << CONNECT << i_req << " "  
          << inet_ntoa( clientname.sin_addr )
          << ":"
          << ntohs    ( clientname.sin_port )
          << endl;
#endif
 
      FD_SET (new_sock, &active_fd_set);
    }

    else
    {
      // create a client thread object. this one will contain all data which is needed by a new posix
      // thread in order to do its tasks. the thr_client pointer will be passed to the posix function
      // there the life of a new thread begins.
      thrd* thr_client = new thrd( i );

      // if the thread pool is empty refill it.
      if ( thrd_pool.empty() )
       refill_thrd_pool( );

#ifndef THRDMOD 
      posix_thread_func( (void*) thr_client ); 

#else
#ifdef _VERBOSE
      cout << THREAD1 << endl; 
#endif

      // creating a new posix thread.
      auto int i_fail = pthread_create( &thrd_pool.front(), NULL, posix_thread_func, (void*) thr_client ); 

      // remove this thread from thread pool because its now in use.
      thrd_pool.pop();
      // check if the thread started correctly.
      if ( i_fail )
      {
        cerr << "Thrd: error with return code " << i_fail << endl;
        close ( i );
      }	

#endif
      FD_CLR( i, &active_fd_set );
    }
   }
 }
}

#endif