Query.vala 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using LibPeer.Protocols.Mx2;
  2. using Gee;
  3. namespace LibPeer.Protocols.Aip {
  4. public class Query {
  5. public Bytes identifier { get; internal set; }
  6. public Bytes data { get; internal set; }
  7. public uint8 max_replies { get; internal set; }
  8. public uint8 hops { get; internal set; }
  9. public InstanceReference[] return_path { get; internal set; }
  10. public signal void on_answer(InstanceInformation answer);
  11. public void serialise(OutputStream stream) throws IOError, Error {
  12. var dos = new DataOutputStream(stream);
  13. dos.byte_order = DataStreamByteOrder.BIG_ENDIAN;
  14. // Write query identifier
  15. dos.write_bytes(identifier);
  16. // Send header data
  17. dos.put_byte(hops);
  18. dos.put_byte(max_replies);
  19. dos.put_uint16((uint16)data.length);
  20. dos.put_byte((uint8)return_path.length);
  21. // Serialise the return path
  22. foreach (var reference in return_path) {
  23. reference.serialise(dos);
  24. }
  25. // Write the query data
  26. dos.write_bytes(data);
  27. }
  28. public Query.from_stream(InputStream stream) throws IOError, Error{
  29. var dis = new DataInputStream(stream);
  30. dis.byte_order = DataStreamByteOrder.BIG_ENDIAN;
  31. // Read the identifier
  32. identifier = dis.read_bytes(16);
  33. // Read header data
  34. hops = dis.read_byte();
  35. max_replies = dis.read_byte();
  36. var data_length = dis.read_uint16();
  37. var return_path_size = dis.read_byte();
  38. // Deserialise return path
  39. return_path = new InstanceReference[return_path_size];
  40. for(var i = 0; i < return_path_size; i++) {
  41. return_path[i] = new InstanceReference.from_stream(dis);
  42. }
  43. // Read the query data
  44. data = stream.read_bytes(data_length);
  45. }
  46. internal void append_return_hop(InstanceReference instance) {
  47. var paths = return_path;
  48. return_path = new InstanceReference[paths.length + 1];
  49. return_path[paths.length] = instance;
  50. }
  51. public Query(Bytes data, uint8 max_replies = 10, uint8 hops = 0, InstanceReference[] return_path = new InstanceReference[0], Bytes? identifier = null) {
  52. if(identifier == null) {
  53. uint8[] uuid = new uint8[16];
  54. UUID.generate_random(uuid);
  55. this.identifier = new Bytes(uuid);
  56. }
  57. this.data = data;
  58. this.max_replies = max_replies;
  59. this.hops = hops;
  60. this.return_path = return_path;
  61. }
  62. }
  63. }