The Original CHomP Software
Public Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
chomp::multiwork::mwTask Class Reference

This class defines a generic task object (coordinator or worker) for the multi-work distributed computations framework. More...

#include <mwtask.h>

Inheritance diagram for chomp::multiwork::mwTask:
chomp::multiwork::mwCoordinator chomp::multiwork::mwWorker chomp::multiwork::mwSubCoordinator< dim, coord > chomp::multiwork::mwSubWorker< dim, coord >

Public Member Functions

 mwTask ()
 The default constructor. More...
 
virtual ~mwTask ()
 The destructor. More...
 
void Port (int number)
 Sets the port number for the communication or 0 to use none. More...
 
int Port () const
 Returns the current port number. More...
 
void ControlNumber (unsigned int number)
 Sets the control number for identification. More...
 
unsigned int ControlNumber () const
 Returns the currently set identification control number. More...
 
void TimeOut (int seconds)
 Sets the network connection time-out interval in seconds. More...
 
int TimeOut () const
 Returns the currently set network connection time-out interval. More...
 
int LogFile (const char *filename)
 Begins logging detailed communication debug information to the given file. More...
 
void LogFile (const mwTask &other)
 Uses another task's log file to log this task's information. More...
 
void LogClose ()
 Closes the log file and adds a line with the time information unless this log file was borrowed from another task. More...
 
int Add (const char *name, int port=-1)
 Adds an address to the list of computers to connect to at the beginning of working or coordinating. More...
 
int Load (const char *filename)
 Loads computer addresses from the given file. More...
 
int QuitWorkers ()
 Quits all the workers whose addresses were added with the 'Add' and 'Load' functions. More...
 

Static Protected Member Functions

static int SendMessage (int fd, unsigned int ctrl, unsigned int code, const mwData &x)
 Sends a message with data to the given socket. More...
 
static int RecvMessage (int fd, unsigned int &ctrl, unsigned int &code, mwData &x)
 Receives a message with data from the given socket. More...
 

Protected Attributes

std::ofstream * logFile
 The debug log file stream. More...
 
std::vector< std::string > computers
 A list of workers or coordinators to connect to at start-up. More...
 
std::vector< int > ports
 A list of port numbers of workers to connect to at start-up. More...
 

Private Member Functions

 mwTask (const mwTask &)
 The copy constructor is forbidden. More...
 
mwTaskoperator= (const mwTask &)
 The assignment operator is forbidden. More...
 

Private Attributes

int portnum
 The network communication port number. More...
 
unsigned int ctrlnum
 The control number that is used to recognize a compatible worker or a compatible coordinator. More...
 
int timeout
 The network communication time-out in seconds. More...
 
bool logBorrowed
 Is this log file pointer borrowed from another task? More...
 

Detailed Description

This class defines a generic task object (coordinator or worker) for the multi-work distributed computations framework.

It is the common part of a worker task and a coordinator task, and contains some general settings that apply to both. Note that this class is inherited by the worker and the coordinator class in the virtual mode, which allows one to create a class that is both a worker and a coordinator at the same time (although this is not recommended in general).

Definition at line 98 of file mwtask.h.

Constructor & Destructor Documentation

◆ mwTask() [1/2]

chomp::multiwork::mwTask::mwTask ( )
inline

The default constructor.

Definition at line 219 of file mwtask.h.

219 :
220 portnum (mwPORT),
223 logFile (0),
224 logBorrowed (false)
225{
226 return;
227} /* mwTask::mwTask */
std::ofstream * logFile
The debug log file stream.
Definition: mwtask.h:177
unsigned int ctrlnum
The control number that is used to recognize a compatible worker or a compatible coordinator.
Definition: mwtask.h:168
bool logBorrowed
Is this log file pointer borrowed from another task?
Definition: mwtask.h:181
int timeout
The network communication time-out in seconds.
Definition: mwtask.h:171
int portnum
The network communication port number.
Definition: mwtask.h:164
#define mwPORT
The default port number used for the communication between the coordinator and workers.
Definition: mwconfig.h:120
#define mwTIMEOUT
The default time-out in seconds (8 hours).
Definition: mwconfig.h:130
#define mwCTRLNUM
The default control number used for data identification.
Definition: mwconfig.h:125

◆ ~mwTask()

chomp::multiwork::mwTask::~mwTask ( )
inlinevirtual

The destructor.

Definition at line 304 of file mwtask.h.

305{
306 // close the log file if it was in use
307 LogClose ();
308
309 return;
310} /* mwTask::~mwTask */
void LogClose()
Closes the log file and adds a line with the time information unless this log file was borrowed from ...
Definition: mwtask.h:231

References LogClose().

◆ mwTask() [2/2]

chomp::multiwork::mwTask::mwTask ( const mwTask )
inlineprivate

The copy constructor is forbidden.

Definition at line 210 of file mwtask.h.

210{}

Member Function Documentation

◆ Add()

int chomp::multiwork::mwTask::Add ( const char *  name,
int  port = -1 
)
inline

Adds an address to the list of computers to connect to at the beginning of working or coordinating.

The addresses must be in the form "computer.domain:port". If port is not defined then the default port number is used.

Definition at line 350 of file mwtask.h.

351{
352 // if the name is empty, ignore it
353 if (!name || !*name)
354 return mwError;
355
356 // determine whether the name contains a colon and port number
357 int pos = 1;
358 while (name [pos])
359 ++ pos;
360 -- pos;
361 while (pos && (name [pos] != ':') &&
362 (name [pos] >= '0') && (name [pos] <= '9'))
363 {
364 -- pos;
365 }
366
367 // if the name contains colon and some digits after the colon...
368 if (pos && (name [pos] == ':') && name [pos + 1])
369 {
370 // append the computer name and the chosen port number
371 char *compname = new char [pos + 1];
372 for (int i = 0; i < pos; ++ i)
373 compname [i] = name [i];
374 compname [pos] = '\0';
375 port = std::atoi (name + pos + 1);
376 if (port <= 0)
377 return mwError;
378 computers. push_back (std::string (compname));
379 ports. push_back (port);
380 delete [] compname;
381 }
382 else
383 {
384 // if the port number is not reasonable, use the default one
385 if (port <= 0)
386 port = portnum;
387 if (port <= 0)
388 return mwError;
389 computers. push_back (std::string (name));
390 ports. push_back (port);
391 }
392 return mwOk;
393} /* mwTask::Add */
std::vector< std::string > computers
A list of workers or coordinators to connect to at start-up.
Definition: mwtask.h:201
std::vector< int > ports
A list of port numbers of workers to connect to at start-up.
Definition: mwtask.h:204
@ mwError
A serious error occurred.
Definition: mwconfig.h:162
@ mwOk
Everything is fine.
Definition: mwconfig.h:159

References computers, chomp::multiwork::mwError, chomp::multiwork::mwOk, portnum, and ports.

Referenced by Load().

◆ ControlNumber() [1/2]

unsigned int chomp::multiwork::mwTask::ControlNumber ( ) const
inline

Returns the currently set identification control number.

Definition at line 332 of file mwtask.h.

333{
334 return ctrlnum;
335} /* mwTask::ControlNumber */

References ctrlnum.

Referenced by chomp::multiwork::mwCoordinator::RecvMessageC(), chomp::multiwork::mwWorker::RecvMessageW(), chomp::multiwork::mwCoordinator::SendMessageC(), and chomp::multiwork::mwWorker::SendMessageW().

◆ ControlNumber() [2/2]

void chomp::multiwork::mwTask::ControlNumber ( unsigned int  number)
inline

Sets the control number for identification.

Definition at line 326 of file mwtask.h.

327{
328 ctrlnum = number;
329 return;
330} /* mwTask::ControlNumber */

References ctrlnum.

◆ Load()

int chomp::multiwork::mwTask::Load ( const char *  filename)
inline

Loads computer addresses from the given file.

Returns the number of acquired addresses or mwError.

Definition at line 395 of file mwtask.h.

396{
397 std::ifstream f (filename);
398 if (!f)
399 return mwError;
400
401 char buf [512];
402 int counter = 0;
403 while (1)
404 {
405 *buf = '\0';
406 f. getline (buf, 512, '\n');
407 if ((*buf == ';') || (*buf == '#') || (*buf == '/'))
408 continue;
409 if (*buf)
410 {
411 int result = this -> Add (buf, portnum);
412 if (result == mwOk)
413 ++ counter;
414 }
415 if (!f)
416 return counter;
417 }
418} /* mwTask::Load */
int Add(const char *name, int port=-1)
Adds an address to the list of computers to connect to at the beginning of working or coordinating.
Definition: mwtask.h:350

References Add(), chomp::multiwork::mwError, chomp::multiwork::mwOk, and portnum.

◆ LogClose()

void chomp::multiwork::mwTask::LogClose ( )
inline

Closes the log file and adds a line with the time information unless this log file was borrowed from another task.

Definition at line 231 of file mwtask.h.

232{
233 if (!logFile)
234 return;
235 if (logBorrowed)
236 {
237 logFile = 0;
238 logBorrowed = false;
239 return;
240 }
241 std::time_t stop_time;
242 std::time (&stop_time);
243 *logFile << "\nMultiWork log file closed on " <<
244 std::asctime (std::localtime (&stop_time)) << "\n"
245 "-----------------------------------------------------\n" <<
246 std::endl;
247 delete logFile;
248 logFile = 0;
249 return;
250} /* mwTask::LogClose */

References logBorrowed, and logFile.

Referenced by LogFile(), and ~mwTask().

◆ LogFile() [1/2]

int chomp::multiwork::mwTask::LogFile ( const char *  filename)
inline

Begins logging detailed communication debug information to the given file.

Returns mwOk on success, mwError if cannot open/create the file.

Definition at line 252 of file mwtask.h.

253{
254 // close the current log if in use
255 if (logFile)
256 LogClose ();
257
258 // if no file name supplied, return now
259 if (!filename || !*filename)
260 return mwOk;
261
262 // create a file stream variable
263 logFile = new std::ofstream;
264 if (!logFile)
265 return mwError;
266
267 // open the log file for appending
268 logFile -> open (filename, std::ios::app);
269
270 // write the current time to the log
271 std::time_t start_time;
272 std::time (&start_time);
273 *logFile << "MultiWork log file opened on " <<
274 std::asctime (std::localtime (&start_time)) << std::endl;
275
276 // if unable to open the file or an error occurred, return mwError
277 if (!*logFile)
278 {
279 delete logFile;
280 return mwError;
281 }
282
283 return mwOk;
284} /* mwTask::LogFile */

References LogClose(), logFile, chomp::multiwork::mwError, and chomp::multiwork::mwOk.

◆ LogFile() [2/2]

void chomp::multiwork::mwTask::LogFile ( const mwTask other)
inline

Uses another task's log file to log this task's information.

Definition at line 286 of file mwtask.h.

287{
288 // close the current log if in use
289 LogClose ();
290
291 // if there is no other log file, do nothing
292 if (!other. logFile)
293 return;
294
295 // borrow the log file pointer
296 logBorrowed = true;
297 logFile = other. logFile;
298
299 return;
300} /* mwTask::LogFile */

References logBorrowed, LogClose(), and logFile.

◆ operator=()

mwTask & chomp::multiwork::mwTask::operator= ( const mwTask )
inlineprivate

The assignment operator is forbidden.

Definition at line 213 of file mwtask.h.

213{return *this;}

◆ Port() [1/2]

int chomp::multiwork::mwTask::Port ( ) const
inline

Returns the current port number.

Definition at line 321 of file mwtask.h.

322{
323 return portnum;
324} /* mwTask::Port */

References portnum.

Referenced by chomp::multiwork::mwCoordinator::BeginListening(), chomp::multiwork::mwWorker::Work(), and chomp::multiwork::mwWorker::WorkOne().

◆ Port() [2/2]

void chomp::multiwork::mwTask::Port ( int  number)
inline

Sets the port number for the communication or 0 to use none.

Definition at line 314 of file mwtask.h.

315{
316 if (number >= 0)
317 portnum = static_cast<short int> (number);
318 return;
319} /* mwTask::Port */

References portnum.

◆ QuitWorkers()

int chomp::multiwork::mwTask::QuitWorkers ( )
inline

Quits all the workers whose addresses were added with the 'Add' and 'Load' functions.

Returns mwOk or mwError.

Definition at line 492 of file mwtask.h.

493{
494 // write to the log file what you are doing
495 if (logFile)
496 *logFile << "Turning workers off..." << std::endl;
497
498 // try connecting to each worker and ask them to exit
499 int counter = 0;
500 for (unsigned int n = 0; n < computers. size (); ++ n)
501 {
502 // retrieve the computer name and port from the list
503 const char *name = computers [n]. c_str ();
504 int port = ports [n];
505
506 // if no valid name or port number read, skip this item
507 if (!*name || !port)
508 continue;
509
510 // try connecting to the computer
511 int fd = mwConnect (name, port);
512
513 // if unsuccessful, make a note and take another one
514 if (fd < 0)
515 {
516 if (logFile)
517 {
518 *logFile << "Worker " << name << ":" <<
519 port << " could not be contacted." <<
520 std::endl;
521 }
522 continue;
523 }
524
525 // prepare the control code to send to the worker
526 unsigned int code = mwByeMsg | mwDontKeepMsg;
527
528 // send the 'Bye!' message to the worker and disconnect it
529 mwData empty;
530 int result = this -> SendMessage (fd, ctrlnum, code, empty);
531 mwDisconnect (fd);
532
533 // add an appropriate message to the log file
534 if (result == mwOk)
535 {
536 ++ counter;
537 if (logFile)
538 {
539 *logFile << "Worker " << name << ":" <<
540 port << " exited successfully." <<
541 std::endl;
542 }
543 }
544 else if (logFile)
545 {
546 *logFile << "Error while sending the disconnect "
547 "message to " << name << ":" << port <<
548 "." << std::endl;
549 }
550 }
551
552 // write to the log file how many workers were turned off
553 if (logFile)
554 *logFile << counter << " worker(s) have been shut down." <<
555 std::endl;
556
557 return mwOk;
558} /* mwTask::QuitWorkers */
static int SendMessage(int fd, unsigned int ctrl, unsigned int code, const mwData &x)
Sends a message with data to the given socket.
Definition: mwtask.h:427
void mwDisconnect(int fd)
Disconnects the given socket.
int mwConnect(const char *name, int port)
Connects to the given computer at the given port.
@ mwByeMsg
Message to the Worker: Please, disconnect.
Definition: mwtask.h:75
@ mwDontKeepMsg
Message to the Worker: Don't keep running after disconnecting.
Definition: mwtask.h:72

References computers, ctrlnum, logFile, chomp::multiwork::mwByeMsg, chomp::multiwork::mwConnect(), chomp::multiwork::mwDisconnect(), chomp::multiwork::mwDontKeepMsg, chomp::multiwork::mwOk, ports, and SendMessage().

◆ RecvMessage()

int chomp::multiwork::mwTask::RecvMessage ( int  fd,
unsigned int &  ctrl,
unsigned int &  code,
mwData x 
)
inlinestaticprotected

Receives a message with data from the given socket.

Receives a message from the given socket.

Verifies the received control code if it is correct. Returns mwOk on success or mwError in the case of failure.

Returns mwOk, mwError or mwLost.

Definition at line 437 of file mwtask.h.

439{
440 // read the code and length of the message
441 char buf00 [12];
442 int result = mwRecvBytes (fd, buf00, 12);
443 if (result < 0)
444 return result;
445 unsigned char *buf0 = reinterpret_cast<unsigned char *> (buf00);
446
447 // extract the length of the message
448 int len = (int) (buf0 [8]) << 24;
449 len |= (int) (buf0 [9]) << 16;
450 len |= (int) (buf0 [10]) << 8;
451 len |= (int) (buf0 [11]);
452 if (len < 0)
453 return mwError;
454
455 // extract the control code of the message
456 ctrl = (int) (buf0 [0]) << 24;
457 ctrl |= (int) (buf0 [1]) << 16;
458 ctrl |= (int) (buf0 [2]) << 8;
459 ctrl |= (int) (buf0 [3]);
460
461 // extract the code of the message
462 code = (int) (buf0 [4]) << 24;
463 code |= (int) (buf0 [5]) << 16;
464 code |= (int) (buf0 [6]) << 8;
465 code |= (int) (buf0 [7]);
466
467 // if the message length is zero, no more reading is necessary
468 if (!len)
469 {
470 x. Reset ();
471 return mwOk;
472 }
473
474 // prepare a memory buffer for the message
475 char *buf = new char [len];
476 if (!buf)
477 return mwError;
478
479 // read the message
480 result = mwRecvBytes (fd, buf, len);
481 if (result < 0)
482 {
483 delete [] buf;
484 return result;
485 }
486
487 // transform the message to mw data
488 x. Take (buf, len);
489 return mwOk;
490} /* RecvMessage */
int mwRecvBytes(int fd, char *buf, int len)
Receives the given amount of data from the given socket.

References chomp::multiwork::mwError, chomp::multiwork::mwOk, and chomp::multiwork::mwRecvBytes().

Referenced by chomp::multiwork::mwCoordinator::RecvMessageC(), and chomp::multiwork::mwWorker::RecvMessageW().

◆ SendMessage()

int chomp::multiwork::mwTask::SendMessage ( int  fd,
unsigned int  ctrl,
unsigned int  code,
const mwData x 
)
inlinestaticprotected

Sends a message with data to the given socket.

Sends a message to the given socket.

The message includes a control number. Returns mwOk on success and mwError in the case of failure.

Returns mwOk, mwError or mwLost.

Definition at line 427 of file mwtask.h.

429{
430 mwData sending;
431 sending << ctrl << code << x. Length () << x;
432 return mwSendBytes (fd, sending. Buffer (), sending. Length ());
433} /* SendMessage */
int mwSendBytes(int fd, const char *buf, int len)
Sends the given buffer to the given socket.

References chomp::multiwork::mwSendBytes().

Referenced by QuitWorkers(), chomp::multiwork::mwCoordinator::SendMessageC(), and chomp::multiwork::mwWorker::SendMessageW().

◆ TimeOut() [1/2]

int chomp::multiwork::mwTask::TimeOut ( ) const
inline

Returns the currently set network connection time-out interval.

Definition at line 343 of file mwtask.h.

344{
345 return timeout;
346} /* mwTask::TimeOut */

References timeout.

Referenced by chomp::multiwork::mwCoordinator::RunLoop(), chomp::multiwork::mwWorker::Work(), and chomp::multiwork::mwWorker::WorkOne().

◆ TimeOut() [2/2]

void chomp::multiwork::mwTask::TimeOut ( int  seconds)
inline

Sets the network connection time-out interval in seconds.

Definition at line 337 of file mwtask.h.

338{
339 timeout = seconds;
340 return;
341} /* mwTask::TimeOut */

References timeout.

Member Data Documentation

◆ computers

std::vector<std::string> chomp::multiwork::mwTask::computers
protected

A list of workers or coordinators to connect to at start-up.

Definition at line 201 of file mwtask.h.

Referenced by Add(), chomp::multiwork::mwCoordinator::ConnectWorkers(), QuitWorkers(), and chomp::multiwork::mwWorker::Work().

◆ ctrlnum

unsigned int chomp::multiwork::mwTask::ctrlnum
private

The control number that is used to recognize a compatible worker or a compatible coordinator.

Note: The worker sends its negation.

Definition at line 168 of file mwtask.h.

Referenced by ControlNumber(), and QuitWorkers().

◆ logBorrowed

bool chomp::multiwork::mwTask::logBorrowed
private

Is this log file pointer borrowed from another task?

Definition at line 181 of file mwtask.h.

Referenced by LogClose(), and LogFile().

◆ logFile

std::ofstream* chomp::multiwork::mwTask::logFile
protected

◆ portnum

int chomp::multiwork::mwTask::portnum
private

The network communication port number.

Definition at line 164 of file mwtask.h.

Referenced by Add(), Load(), and Port().

◆ ports

std::vector<int> chomp::multiwork::mwTask::ports
protected

A list of port numbers of workers to connect to at start-up.

Definition at line 204 of file mwtask.h.

Referenced by Add(), chomp::multiwork::mwCoordinator::ConnectWorkers(), QuitWorkers(), and chomp::multiwork::mwWorker::Work().

◆ timeout

int chomp::multiwork::mwTask::timeout
private

The network communication time-out in seconds.

Definition at line 171 of file mwtask.h.

Referenced by TimeOut(), and chomp::multiwork::mwWorker::Work().


The documentation for this class was generated from the following file: