You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.5 KiB

9 years ago
  1. wire = require("./wire")
  2. module.exports = {
  3. types : {
  4. 0x01 : "echo",
  5. 0x02 : "flush",
  6. 0x03 : "info",
  7. 0x04 : "set_option",
  8. 0x21 : "append_tx",
  9. 0x22 : "get_hash",
  10. 0x23 : "commit",
  11. 0x24 : "rollback",
  12. 0x25 : "add_listener",
  13. 0x26 : "rm_listener",
  14. },
  15. decoder : RequestDecoder,
  16. buffer: BytesBuffer
  17. }
  18. function RequestDecoder(buf){
  19. this.buf= buf
  20. }
  21. var decode_string = wire.decode_string
  22. // return nothing, one thing, or a list of things
  23. RequestDecoder.prototype.echo = function(){ return decode_string(this.buf) };
  24. RequestDecoder.prototype.flush = function(){};
  25. RequestDecoder.prototype.info = function(){};
  26. RequestDecoder.prototype.set_option = function(){ return [decode_string(this.buf), decode_string(this.buf)] };
  27. RequestDecoder.prototype.append_tx = function(){ return decode_string(this.buf)};
  28. RequestDecoder.prototype.get_hash = function(){ };
  29. RequestDecoder.prototype.commit = function(){ };
  30. RequestDecoder.prototype.rollback = function(){ };
  31. RequestDecoder.prototype.add_listener = function(){ }; // TODO
  32. RequestDecoder.prototype.rm_listener = function(){ }; // TODO
  33. // buffered reader with read(n) method
  34. function BytesBuffer(buf){
  35. this.buf = buf
  36. }
  37. BytesBuffer.prototype.read = function(n){
  38. b = this.buf.slice(0, n)
  39. this.buf = this.buf.slice(n)
  40. return b
  41. };
  42. BytesBuffer.prototype.write = function(buf){
  43. this.buf = Buffer.concat([this.buf, buf]);
  44. };
  45. BytesBuffer.prototype.size = function(){
  46. return this.buf.length
  47. }
  48. BytesBuffer.prototype.peek = function(){
  49. return this.buf[0]
  50. }