From a0f3c42d609d92bbc9d9e3ca0457ce1841c2ea26 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 4 May 2015 14:07:43 +0200 Subject: [PATCH] jsv8provider --- eth/main.cpp | 14 +-------- libjsconsole/CMakeLists.txt | 3 +- libjsconsole/JSConsole.cpp | 14 ++++----- libjsconsole/JSConsole.h | 7 +++-- libjsconsole/JSV8Connector.cpp | 36 +++++++++++++++++++++++ libjsconsole/JSV8Connector.h | 30 +++++++++++++++++++ libjsengine/JSV8RPC.cpp | 53 ++++++++++++++-------------------- libjsengine/JSV8RPC.h | 10 ++----- 8 files changed, 102 insertions(+), 65 deletions(-) create mode 100644 libjsconsole/JSV8Connector.cpp create mode 100644 libjsconsole/JSV8Connector.h diff --git a/eth/main.cpp b/eth/main.cpp index 1ead0e08d..f1b2c0358 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -412,18 +412,6 @@ void doFarm(MinerType _m, string const& _remote, unsigned _recheckPeriod) int main(int argc, char** argv) { - JSConsole console; - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); - console.repl(); cout << "\x1b[30mEthBlack\x1b[0m" << endl; cout << "\x1b[90mEthCoal\x1b[0m" << endl; cout << "\x1b[37mEthGray\x1b[0m" << endl; @@ -1638,7 +1626,7 @@ int main(int argc, char** argv) if (useConsole) { #if ETH_JSCONSOLE - JSConsole console; + JSConsole console(web3, vector({sigKey})); while (!g_exit) { console.repl(); diff --git a/libjsconsole/CMakeLists.txt b/libjsconsole/CMakeLists.txt index f019cbccf..932cd20dd 100644 --- a/libjsconsole/CMakeLists.txt +++ b/libjsconsole/CMakeLists.txt @@ -13,6 +13,7 @@ aux_source_directory(. SRC_LIST) include_directories(BEFORE ..) include_directories(${V8_INCLUDE_DIRS}) include_directories(${READLINE_INCLUDE_DIRS}) +include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) set(EXECUTABLE jsconsole) @@ -26,7 +27,7 @@ add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS} ${JSRES}) target_link_libraries(${EXECUTABLE} jsengine) target_link_libraries(${EXECUTABLE} devcore) target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES}) +target_link_libraries(${EXECUTABLE} web3jsonrpc) install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) - diff --git a/libjsconsole/JSConsole.cpp b/libjsconsole/JSConsole.cpp index 3c63d6330..ec9d05948 100644 --- a/libjsconsole/JSConsole.cpp +++ b/libjsconsole/JSConsole.cpp @@ -3,26 +3,24 @@ // #include -#include #include #include "JSConsole.h" +#include "JSV8Connector.h" #include "libjsconsole/JSConsoleResources.hpp" // TODO: readline! #include -#include using namespace std; using namespace dev; using namespace dev::eth; -JSConsole::JSConsole(): m_engine(), m_printer(m_engine), m_rpc(m_engine) +JSConsole::JSConsole(WebThreeDirect& _web3, std::vector const& _accounts): + m_engine(), + m_printer(m_engine) { -// JSConsoleResources resources; -// string web3 = resources.loadResourceAsString("web3"); -// m_engine.eval(web3.c_str()); -// m_engine.eval("web3 = require('web3');"); - m_rpc.StartListening(); + m_jsonrpcConnector.reset(new JSV8Connector(m_engine)); + m_jsonrpcServer.reset(new WebThreeStubServer(*m_jsonrpcConnector.get(), _web3, _accounts)); } void JSConsole::repl() const diff --git a/libjsconsole/JSConsole.h b/libjsconsole/JSConsole.h index 96008baa8..5112b056d 100644 --- a/libjsconsole/JSConsole.h +++ b/libjsconsole/JSConsole.h @@ -6,7 +6,7 @@ #include #include -#include +#include namespace dev { @@ -16,7 +16,7 @@ namespace eth class JSConsole { public: - JSConsole(); + JSConsole(WebThreeDirect& _web3, std::vector const& _accounts); void repl() const; private: @@ -24,7 +24,8 @@ private: JSV8Engine m_engine; JSV8Printer m_printer; - JSV8RPC m_rpc; + std::unique_ptr m_jsonrpcServer; + std::unique_ptr m_jsonrpcConnector; }; } diff --git a/libjsconsole/JSV8Connector.cpp b/libjsconsole/JSV8Connector.cpp new file mode 100644 index 000000000..1d43e7cd2 --- /dev/null +++ b/libjsconsole/JSV8Connector.cpp @@ -0,0 +1,36 @@ +// +// Created by Marek Kotewicz on 04/05/15. +// + +#include "JSV8Connector.h" + +using namespace std; +using namespace dev; +using namespace dev::eth; + +bool JSV8Connector::StartListening() +{ + return true; +} + +bool JSV8Connector::StopListening() +{ + return true; +} + +bool JSV8Connector::SendResponse(std::string const &_response, void *_addInfo) +{ + (void)_addInfo; + m_lastResponse = _response.c_str(); + return true; +} + +void JSV8Connector::onSend(const char *payload) +{ + OnRequest(payload, NULL); +} + +JSV8Connector::~JSV8Connector() +{ + StopListening(); +} diff --git a/libjsconsole/JSV8Connector.h b/libjsconsole/JSV8Connector.h new file mode 100644 index 000000000..d76aa8d1c --- /dev/null +++ b/libjsconsole/JSV8Connector.h @@ -0,0 +1,30 @@ +// +// Created by Marek Kotewicz on 04/05/15. +// + +#pragma once + +#include +#include + +namespace dev +{ +namespace eth +{ + +class JSV8Connector : public jsonrpc::AbstractServerConnector, public JSV8RPC +{ + +public: + JSV8Connector(JSV8Engine const &_engine) : JSV8RPC(_engine) {} + virtual ~JSV8Connector(); + + bool StartListening(); + bool StopListening(); + bool SendResponse(std::string const& _response, void* _addInfo = NULL); + + void onSend(const char* payload); +}; + +} +} diff --git a/libjsengine/JSV8RPC.cpp b/libjsengine/JSV8RPC.cpp index 72dbc12ca..8ce7c81d0 100644 --- a/libjsengine/JSV8RPC.cpp +++ b/libjsengine/JSV8RPC.cpp @@ -4,7 +4,6 @@ #include "libjsconsole/JSConsoleResources.hpp" #include "JSV8RPC.h" -#include using namespace std; using namespace dev; @@ -17,34 +16,29 @@ namespace eth void JSV8RPCSend(v8::FunctionCallbackInfo const& args) { - const char* tmp = R"( - { - "id": 1, - "jsonrpc": "2.0", - "result": "0x9df33b35fbdd8dff5e557a0cce288614dbf7327c292f1ac5b9c6c0e672005f48" - } - )"; - v8::Local JSON = v8::String::NewFromUtf8(args.GetIsolate(), "JSON"); v8::Local parse = v8::String::NewFromUtf8(args.GetIsolate(), "parse"); + v8::Local stringify = v8::String::NewFromUtf8(args.GetIsolate(), "stringify"); v8::Handle jsonObject = v8::Handle::Cast(args.GetIsolate()->GetCurrentContext()->Global()->Get(JSON)); - v8::Handle func = v8::Handle::Cast(jsonObject->Get(parse)); + v8::Handle parseFunc = v8::Handle::Cast(jsonObject->Get(parse)); + v8::Handle stringifyFunc = v8::Handle::Cast(jsonObject->Get(stringify)); - v8::Local values[1] = {v8::String::NewFromUtf8(args.GetIsolate(), tmp)}; - args.GetReturnValue().Set(func->Call(func, 1, values)); -} + v8::Local self = args.Holder(); + v8::Local wrap = v8::Local::Cast(self->GetInternalField(0)); + JSV8RPC* that = static_cast(wrap->Value()); + v8::Local vals[1] = {args[0]->ToObject()}; + v8::Local stringifiedArg = stringifyFunc->Call(stringifyFunc, 1, vals); + v8::String::Utf8Value str(stringifiedArg); + that->onSend(*str); + v8::Local values[1] = {v8::String::NewFromUtf8(args.GetIsolate(), that->m_lastResponse)}; + args.GetReturnValue().Set(parseFunc->Call(parseFunc, 1, values)); } -} - -JSV8RPC::JSV8RPC(JSV8Engine const &_engine): m_engine(_engine) {} -JSV8RPC::~JSV8RPC() -{ - StopListening(); +} } -bool JSV8RPC::StartListening() +JSV8RPC::JSV8RPC(JSV8Engine const &_engine): m_engine(_engine) { v8::HandleScope scope(m_engine.context()->GetIsolate()); v8::Local rpcTemplate = v8::ObjectTemplate::New(m_engine.context()->GetIsolate()); @@ -63,17 +57,12 @@ bool JSV8RPC::StartListening() v8::Handle func = v8::Handle::Cast(web3object->Get(setProvider)); v8::Local values[1] = {obj}; func->Call(func, 1, values); - return true; -} -bool JSV8RPC::StopListening() -{ - return true; -} - -bool JSV8RPC::SendResponse(std::string const &_response, void *_addInfo) -{ - m_lastResponse = _response; - (void)_addInfo; - return true; + m_lastResponse = R"( + { + "id": 1, + "jsonrpc": "2.0", + "error": "Uninitalized JSV8RPC!" + } + )"; } diff --git a/libjsengine/JSV8RPC.h b/libjsengine/JSV8RPC.h index 36591ee01..25e671ab6 100644 --- a/libjsengine/JSV8RPC.h +++ b/libjsengine/JSV8RPC.h @@ -4,7 +4,6 @@ #pragma once -//#include #include namespace dev @@ -16,17 +15,12 @@ class JSV8RPC { public: JSV8RPC(JSV8Engine const& _engine); - virtual ~JSV8RPC(); - bool StartListening(); - - bool StopListening(); - - bool SendResponse(std::string const& _response, void* _addInfo = NULL); + virtual void onSend(const char* _payload) = 0; + const char* m_lastResponse; private: JSV8Engine const& m_engine; - std::string m_lastResponse; }; }