83 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-12-18 20:44:48 -05:00
server = require("./server")
wire = require("./wire")
util = require("util")
function CounterApp(){
this.hashCount = 0;
this.txCount = 0;
this.serial = false;
2015-12-18 20:44:48 -05:00
};
CounterApp.prototype.echo = function(msg){
2015-12-18 20:44:48 -05:00
return {"response": msg, "ret_code":0}
}
CounterApp.prototype.info = function(){
return {"response": [util.format("hashes:%d, txs:%d", this.hashCount, this.txCount)]}
2015-12-18 20:44:48 -05:00
}
CounterApp.prototype.set_option = function(key, value){
2015-12-18 20:44:48 -05:00
if (key == "serial" && value == "on"){
this.serial = true;
}
return {"ret_code":0}
}
CounterApp.prototype.append_tx = function(txBytes){
2015-12-18 20:44:48 -05:00
if (this.serial) {
2015-12-21 18:47:14 -05:00
txByteArray = new Buffer(txBytes)
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
2015-12-18 20:44:48 -05:00
txByteArray = wire.hex2bytes(txBytes.slice(2));
}
2015-12-21 18:47:14 -05:00
r = new msg.buffer(txByteArray)
txValue = wire.decode_big_endian(r, txBytes.length)
if (txValue != this.txCount){
return {"ret_code":6}
2015-12-18 20:44:48 -05:00
}
}
this.txCount += 1;
return {"ret_code":0} // TODO: return events
}
CounterApp.prototype.check_tx = function(txBytes){
if (this.serial) {
txByteArray = new Buffer(txBytes)
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
txByteArray = wire.hex2bytes(txBytes.slice(2));
}
r = new msg.buffer(txByteArray)
txValue = wire.decode_big_endian(r, txBytes.length)
if (txValue < this.txCount){
return {"ret_code":6}
}
}
return {"ret_code":0}
}
CounterApp.prototype.get_hash = function(){
2015-12-18 20:44:48 -05:00
this.hashCount += 1;
if (this.txCount == 0){
return {"response": "", "ret_code":0}
}
h = wire.encode_big_endian(this.txCount, 8);
h = wire.reverse(h); // TODO
return {"response": h.toString(), "ret_code":0}
}
CounterApp.prototype.add_listener = function(){
2015-12-18 20:44:48 -05:00
return {"ret_code":0}
}
CounterApp.prototype.rm_listener = function(){
2015-12-18 20:44:48 -05:00
return {"ret_code":0}
}
CounterApp.prototype.event = function(){
2015-12-18 20:44:48 -05:00
}
console.log("Counter app in Javascript")
var app = new CounterApp();
var appServer = new server.AppServer(app);
appServer.server.listen(46658)