Browse Source

jsv8provider

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
a0f3c42d60
  1. 14
      eth/main.cpp
  2. 3
      libjsconsole/CMakeLists.txt
  3. 14
      libjsconsole/JSConsole.cpp
  4. 7
      libjsconsole/JSConsole.h
  5. 36
      libjsconsole/JSV8Connector.cpp
  6. 30
      libjsconsole/JSV8Connector.h
  7. 53
      libjsengine/JSV8RPC.cpp
  8. 10
      libjsengine/JSV8RPC.h

14
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<KeyPair>({sigKey}));
while (!g_exit)
{
console.repl();

3
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} )

14
libjsconsole/JSConsole.cpp

@ -3,26 +3,24 @@
//
#include <iostream>
#include <algorithm>
#include <libdevcore/Log.h>
#include "JSConsole.h"
#include "JSV8Connector.h"
#include "libjsconsole/JSConsoleResources.hpp"
// TODO: readline!
#include <readline/readline.h>
#include <readline/history.h>
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<dev::KeyPair> 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

7
libjsconsole/JSConsole.h

@ -6,7 +6,7 @@
#include <libjsengine/JSV8Engine.h>
#include <libjsengine/JSV8Printer.h>
#include <libjsengine/JSV8RPC.h>
#include <libweb3jsonrpc/WebThreeStubServer.h>
namespace dev
{
@ -16,7 +16,7 @@ namespace eth
class JSConsole
{
public:
JSConsole();
JSConsole(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts);
void repl() const;
private:
@ -24,7 +24,8 @@ private:
JSV8Engine m_engine;
JSV8Printer m_printer;
JSV8RPC m_rpc;
std::unique_ptr<WebThreeStubServer> m_jsonrpcServer;
std::unique_ptr<jsonrpc::AbstractServerConnector> m_jsonrpcConnector;
};
}

36
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();
}

30
libjsconsole/JSV8Connector.h

@ -0,0 +1,30 @@
//
// Created by Marek Kotewicz on 04/05/15.
//
#pragma once
#include <jsonrpccpp/server/abstractserverconnector.h>
#include <libjsengine/JSV8RPC.h>
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);
};
}
}

53
libjsengine/JSV8RPC.cpp

@ -4,7 +4,6 @@
#include "libjsconsole/JSConsoleResources.hpp"
#include "JSV8RPC.h"
#include <assert.h>
using namespace std;
using namespace dev;
@ -17,34 +16,29 @@ namespace eth
void JSV8RPCSend(v8::FunctionCallbackInfo<v8::Value> const& args)
{
const char* tmp = R"(
{
"id": 1,
"jsonrpc": "2.0",
"result": "0x9df33b35fbdd8dff5e557a0cce288614dbf7327c292f1ac5b9c6c0e672005f48"
}
)";
v8::Local<v8::String> JSON = v8::String::NewFromUtf8(args.GetIsolate(), "JSON");
v8::Local<v8::String> parse = v8::String::NewFromUtf8(args.GetIsolate(), "parse");
v8::Local<v8::String> stringify = v8::String::NewFromUtf8(args.GetIsolate(), "stringify");
v8::Handle<v8::Object> jsonObject = v8::Handle<v8::Object>::Cast(args.GetIsolate()->GetCurrentContext()->Global()->Get(JSON));
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse));
v8::Handle<v8::Function> parseFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(parse));
v8::Handle<v8::Function> stringifyFunc = v8::Handle<v8::Function>::Cast(jsonObject->Get(stringify));
v8::Local<v8::Value> values[1] = {v8::String::NewFromUtf8(args.GetIsolate(), tmp)};
args.GetReturnValue().Set(func->Call(func, 1, values));
}
v8::Local<v8::Object> self = args.Holder();
v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(self->GetInternalField(0));
JSV8RPC* that = static_cast<JSV8RPC*>(wrap->Value());
v8::Local<v8::Value> vals[1] = {args[0]->ToObject()};
v8::Local<v8::Value> stringifiedArg = stringifyFunc->Call(stringifyFunc, 1, vals);
v8::String::Utf8Value str(stringifiedArg);
that->onSend(*str);
v8::Local<v8::Value> 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<v8::ObjectTemplate> rpcTemplate = v8::ObjectTemplate::New(m_engine.context()->GetIsolate());
@ -63,17 +57,12 @@ bool JSV8RPC::StartListening()
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(web3object->Get(setProvider));
v8::Local<v8::Value> 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!"
}
)";
}

10
libjsengine/JSV8RPC.h

@ -4,7 +4,6 @@
#pragma once
//#include <jsonrpccpp/server/abstractserverconnector.h>
#include <libjsengine/JSV8Engine.h>
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;
};
}

Loading…
Cancel
Save