-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from syucream/feature/hook_prototype
Implement prototype of flexible hook handler class
- Loading branch information
Showing
14 changed files
with
598 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class MyHandler | ||
def on_send_request_hdr | ||
puts 'on_send_request_hdr' | ||
c = ATS::Connection.new | ||
puts c.remote_ip | ||
end | ||
|
||
def on_read_response_hdr | ||
puts 'on_read_response_hdr' | ||
c = ATS::Connection.new | ||
puts c.remote_ip | ||
end | ||
|
||
def on_send_response_hdr | ||
puts 'on_send_response_hdr' | ||
c = ATS::Connection.new | ||
puts c.remote_ip | ||
end | ||
end | ||
|
||
es = ATS::EventSystem.new | ||
es.register MyHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
// ts_mruby_eventsystem.cpp - ts_mruby mruby module | ||
// | ||
*/ | ||
|
||
#include "ts_mruby_eventsystem.hpp" | ||
#include "ts_mruby_internal.hpp" | ||
|
||
#include <atscppapi/Transaction.h> | ||
#include <atscppapi/utils.h> | ||
|
||
#include <mruby.h> | ||
#include <mruby/class.h> | ||
#include <mruby/compile.h> | ||
#include <mruby/data.h> | ||
#include <mruby/proc.h> | ||
#include <mruby/variable.h> | ||
|
||
using namespace std; | ||
using namespace atscppapi; | ||
|
||
// TODO Support some aliases ... ? | ||
static const string SEND_REQUEST_HDR_HANDLER = "on_send_request_hdr"; | ||
static const string READ_RESPONSE_HDR_HANDLER = "on_read_response_hdr"; | ||
static const string SEND_RESPONSE_HDR_HANDLER = "on_send_response_hdr"; | ||
|
||
void | ||
EventSystemPlugin::handleSendRequestHeaders(Transaction& transaction) { | ||
auto context = shared_ptr<TSMrubyContext>(new TSMrubyContext()); | ||
context->setTransaction(&transaction); | ||
context->setStateTag(TransactionStateTag::SEND_REQUEST_HEADERS); | ||
|
||
callHandler_(move(context), SEND_REQUEST_HDR_HANDLER); | ||
|
||
transaction.resume(); | ||
} | ||
|
||
void | ||
EventSystemPlugin::handleReadResponseHeaders(Transaction& transaction) { | ||
auto context = shared_ptr<TSMrubyContext>(new TSMrubyContext()); | ||
context->setTransaction(&transaction); | ||
context->setStateTag(TransactionStateTag::READ_RESPONSE_HEADERS); | ||
|
||
callHandler_(move(context), READ_RESPONSE_HDR_HANDLER); | ||
|
||
transaction.resume(); | ||
} | ||
|
||
void | ||
EventSystemPlugin::handleSendResponseHeaders(Transaction& transaction) { | ||
auto context = shared_ptr<TSMrubyContext>(new TSMrubyContext()); | ||
context->setTransaction(&transaction); | ||
context->setStateTag(TransactionStateTag::SEND_RESPONSE_HEADERS); | ||
|
||
callHandler_(move(context), SEND_RESPONSE_HDR_HANDLER); | ||
|
||
transaction.resume(); | ||
} | ||
|
||
mrb_value | ||
EventSystemPlugin::callHandler_(shared_ptr<TSMrubyContext> context, const string& sym) { | ||
auto* tlmrb = ts_mruby::getThreadLocalMrubyStates(); | ||
mrb_state* mrb = tlmrb->getMrb(); | ||
mrb->ud = reinterpret_cast<void *>(context.get()); | ||
|
||
// Run mruby script | ||
mrb_value rv = mrb_funcall(mrb, handler_obj_->getValue(), sym.c_str(), 0, nullptr); | ||
|
||
return rv; | ||
} | ||
|
||
static mrb_value ts_mrb_register_es(mrb_state *mrb, | ||
mrb_value self) { | ||
mrb_value argv; | ||
mrb_get_args(mrb, "C", &argv); | ||
struct RClass* handler_class = mrb_class_ptr(argv); | ||
|
||
auto* context = reinterpret_cast<TSMrubyContext *>(mrb->ud); | ||
auto* transaction = context->getTransaction(); | ||
transaction->addPlugin(new EventSystemPlugin(*transaction, mrb, handler_class)); | ||
|
||
return self; | ||
} | ||
|
||
void ts_mrb_eventsystem_class_init(mrb_state *mrb, struct RClass *rclass) { | ||
struct RClass *class_es; | ||
|
||
// EventSystem:: | ||
class_es = | ||
mrb_define_class_under(mrb, rclass, "EventSystem", mrb->object_class); | ||
|
||
mrb_define_method(mrb, class_es, "register", ts_mrb_register_es, MRB_ARGS_REQ(1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
// ts_mruby_eventsystem.h - ts_mruby mruby module header | ||
// | ||
*/ | ||
|
||
#ifndef TS_MRUBY_EVENTSYSTEM_H | ||
#define TS_MRUBY_EVENTSYSTEM_H | ||
|
||
#include "ts_mruby_init.hpp" | ||
#include "ts_mruby_internal.hpp" | ||
#include <atscppapi/Transaction.h> | ||
#include <atscppapi/TransactionPlugin.h> | ||
#include <mruby.h> | ||
|
||
void ts_mrb_eventsystem_class_init(mrb_state *mrb, struct RClass *rclass); | ||
|
||
/** | ||
* Management plugin of EventHandling and ATS hook. | ||
* | ||
*/ | ||
class EventSystemPlugin : public atscppapi::TransactionPlugin { | ||
private: | ||
std::shared_ptr<LentMrbValue> handler_obj_; | ||
mrb_value callHandler_(std::shared_ptr<TSMrubyContext>, const std::string&); | ||
|
||
public: | ||
EventSystemPlugin(atscppapi::Transaction &transaction, mrb_state* mrb, struct RClass* rclass) | ||
: atscppapi::TransactionPlugin(transaction) { | ||
// TODO High cost. It should switch hook resistration by any criteria. | ||
registerHook(HOOK_SEND_REQUEST_HEADERS); | ||
registerHook(HOOK_READ_RESPONSE_HEADERS); | ||
registerHook(HOOK_SEND_RESPONSE_HEADERS); | ||
|
||
// Should its handled in outer of this class? | ||
mrb_value v = mrb_obj_new(mrb, rclass, 0, nullptr); | ||
|
||
auto* tlmrb = ts_mruby::getThreadLocalMrubyStates(); | ||
handler_obj_ = tlmrb->getManager().lend_mrb_value(v); | ||
} | ||
|
||
void handleSendRequestHeaders(atscppapi::Transaction&); | ||
void handleReadResponseHeaders(atscppapi::Transaction&); | ||
void handleSendResponseHeaders(atscppapi::Transaction&); | ||
}; | ||
|
||
#endif // TS_MRUBY_EVENTSYSTEM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.