33#ifndef _CHOMP_MULTIWORK_MWTASK_H_
34#define _CHOMP_MULTIWORK_MWTASK_H_
110 void Port (
int number);
132 int LogFile (
const char *filename);
147 int Add (
const char *name,
int port = -1);
151 int Load (
const char *filename);
190 unsigned int code,
const mwData &x);
195 static int RecvMessage (
int fd,
unsigned int &ctrl,
196 unsigned int &code,
mwData &x);
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" <<
259 if (!filename || !*filename)
268 logFile -> open (filename, std::ios::app);
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;
317 portnum =
static_cast<short int> (number);
361 while (pos && (name [pos] !=
':') &&
362 (name [pos] >=
'0') && (name [pos] <=
'9'))
368 if (pos && (name [pos] ==
':') && name [pos + 1])
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);
378 computers. push_back (std::string (compname));
379 ports. push_back (port);
389 computers. push_back (std::string (name));
390 ports. push_back (port);
397 std::ifstream f (filename);
406 f. getline (buf, 512,
'\n');
407 if ((*buf ==
';') || (*buf ==
'#') || (*buf ==
'/'))
428 unsigned int code,
const mwData &x)
431 sending << ctrl << code << x. Length () << x;
432 return mwSendBytes (fd, sending. Buffer (), sending. Length ());
438 unsigned int &code,
mwData &x)
445 unsigned char *buf0 =
reinterpret_cast<unsigned char *
> (buf00);
448 int len = (int) (buf0 [8]) << 24;
449 len |= (int) (buf0 [9]) << 16;
450 len |= (int) (buf0 [10]) << 8;
451 len |= (int) (buf0 [11]);
456 ctrl = (int) (buf0 [0]) << 24;
457 ctrl |= (int) (buf0 [1]) << 16;
458 ctrl |= (int) (buf0 [2]) << 8;
459 ctrl |= (int) (buf0 [3]);
462 code = (int) (buf0 [4]) << 24;
463 code |= (int) (buf0 [5]) << 16;
464 code |= (int) (buf0 [6]) << 8;
465 code |= (int) (buf0 [7]);
475 char *buf =
new char [len];
496 *
logFile <<
"Turning workers off..." << std::endl;
500 for (
unsigned int n = 0; n <
computers. size (); ++ n)
503 const char *name =
computers [n]. c_str ();
504 int port =
ports [n];
518 *
logFile <<
"Worker " << name <<
":" <<
519 port <<
" could not be contacted." <<
539 *
logFile <<
"Worker " << name <<
":" <<
540 port <<
" exited successfully." <<
546 *
logFile <<
"Error while sending the disconnect "
547 "message to " << name <<
":" << port <<
554 *
logFile << counter <<
" worker(s) have been shut down." <<
This class is used to convert data structures into a single sequence of bytes and to retrieve this da...
This class defines a generic task object (coordinator or worker) for the multi-work distributed compu...
int Load(const char *filename)
Loads computer addresses from the given file.
static int SendMessage(int fd, unsigned int ctrl, unsigned int code, const mwData &x)
Sends a message with data to the given socket.
int Port() const
Returns the current port number.
std::ofstream * logFile
The debug log file stream.
int TimeOut() const
Returns the currently set network connection time-out interval.
int QuitWorkers()
Quits all the workers whose addresses were added with the 'Add' and 'Load' functions.
void LogClose()
Closes the log file and adds a line with the time information unless this log file was borrowed from ...
unsigned int ctrlnum
The control number that is used to recognize a compatible worker or a compatible coordinator.
mwTask()
The default constructor.
unsigned int ControlNumber() const
Returns the currently set identification control number.
static int RecvMessage(int fd, unsigned int &ctrl, unsigned int &code, mwData &x)
Receives a message with data from the given socket.
virtual ~mwTask()
The destructor.
bool logBorrowed
Is this log file pointer borrowed from another task?
int timeout
The network communication time-out in seconds.
mwTask & operator=(const mwTask &)
The assignment operator is forbidden.
int LogFile(const char *filename)
Begins logging detailed communication debug information to the given file.
std::vector< std::string > computers
A list of workers or coordinators to connect to at start-up.
std::vector< int > ports
A list of port numbers of workers to connect to at start-up.
mwTask(const mwTask &)
The copy constructor is forbidden.
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.
int portnum
The network communication port number.
#define mwPORT
The default port number used for the communication between the coordinator and workers.
#define mwTIMEOUT
The default time-out in seconds (8 hours).
#define mwCTRLNUM
The default control number used for data identification.
This file contains the basic configuration of the MultiWork module, mainly as a definition of a serie...
This file contains the definition of the MultiWork data class.
This file contains the definition of some low-level functions for the TCP/IP streams communication an...
int mwSendBytes(int fd, const char *buf, int len)
Sends the given buffer to the given socket.
@ mwError
A serious error occurred.
@ mwOk
Everything is fine.
void mwDisconnect(int fd)
Disconnects the given socket.
int mwRecvBytes(int fd, char *buf, int len)
Receives the given amount of data from the given socket.
int mwConnect(const char *name, int port)
Connects to the given computer at the given port.
mwCodes
Various message codes used for the communication between the coordinator and workers.
@ mwKeepMsg
Message to the Worker: Keep running after having disconnected.
@ mwStdMsg
Message to the Worker: A standard piece of data to be processed.
@ mwInitMsg
Message to the Worker: A piece of initialization data to be processed.
@ mwRejectedMsg
Message to the Coordinator: The data has been rejected.
@ mwByeMsg
Message to the Worker: Please, disconnect.
@ mwPortMsg
Message to the Coordinator: I will listen at this port number.
@ mwNoMsg
Message: No special code information included.
@ mwDontKeepMsg
Message to the Worker: Don't keep running after disconnecting.
This namespace contains the entire CHomP library interface.