Skip to content

Commit

Permalink
Fix RputsPlugin class for ATS::rputs and ATS::echo
Browse files Browse the repository at this point in the history
Fix RputsPlugin class for ATS::rputs and ATS::echo mruby methods.
TODO: Improve RputsPlugin pointer management by using Singleton pattern.
  • Loading branch information
syucream committed Apr 29, 2014
1 parent d8e3997 commit 45662c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/ts_mruby_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
//
*/

#include <iostream>
#include <sstream>

#include <atscppapi/HttpStatus.h>
#include <atscppapi/Transaction.h>
#include <atscppapi/InterceptPlugin.h>

#include <mruby.h>
#include <mruby/proc.h>
Expand All @@ -24,34 +19,20 @@
using namespace atscppapi;
using std::string;

class RputsPlugin : public InterceptPlugin {
private:
const string _message;

template <typename T>
string toString(T num){
std::stringstream ss;
ss << num;
return ss.str();
}

public:
RputsPlugin(Transaction &transaction, const string& msg)
: InterceptPlugin(transaction, InterceptPlugin::TRANSACTION_INTERCEPT), _message(msg) { }

void consume(const string &data, InterceptPlugin::RequestDataType type) {}
RputsPlugin *rputs = NULL;

void handleInputComplete(){
string response("HTTP/1.1 200 OK\r\n"
"Content-Length: " + toString(_message.size()) + "\r\n"
"\r\n");
InterceptPlugin::produce(response);
response = _message + "\r\n";
InterceptPlugin::produce(response);
InterceptPlugin::setOutputComplete();
}
RputsPlugin::~RputsPlugin() { rputs = NULL; }

};
void RputsPlugin::handleInputComplete(){
string response("HTTP/1.1 200 OK\r\n"
"Content-Length: " + toString(_message.size()) + "\r\n"
"\r\n");
InterceptPlugin::produce(response);
response = _message + "\r\n";
InterceptPlugin::produce(response);
InterceptPlugin::setOutputComplete();
}

static mrb_value ts_mrb_get_ts_mruby_name(mrb_state *mrb, mrb_value self)
{
Expand Down Expand Up @@ -82,8 +63,12 @@ static mrb_value ts_mrb_rputs(mrb_state *mrb, mrb_value self)
}
const string msg((char*)RSTRING_PTR(argv), RSTRING_LEN(argv));

atscppapi::Transaction* transaction = ts_mrb_get_transaction();
transaction->addPlugin(new RputsPlugin(*transaction, msg));
if (rputs == NULL) {
atscppapi::Transaction* transaction = ts_mrb_get_transaction();
rputs = new RputsPlugin(*transaction);
transaction->addPlugin(rputs);
}
rputs->appendMessage(msg);

return self;
}
Expand All @@ -98,8 +83,12 @@ static mrb_value ts_mrb_echo(mrb_state *mrb, mrb_value self)
string msg((char*)RSTRING_PTR(argv), RSTRING_LEN(argv));
msg += "\n";

atscppapi::Transaction* transaction = ts_mrb_get_transaction();
transaction->addPlugin(new RputsPlugin(*transaction, msg));
if (rputs == NULL) {
atscppapi::Transaction* transaction = ts_mrb_get_transaction();
rputs = new RputsPlugin(*transaction);
transaction->addPlugin(rputs);
}
rputs->appendMessage(msg);

return self;
}
Expand Down
39 changes: 39 additions & 0 deletions src/ts_mruby_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,46 @@
#ifndef TS_MRUBY_CORE_H
#define TS_MRUBY_CORE_H

#include <iostream>
#include <sstream>

#include <atscppapi/Transaction.h>
#include <atscppapi/InterceptPlugin.h>

#define MODULE_NAME "ts_mruby"
#define MODULE_VERSION "0.0.1"

namespace {

using namespace atscppapi;
using std::string;

class RputsPlugin : public InterceptPlugin {
private:
string _message;

template <typename T>
string toString(T num){
std::stringstream ss;
ss << num;
return ss.str();
}

public:
RputsPlugin(Transaction &transaction)
: InterceptPlugin(transaction, InterceptPlugin::TRANSACTION_INTERCEPT),
_message("") { }

~RputsPlugin();

void consume(const string &data, InterceptPlugin::RequestDataType type) {}

void appendMessage(const string msg) { _message += msg; }

void handleInputComplete();

};

}

#endif // TS_MRUBY_CORE_H

0 comments on commit 45662c8

Please sign in to comment.