The worker thread. More...
#include <worker.h>
Classes | |
struct | fileloc |
Worker helper class for keeping track of file locations. More... | |
Public Member Functions | |
Worker (void *lp) | |
~Worker () | |
Worker destructor. | |
void | comm () |
the communication control protocol. | |
Private Member Functions | |
int | parseARGS (char **args, char *line) |
Helper to parse socket message. Helper parses the socket message and splits it using protocol delimiter. The parsed message is contained within the receive buffer. | |
bool | auth () |
The Server authentication protocol. Simple two way authentication mechanism. We receive a authentication string from the client and reply by sending a string. The client must accept the following protocol: 1. Recv "AUTH:std::string\r\n" : This tells us that the client are attempting to authenticate itself. 2.a Send "AUTH:std::string\r\n" : We reply that the authentication was a success and authenticate ourself. 2.b Authentication failure. Send "CLOSE\r\n" : Authentication failure. Server announces this to client and closes. | |
bool | intent () |
Handling of intents created by clients Client publish its intent. The server has only support for one intent: Extract. The server must accept the following protocol: 1. Recv "EXTRACT:std::string::std::string\r\n" : This tells the server that we are want to extract features. 2. Send "OK" : The server confirms the intent. | |
bool | receivefile () |
The receive file protocol. | |
bool | sendfile () |
The send file protocol. | |
void | message (const char *msg) |
Echos a message to stdout. | |
bool | receiveMessage () |
Receive message helper. Helper method for receiving a message over the socket. | |
bool | sendMessage (string msg) |
Send message helper. Helper method for sending the message over the socket. | |
void | killConnection () |
The close socket protocol. Closes the open socket connection. Server closing protocol: 1. Send "CLOSING\r\n" : Server announce that it is closing its socket. 2. Close. | |
string | randomFilename (string suffix) |
Generates a unique random file name. Helper method that uses built in function tmpnam to generate unique temporary filenames. | |
Private Attributes | |
int * | csock |
string | authMessage |
string | authReply |
string | featureType |
string | descriptorType |
struct Worker::fileloc | recvFile |
Worker helper class for keeping track of file locations. | |
struct Worker::fileloc | sendFile |
int | snt |
int | rcv |
char * | header [4096] |
char | recordBUFF [4096] |
stringstream | ss |
The worker thread.
This is the socket worker thread. The class implements the socket communication protocols and instantiates cvUtils.h as needed.
The overall communication protocol follows this pattern:
1. Client sends authentication message.
2a. Server replies with an authentication message.
2b. Server closes connection if authentication fails.
3a. Client sends intent.
3b. Client closes connection if server authentication message fails.
3c. Client closes connection if server closes connection.
4. Server responds to intent.
5. If the server understands intent the client announces a file send operation.
6. Server replies with OK if it understands intent.
7. If file send OK from server; client sends file and waits for reply.
8. Server performs operations based on the intent(3a) from the client and announces that it wishes to send a file when it is finished.
9. Client responds with OK if it understands the intent.
10. Server sends file and closes its connection.
11. The client closes its connection.
See the specific methods for protocol details.
Definition at line 54 of file worker.h.
Worker::Worker | ( | void * | lp | ) |
Definition at line 22 of file worker.cpp.
References authMessage, authReply, csock, message(), and recordBUFF.
Worker::~Worker | ( | ) |
Worker destructor.
Removes temporary files and deletes the socket.
Definition at line 39 of file worker.cpp.
References csock, Worker::fileloc::getFullName(), recvFile, and sendFile.
bool Worker::auth | ( | ) | [private] |
The Server authentication protocol. Simple two way authentication mechanism. We receive a authentication string from the client and reply by sending a string.
The client must accept the following protocol:
1. Recv "AUTH:std::string\r\n" : This tells us that the client are attempting to authenticate itself.
2.a Send "AUTH:std::string\r\n" : We reply that the authentication was a success and authenticate ourself.
2.b Authentication failure. Send "CLOSE\r\n" : Authentication failure. Server announces this to client and closes.
Definition at line 163 of file worker.cpp.
References headers::authenticate, authMessage, authReply, headers::delim, headers::end, header, message(), receiveMessage(), and sendMessage().
Referenced by comm().
void Worker::comm | ( | ) |
the communication control protocol.
This method implements the complete communication control protocol.
The overall communication protocol follows this pattern:
1. The client authenticates.
2. Client sends intent.
3. Client sends file and waits for reply.
4. Server performs operations based on the intent(2) from the client.
5. Server sends file and closes its connection
6. The client closes its connection.
See auth(), intent(), receivefile(), sendFile() and killConnection() for protocol details.
Definition at line 82 of file worker.cpp.
References auth(), descriptorType, featureType, Worker::fileloc::getFullName(), Worker::fileloc::initFromPath(), intent(), killConnection(), message(), receivefile(), recvFile, sendfile(), and sendFile.
Referenced by Server::newJob().
bool Worker::intent | ( | ) | [private] |
Handling of intents created by clients Client publish its intent. The server has only support for one intent: Extract.
The server must accept the following protocol:
1. Recv "EXTRACT:std::string::std::string\r\n" : This tells the server that we are want to extract features.
2. Send "OK" : The server confirms the intent.
Definition at line 125 of file worker.cpp.
References descriptorType, headers::end, headers::extract, featureType, header, message(), headers::ok, receiveMessage(), and sendMessage().
Referenced by comm().
void Worker::killConnection | ( | ) | [private] |
The close socket protocol. Closes the open socket connection.
Server closing protocol:
1. Send "CLOSING\r\n" : Server announce that it is closing its socket.
2. Close.
Definition at line 363 of file worker.cpp.
References headers::close, csock, and message().
Referenced by comm().
void Worker::message | ( | const char * | msg | ) | [private] |
Echos a message to stdout.
msg | The message to display. |
Definition at line 432 of file worker.cpp.
Referenced by auth(), comm(), intent(), killConnection(), receivefile(), sendfile(), and Worker().
int Worker::parseARGS | ( | char ** | args, | |
char * | line | |||
) | [private] |
Helper to parse socket message. Helper parses the socket message and splits it using protocol delimiter. The parsed message is contained within the receive buffer.
args | The message receive buffer | |
line | The message helper buffer telling parseArgs how long the message in args is. |
Definition at line 55 of file worker.cpp.
References headers::delim, and headers::end.
Referenced by receiveMessage().
string Worker::randomFilename | ( | string | suffix | ) | [private] |
Generates a unique random file name. Helper method that uses built in function tmpnam to generate unique temporary filenames.
suffix | The file type of the random file. |
Definition at line 445 of file worker.cpp.
Referenced by receivefile().
bool Worker::receivefile | ( | ) | [private] |
The receive file protocol.
Accepts bytes into a buffer from the socket and writes piece by piece to a loaded file until complete. This process consists of handshaking with the client connected to the socket.
The client must accept the following protocol:
1. Recv "FC:std::string:unsigned long\r\n" : This tells us that the client are attempting a file copy. The client sends the name of the file and the size of the file in bytes.
2. Send "OK\r\n" : We understand that the client tries to send a file.
3. Recv bytes : Now we enter a loop receiving bytes until file size is received.
Definition at line 210 of file worker.cpp.
References csock, headers::end, headers::filecopy, Worker::fileloc::getFullName(), header, Worker::fileloc::initFromPath(), message(), headers::ok, randomFilename(), rcv, receiveMessage(), recordBUFF, recvFile, sendMessage(), and ss.
Referenced by comm().
bool Worker::receiveMessage | ( | ) | [private] |
Receive message helper. Helper method for receiving a message over the socket.
Definition at line 405 of file worker.cpp.
References headers::close, csock, header, parseARGS(), rcv, and recordBUFF.
Referenced by auth(), intent(), receivefile(), and sendfile().
bool Worker::sendfile | ( | ) | [private] |
The send file protocol.
Loads a file into a buffer and sends piece by piece via a socket object until complete. This process consists of handshaking with the connected client via the socket.
The client must accept the following protocol:
1. Send "FC:std::string:unsigned long\r\n" : This tells the client that we are attemping a filecopy. The server sends the name of the file and the size of the file in bytes.
2. Recv "OK\r\n" : If we receive this from the client we know to proceed.
3. Send bytes : Now we enter a loop sending bytes until filesize is sent.
Definition at line 291 of file worker.cpp.
References csock, headers::delim, headers::end, headers::filecopy, Worker::fileloc::getFullName(), header, message(), Worker::fileloc::name, headers::ok, rcv, receiveMessage(), recordBUFF, sendFile, sendMessage(), snt, and ss.
Referenced by comm().
bool Worker::sendMessage | ( | string | msg | ) | [private] |
Send message helper. Helper method for sending the message over the socket.
msg | The message to send. |
Definition at line 379 of file worker.cpp.
References csock, recordBUFF, and snt.
Referenced by auth(), intent(), receivefile(), and sendfile().
string Worker::authMessage [private] |
string Worker::authReply [private] |
int* Worker::csock [private] |
Definition at line 61 of file worker.h.
Referenced by killConnection(), receivefile(), receiveMessage(), sendfile(), sendMessage(), Worker(), and ~Worker().
string Worker::descriptorType [private] |
string Worker::featureType [private] |
char* Worker::header[4096] [private] |
Definition at line 95 of file worker.h.
Referenced by auth(), intent(), receivefile(), receiveMessage(), and sendfile().
int Worker::rcv [private] |
Definition at line 93 of file worker.h.
Referenced by receivefile(), receiveMessage(), and sendfile().
char Worker::recordBUFF[4096] [private] |
Definition at line 96 of file worker.h.
Referenced by receivefile(), receiveMessage(), sendfile(), sendMessage(), and Worker().
struct Worker::fileloc Worker::recvFile [private] |
Worker helper class for keeping track of file locations.
This class helps the worker thread keep track of the files created (randomly created filenames) when files are copied over the socket and new files returned from the CvUtils class.
Referenced by comm(), receivefile(), and ~Worker().
struct Worker::fileloc Worker::sendFile [private] |
Referenced by comm(), sendfile(), and ~Worker().
int Worker::snt [private] |
Definition at line 92 of file worker.h.
Referenced by sendfile(), and sendMessage().
stringstream Worker::ss [private] |
Definition at line 98 of file worker.h.
Referenced by receivefile(), and sendfile().