浏览代码

Big block of work done over however long it has been since I last did a commit + fixes around AIP and peer info

Billy Barrow 3 年之前
父节点
当前提交
56c272bd46

+ 13 - 2
src/lib/Networks/PeerInfo.vala

@@ -9,7 +9,7 @@ namespace LibPeer.Networks
 
         private static ConcurrentHashMap<Bytes, Type> info_types = new ConcurrentHashMap<Bytes, Type>((a) => a.hash(), (a, b) => a.compare(b) == 0);
         
-        protected abstract void build(uint8 data_length, InputStream stream) throws IOError, Error;
+        protected abstract void build(uint8 data_length, InputStream stream, Bytes network_type) throws IOError, Error;
         
         public abstract Bytes get_network_identifier();
 
@@ -25,22 +25,29 @@ namespace LibPeer.Networks
             // Create a stream writer
             var writer = new DataOutputStream(stream);
             writer.byte_order = DataStreamByteOrder.BIG_ENDIAN;
+            print("Start serialising PeerInfo\n");
 
             // Get the informational data
             var type = get_network_identifier();
             var data = get_data_segment();
 
+            print("Serialising type length\n");
             // Write the length of the network type
             writer.put_byte((uint8)type.length);
 
+            print("Serialising data segment length\n");
             // Write the length of the data segment
             writer.put_byte((uint8)data.length);
 
+            var stringType = new ByteComposer().add_bytes(type).to_string(true);
+            print(@"Serialising type: $(stringType) ($(to_string()))\n");
             // Write the network identifier
             writer.write_bytes(type);
 
+            print("Serialising data\n");
             // Write the data
             writer.write_bytes(data);
+            print("Serialised peer info\n");
         }
         
         public static PeerInfo deserialise(InputStream stream) throws IOError, Error {
@@ -67,12 +74,16 @@ namespace LibPeer.Networks
             PeerInfo peer_info = Object.new(peer_info_type) as PeerInfo;
 
             // Build out the data
-            peer_info.build(data_length, reader);
+            peer_info.build(data_length, reader, network_type);
 
             // Return the object
             return peer_info;
         }
 
+        protected void register_info_type() {
+            info_types.set(get_network_identifier(), get_type());
+        }
+
     }
 
 }

+ 3 - 2
src/lib/Networks/Simulation/NetSimPeerInfo.vala

@@ -10,6 +10,7 @@ namespace LibPeer.Networks.Simulation {
         internal uint8[] identifier = new uint8[IDENTIFIER_SIZE];
 
         internal NetSimPeerInfo(Bytes identifier) {
+            register_info_type();
             this.identifier = identifier.get_data();
         }
 
@@ -17,7 +18,7 @@ namespace LibPeer.Networks.Simulation {
             return new Bytes({'N', 'e', 't', 'S', 'i', 'm'});
         }
 
-        protected override void build(uint8 data_length, InputStream stream) throws Error {
+        protected override void build(uint8 data_length, InputStream stream, Bytes network_type) throws Error {
             identifier = stream.read_bytes(16).get_data();
         }
 
@@ -44,7 +45,7 @@ namespace LibPeer.Networks.Simulation {
         }
 
         public override string to_string() {
-            return "";
+            return @"NetSim [$(identifier[0]), $(identifier[1]), $(identifier[2]), $(identifier[3])]";
         }
     }
 

+ 4 - 2
src/lib/Networks/UnknownPeerInfo.vala

@@ -8,13 +8,15 @@ namespace LibPeer.Networks
     public class UnknownPeerInfo : PeerInfo {
 
         private Bytes information;
+        private Bytes network_identifier;
 
         public override GLib.Bytes get_network_identifier () {
-            return new Bytes({});
+            return new Bytes(network_identifier.get_data());
         }
 
-        protected override void build(uint8 data_length, InputStream stream) throws Error {
+        protected override void build(uint8 data_length, InputStream stream, Bytes network_type) throws Error {
             information = stream.read_bytes(data_length);
+            network_identifier = network_type;
         }
 
         protected override Bytes get_data_segment() {

+ 9 - 5
src/lib/Protocols/AIP/ApplicationInformationProtocol.vala

@@ -53,7 +53,7 @@ namespace LibPeer.Protocols.Aip {
         protected TimeoutMap<InstanceReference, Bytes> pending_group_peers = new TimeoutMap<InstanceReference, Bytes>(120, (a) => a.hash(), (a, b) => a.compare(b) == 0);
         public signal void ready();
 
-        private GLib.List<Query> pending_queries = new GLib.List<Query>();
+        private Gee.List<Query> pending_queries = new Gee.LinkedList<Query>();
 
         public ApplicationInformationProtocol(Muxer muxer, AipCapabilities? capabilities = null, bool join_all = false) {
             if(capabilities == null) {
@@ -194,10 +194,11 @@ namespace LibPeer.Protocols.Aip {
             peer_info.add(info);
             
             // Do we have any pending queries?
-            if(pending_queries.length() > 0) {
+            if(pending_queries.size > 0) {
+                print("Sending pending queries");
                 // Clear the list
-                var queries = pending_queries.copy();
-                pending_queries = null;
+                var queries = pending_queries;
+                pending_queries = new Gee.LinkedList<Query>();
 
                 // Send pending queries
                 foreach (var query in queries) {
@@ -483,12 +484,13 @@ namespace LibPeer.Protocols.Aip {
             print("Queue query answer\n");
             // Do we have peer info to send yet?
             if(peer_info.size > 0) {
+                print("Query sent immediately\n");
                 // Yes, do it
                 send_query_answer(query);
             }
             else {
                 // No, wait for peer info
-                pending_queries.append(query);
+                pending_queries.add(query);
             }
         }
 
@@ -498,6 +500,7 @@ namespace LibPeer.Protocols.Aip {
 
             // Serialise the info
             MemoryOutputStream stream = new MemoryOutputStream(null, GLib.realloc, GLib.free);
+            print("Serialising instance info\n");
             instance_info.serialise(stream);
             stream.close();
             uint8[] buffer = stream.steal_data();
@@ -615,6 +618,7 @@ namespace LibPeer.Protocols.Aip {
 
             // Open a stream with the instance
             transport.initialise_stream(send_to).established.connect(stream => {
+                print("Writing answer to stream\n");
                 // Tell the instance that the data that follows is an answer
                 stream.write(new uint8[] { DATA_FOLLOWING_ANSWER });
 

+ 1 - 0
src/lib/Protocols/AIP/InstanceInformation.vala

@@ -24,6 +24,7 @@ namespace LibPeer.Protocols.Aip {
             // Write number of connection methods
             dos.put_byte((uint8)connection_methods.length);
 
+            print("Connection methods\n");
             // Write connection methods
             foreach (var method in connection_methods) {
                 method.serialise(dos);

+ 1 - 1
src/lib/Protocols/STP/Streams/InputStream.vala

@@ -48,7 +48,7 @@ namespace LibPeer.Protocols.Stp.Streams {
                 data_cond.wait(data_mutex);
             }
             var available_data = unread_data.length < buffer.length ? unread_data.length : buffer.length;
-            print(@"Read $(available_data) of $(buffer.length) bytes\n");
+            //  print(@"Read $(available_data) of $(buffer.length) bytes\n");
             for(int i = 0; i < available_data; i++) {
                 buffer[i] = unread_data[i];
             }