Created
February 21, 2016 16:15
-
-
Save polarbeard/f5c5b02a2ce7c1a69c77 to your computer and use it in GitHub Desktop.
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
diff -uNr a/bitcoin/src/hooks.cpp b/bitcoin/src/hooks.cpp | |
--- a/bitcoin/src/hooks.cpp 1969-12-31 19:00:00.000000000 -0500 | |
+++ b/bitcoin/src/hooks.cpp 2016-02-21 10:09:51.216134247 -0500 | |
@@ -0,0 +1,19 @@ | |
+#include "hooks.h" | |
+ | |
+void SchemeHook::SetCallback(scheme *sc, pointer callback) { | |
+ scbk = SchemeCallback(sc, type, callback); | |
+} | |
+ | |
+void SchemeHook::Call(const char *event, CNode* pnode) { | |
+ if (scbk.bound) { | |
+ Call(mk_pair(scbk.sc, event, marshall(scbk.sc, pnode))); | |
+ } | |
+} | |
+ | |
+void SchemeHook::Call(pointer pair) { | |
+ if (scbk.bound) { | |
+ scheme_call(scbk.sc, scheme_eval(scbk.sc, scbk.symbol), pair); | |
+ } | |
+} | |
+ | |
+SchemeHook PeerHook("btc-peer"); | |
diff -uNr a/bitcoin/src/hooks.h b/bitcoin/src/hooks.h | |
--- a/bitcoin/src/hooks.h 1969-12-31 19:00:00.000000000 -0500 | |
+++ b/bitcoin/src/hooks.h 2016-02-20 14:36:54.367154298 -0500 | |
@@ -0,0 +1,35 @@ | |
+#ifndef HOOKS_H | |
+#define HOOKS_H | |
+ | |
+#include "headers.h" | |
+#include "shiva.h" | |
+ | |
+using namespace std; | |
+using namespace scm; | |
+ | |
+class SchemeCallback { | |
+public: | |
+ bool bound; | |
+ scheme *sc; | |
+ pointer symbol; | |
+ SchemeCallback() : bound(false) {} | |
+ SchemeCallback(scheme *sc, string type, pointer fn) | |
+ : bound(true), sc(sc), | |
+ symbol(mk_symbol(sc, (type + "-hook-callback").c_str())) { | |
+ scheme_define(sc, sc->global_env, symbol, fn); | |
+ } | |
+}; | |
+ | |
+class SchemeHook { | |
+ SchemeCallback scbk; | |
+ string type; | |
+public: | |
+ SchemeHook(string type) : type(type) {} | |
+ void SetCallback (scheme *sc, pointer callback); | |
+ void Call(const char *event, CNode* pnode); | |
+ void Call(pointer pair); | |
+}; | |
+ | |
+extern SchemeHook PeerHook; | |
+ | |
+#endif | |
diff -uNr a/bitcoin/src/makefile.unix b/bitcoin/src/makefile.unix | |
--- a/bitcoin/src/makefile.unix 2016-02-21 11:11:30.926480120 -0500 | |
+++ b/bitcoin/src/makefile.unix 2016-02-21 07:33:01.001471512 -0500 | |
@@ -71,7 +71,7 @@ | |
DEBUGFLAGS=-g | |
CXXFLAGS=-O2 | |
xCXXFLAGS=-pthread -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(DEFS) $(HARDENING) $(CXXFLAGS) | |
-CFLAGS=-lm -DUSE_ASCII_NAMES=0 -O2 | |
+CFLAGS=$(DEBUGFLAGS) -lm -DUSE_ASCII_NAMES=0 -O2 | |
HEADERS = \ | |
base58.h \ | |
bignum.h \ | |
@@ -79,6 +79,7 @@ | |
crypter.h \ | |
db.h \ | |
headers.h \ | |
+ hooks.h \ | |
init.h \ | |
key.h \ | |
keystore.h \ | |
@@ -100,6 +101,7 @@ | |
obj/checkpoints.o \ | |
obj/crypter.o \ | |
obj/db.o \ | |
+ obj/hooks.o \ | |
obj/init.o \ | |
obj/keystore.o \ | |
obj/main.o \ | |
diff -uNr a/bitcoin/src/net.cpp b/bitcoin/src/net.cpp | |
--- a/bitcoin/src/net.cpp 2016-02-21 11:11:30.974480358 -0500 | |
+++ b/bitcoin/src/net.cpp 2016-02-20 13:43:50.235365041 -0500 | |
@@ -8,7 +8,7 @@ | |
#include "net.h" | |
#include "init.h" | |
#include "strlcpy.h" | |
- | |
+#include "hooks.h" | |
using namespace std; | |
using namespace boost; | |
@@ -462,9 +462,12 @@ | |
else | |
pnode->AddRef(); | |
CRITICAL_BLOCK(cs_vNodes) | |
+ { | |
vNodes.push_back(pnode); | |
+ } | |
pnode->nTimeConnected = GetTime(); | |
+ PeerHook.Call("connected", pnode); | |
return pnode; | |
} | |
else | |
@@ -603,6 +606,7 @@ | |
// close socket and cleanup | |
pnode->CloseSocketDisconnect(); | |
+ PeerHook.Call("disconnect", pnode); | |
pnode->Cleanup(); | |
// hold in disconnected pool until all refs are released | |
@@ -733,7 +737,10 @@ | |
CNode* pnode = new CNode(hSocket, addr, true); | |
pnode->AddRef(); | |
CRITICAL_BLOCK(cs_vNodes) | |
+ { | |
vNodes.push_back(pnode); | |
+ PeerHook.Call("connected", pnode); | |
+ } | |
} | |
} | |
diff -uNr a/bitcoin/src/shiva/scheme.c b/bitcoin/src/shiva/scheme.c | |
--- a/bitcoin/src/shiva/scheme.c 2016-02-21 11:11:30.714479069 -0500 | |
+++ b/bitcoin/src/shiva/scheme.c 2016-02-21 11:11:14.246397408 -0500 | |
@@ -1571,6 +1571,8 @@ | |
port *pt=sc->outport->_object._port; | |
if(pt->kind&port_file) { | |
fwrite(s,1,len,pt->rep.stdio.file); | |
+ if( pt->rep.stdio.interactive ) | |
+ fflush( pt->rep.stdio.file ); | |
} else { | |
for(;len;len--) { | |
if(pt->rep.string.curr!=pt->rep.string.past_the_end) { | |
diff -uNr a/bitcoin/src/shiva/scheme-knobs.h b/bitcoin/src/shiva/scheme-knobs.h | |
--- a/bitcoin/src/shiva/scheme-knobs.h 2016-02-21 11:11:30.578478394 -0500 | |
+++ b/bitcoin/src/shiva/scheme-knobs.h 2016-02-17 06:16:18.632911743 -0500 | |
@@ -4,6 +4,6 @@ | |
#define STANDALONE 0 | |
#define USE_INTERFACE 1 | |
#define USE_MATH 1 | |
-#define prompt "#>" | |
+#define prompt "#> " | |
#endif | |
diff -uNr a/bitcoin/src/shiva.cpp b/bitcoin/src/shiva.cpp | |
--- a/bitcoin/src/shiva.cpp 2016-02-21 11:11:30.642478712 -0500 | |
+++ b/bitcoin/src/shiva.cpp 2016-02-21 11:14:13.059284093 -0500 | |
@@ -22,7 +22,7 @@ | |
#include "init.h" | |
#include "util.h" | |
#include "shiva.h" | |
- | |
+#include "hooks.h" | |
using namespace std; | |
using namespace scm; | |
@@ -169,6 +169,70 @@ | |
return NULL; | |
} | |
+pointer reverse_in_place(scheme *sc, pointer term, pointer list) { | |
+ pointer p = list, result = term, q; | |
+ | |
+ while (p != sc->NIL) { | |
+ q = sc->vptr->pair_cdr(p); | |
+ sc->vptr->set_cdr(p, result); | |
+ result = p; | |
+ p = q; | |
+ } | |
+ return (result); | |
+} | |
+ | |
+pointer mk_pair(scheme *sc, const char *k, pointer v) { | |
+ return cons(sc, sc->vptr->mk_symbol(sc, k), cons(sc, v, sc->NIL)); | |
+} | |
+ | |
+pointer mk_list(scheme *sc, ...) { | |
+ va_list arg_ptr; | |
+ va_start(arg_ptr, sc); | |
+ | |
+ pointer arg = va_arg(arg_ptr, pointer); | |
+ pointer list = sc->NIL; | |
+ | |
+ while (arg != NULL) { | |
+ list = cons(sc, arg, list); | |
+ arg = va_arg(arg_ptr, pointer); | |
+ } | |
+ | |
+ va_end(arg_ptr); | |
+ return reverse_in_place(sc, sc->NIL, list); | |
+} | |
+ | |
+pointer marshall(scheme *sc, bool v) { | |
+ return v ? sc->T : sc->F; | |
+} | |
+ | |
+pointer marshall(scheme *sc, int v) { | |
+ return sc->vptr->mk_integer(sc, v); | |
+} | |
+ | |
+pointer marshall(scheme *sc, string v) { | |
+ return sc->vptr->mk_string(sc, v.c_str()); | |
+} | |
+ | |
+pointer marshall(scheme *sc, boost::int64_t v) { | |
+ return sc->vptr->mk_integer(sc, v); | |
+} | |
+ | |
+pointer marshall(scheme *sc, CNode* pnode) { | |
+ return mk_list(sc, | |
+ mk_pair(sc, "address", marshall(sc, pnode->addr.ToString())), | |
+ mk_pair(sc, "services", marshall(sc, strprintf("%08"PRI64x, pnode->nServices))), | |
+ mk_pair(sc, "last-send", marshall(sc, (boost::int64_t)pnode->nLastSend)), | |
+ mk_pair(sc, "last-send-empty", marshall(sc, (boost::int64_t)pnode->nLastSendEmpty)), | |
+ mk_pair(sc, "last-recv", marshall(sc, (boost::int64_t)pnode->nLastRecv)), | |
+ mk_pair(sc, "connected-time", marshall(sc, (boost::int64_t)pnode->nTimeConnected)), | |
+ mk_pair(sc, "version", marshall(sc, pnode->nVersion)), | |
+ mk_pair(sc, "subversion", marshall(sc, pnode->strSubVer)), | |
+ mk_pair(sc, "is-client", marshall(sc, pnode->fClient)), | |
+ mk_pair(sc, "is-inbound", marshall(sc, pnode->fInbound)), | |
+ mk_pair(sc, "is-network-node", marshall(sc, pnode->fNetworkNode)), | |
+ mk_pair(sc, "release-time", marshall(sc, (boost::int64_t)pnode->nReleaseTime)), | |
+ mk_pair(sc, "starting-height", marshall(sc, pnode->nStartingHeight)), NULL); | |
+} | |
////////////////////////////////////////////////////////////////////////////// | |
/* Now Taste The Meat. */ | |
@@ -187,9 +251,35 @@ | |
return sc->NIL; | |
} | |
+/* Peer hook binder. */ | |
+static pointer on_btc_peer(scheme *sc, pointer args) { | |
+ pointer callback = sc->vptr->pair_car(args); | |
+ PeerHook.SetCallback(sc, callback); | |
+ return callback; | |
+} | |
+ | |
+/* Peer emitter. */ | |
+static pointer emit_btc_peer(scheme *sc, pointer args) { | |
+ PeerHook.Call(args); | |
+ return args; | |
+} | |
+ | |
+/* Returns connected peers. */ | |
+static pointer btc_peers(scheme *sc, pointer args) { | |
+ pointer peers = sc->NIL; | |
+ | |
+ CRITICAL_BLOCK(cs_vNodes) | |
+ BOOST_FOREACH(CNode* pnode, vNodes) | |
+ peers = cons(sc, marshall(sc, pnode), peers); | |
+ | |
+ return peers; | |
+} | |
/* Install the hooks. For each of the above, must do this: */ | |
static void init_shiva_hooks(scheme *sc) { | |
+ scheme_define(sc, sc->global_env, mk_symbol(sc, "btc-peers" ), mk_foreign_func(sc, btc_peers)); | |
+ scheme_define(sc, sc->global_env, mk_symbol(sc, "on-btc-peer" ), mk_foreign_func(sc, on_btc_peer)); | |
+ scheme_define(sc, sc->global_env, mk_symbol(sc, "emit-btc-peer" ), mk_foreign_func(sc, emit_btc_peer)); | |
scheme_define(sc, sc->global_env, mk_symbol(sc, "btc-get-best-height" ), mk_foreign_func(sc, btc_get_best_height)); | |
scheme_define(sc, sc->global_env, mk_symbol(sc, "btc-shutdown" ), mk_foreign_func(sc, btc_shutdown)); | |
} | |
diff -uNr a/bitcoin/src/shiva.h b/bitcoin/src/shiva.h | |
--- a/bitcoin/src/shiva.h 2016-02-21 11:11:30.618478593 -0500 | |
+++ b/bitcoin/src/shiva.h 2016-02-21 09:06:02.549148913 -0500 | |
@@ -1,6 +1,8 @@ | |
#ifndef SHIVA_H | |
#define SHIVA_H | |
+#include "init.h" | |
+ | |
#define BACKLOG 10 /* listen() */ | |
namespace scm { | |
@@ -11,5 +13,16 @@ | |
void ThreadShiva(void* parg); | |
+scm::pointer reverse_in_place(scm::scheme *sc, scm::pointer term, scm::pointer list); | |
+ | |
+scm::pointer mk_pair(scm::scheme *sc, const char *k, scm::pointer v); | |
+scm::pointer mk_list(scm::scheme *sc, ...); | |
+ | |
+scm::pointer marshall(scm::scheme *sc, bool v); | |
+scm::pointer marshall(scm::scheme *sc, int v); | |
+scm::pointer marshall(scm::scheme *sc, std::string v); | |
+scm::pointer marshall(scm::scheme *sc, boost::int64_t v); | |
+scm::pointer marshall(scm::scheme *sc, CNode* pnode); | |
+ | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment