Skip to content

Commit

Permalink
Moving add_and_run option from server to client
Browse files Browse the repository at this point in the history
  • Loading branch information
mendelgusmao committed Mar 1, 2012
1 parent c1db89f commit 55ea5b7
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Command {
bool is_malformed;
bool is_internal;
bool do_not_queue;
bool add_and_run;

Command();
string line();
Expand Down
2 changes: 2 additions & 0 deletions src/Common/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Command Protocol::parse(const string &data) {
cmd.interval = lexical_cast<unsigned int>(packet[3]);
cmd.timeout = lexical_cast<unsigned int>(packet[4]);
cmd.do_not_queue = packet[5] == "1";
cmd.add_and_run = packet[6] == "1";
cmd.is_malformed = false;
}
}
Expand All @@ -48,6 +49,7 @@ string Protocol::build(const Command &cmd) {
packet.push_back(lexical_cast<string>(cmd.interval));
packet.push_back(lexical_cast<string>(cmd.timeout));
packet.push_back(cmd.do_not_queue ? "1" : "0");
packet.push_back(cmd.add_and_run ? "1" : "0");
packet.push_back("");

return join(packet, PROTOCOL_DELIMITER);
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define PROTOCOL_HEADER "cgi4lcd"
#define PROTOCOL_DELIMITER "|"
#define PROTOCOL_EXPECTED_SIZE 7
#define PROTOCOL_EXPECTED_SIZE 8

using namespace std;

Expand Down
3 changes: 3 additions & 0 deletions src/Plugin/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ unsigned int Client::_execution_interval(0);
unsigned int Client::_execution_timeout(0);
int Client::_refresh_interval(0);
string Client::_default_extension("");
bool Client::_add_and_run(false);

void Client::start() {
_app_path = Utils::app_path();
Expand All @@ -26,6 +27,7 @@ void Client::start() {
_execution_timeout = lexical_cast<unsigned int>(Utils::ini_read(_ini_file, "cgi4lcd.timeout", "30000"));
_default_extension = Utils::ini_read(_ini_file, "cgi4lcd.default_extension", "");
_refresh_interval = lexical_cast<int>(Utils::ini_read(_ini_file, "cgi4lcd.refresh", "1000"));
_add_and_run = Utils::ini_read(_ini_file, "cgi4lcd.add_and_run", "1") == "1";
}

string Client::execute(string script, const string &parameters, bool version, bool do_not_queue) {
Expand Down Expand Up @@ -97,6 +99,7 @@ string Client::request(const string &interpreter, const string &arguments, unsig
cmd.interval = interval;
cmd.timeout = timeout;
cmd.do_not_queue = do_not_queue;
cmd.add_and_run = _add_and_run;

string buffer("");

Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Client {
static unsigned int _execution_timeout;
static int _refresh_interval;
static string _default_extension;
static bool _add_and_run;

static void start();
static string execute(string script, const string &parameters, bool version=false, bool do_not_queue=false);
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Queue::Queue(boost::asio::io_service& io_service) :

}

void Queue::add(Command &cmd, bool add_and_run) {
void Queue::add(Command &cmd) {

map<string, Command>::iterator it = _commands.find(cmd.line());

if (it == _commands.end()) {
cmd.response = "";
_commands[cmd.line()] = cmd;

if (add_and_run) {
if (cmd.add_and_run) {
_commands[cmd.line()].run();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Queue {
public:

Queue(boost::asio::io_service& io_service);
void add(Command &cmd, bool add_and_run);
void add(Command &cmd);
void run();
Command get(const string& line);

Expand Down
7 changes: 3 additions & 4 deletions src/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

using namespace std;

Server::Server(boost::asio::io_service& io_service, short port, bool add_and_run) :
Server::Server(boost::asio::io_service& io_service, short port) :
_io_service(io_service),
_socket(io_service, udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port)),
_queue(io_service),
_add_and_run(add_and_run)
_queue(io_service)
{

receive();
Expand All @@ -29,7 +28,7 @@ void Server::handle_receive_from(const boost::system::error_code& error, size_t
cmd.run();
}
else {
_queue.add(cmd, _add_and_run);
_queue.add(cmd);
cmd = _queue.get(cmd.line());
}

Expand Down
3 changes: 1 addition & 2 deletions src/Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class Server
udp::socket _socket;
Queue _queue;
udp::endpoint _sender_endpoint;
bool _add_and_run;
enum { max_length = 1024 };
char _data[max_length];

Server(boost::asio::io_service& io_service, short port, bool add_and_run);
Server(boost::asio::io_service& io_service, short port);
void handle_receive_from(const boost::system::error_code& error, size_t bytes_recvd);
void handle_send_to(const boost::system::error_code& error, size_t bytes_sent);
void receive();
Expand Down
3 changes: 1 addition & 2 deletions src/Server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ int main(int argc, char* argv[]) {

string ini_file(Utils::app_path() + "\\..\\scripts\\cgi4lcd.ini");
unsigned int port = lexical_cast<unsigned int>(Utils::ini_read(ini_file, "cgi4lcd.port", "65432"));
bool add_and_run = Utils::ini_read(ini_file, "cgi4lcd.add_and_run", "1") == "1";

#ifndef DEBUG
HWND hWnd = GetConsoleWindow();
Expand All @@ -17,7 +16,7 @@ int main(int argc, char* argv[]) {

try {
boost::asio::io_service io_service;
Server s(io_service, port, add_and_run);
Server s(io_service, port);
io_service.run();
}
catch (std::exception& e) {
Expand Down

0 comments on commit 55ea5b7

Please sign in to comment.