-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadHandler.cpp
More file actions
85 lines (67 loc) · 2.38 KB
/
ReadHandler.cpp
File metadata and controls
85 lines (67 loc) · 2.38 KB
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
/*
* File: ReadHandler.cpp
* Author: kmcqueen
*
* Created on September 13, 2013, 1:59 AM
*/
#include "ReadHandler.h"
#include <iostream>
#include <stdlib.h>
#include "GetMessageRequest.h"
#include "main.h"
ReadHandler* ReadHandler::instance() {
static ReadHandler* instance = new ReadHandler();
return instance;
}
ReadHandler::~ReadHandler() {
}
Request* ReadHandler::prepareRequest(string commandLine) {
string prefix = this->getCommandPrefix();
// extract the recipient (which is between the prefix and the space)
int space = commandLine.find(" ", prefix.size());
if (space == string::npos) {
this->printUnexpectedCommand(commandLine);
return NULL;
}
string recipient = commandLine.substr(prefix.size(), space - prefix.size());
debug("\trecipient is: " + recipient);
// extract the number (which is everything after the space)
string number = commandLine.substr(space + 1);
debug("\tnumber is: " + number);
return new GetMessageRequest(recipient, number);
}
void ReadHandler::doHandleResponse(string response) {
string _message_ = "message ";
int _message_len_ = _message_.length();
// make sure that the response begins with "message "
if (response.find(_message_) != 0) {
debug("ReadHandler::doHandleResponse -- Unexpected response: " + response);
return;
}
// find the space between [subject] and [length]
int space = response.find(" ", _message_len_);
if (space == string::npos) {
debug("ReadHandler::doHandleResponse -- Unexpected response: " + response);
return;
}
// extract the subject
string subject = response.substr(_message_len_, space - _message_len_);
cout << subject << endl;
// parse out the number of messages from the response
int msgLen = atoi(response.substr(space + 1).c_str());
// get the server proxy
ServerProxy* server = ServerProxy::instance();
// get the message body from the server
string body = server->getResponseString(msgLen);
// output the message body
cout << body << endl;
}
string ReadHandler::getHelpString() {
return "read [recipient] [number]\n\t-- get the full message by [number] addressed to the [recipient]";
}
string ReadHandler::getCommandPrefix() {
return "read ";
}
string ReadHandler::getName() {
return "ReadHandler";
}