Przeglądaj źródła

big lambda probelms

Billy Barrow 4 lat temu
rodzic
commit
2dc25d77a0

+ 1 - 1
src/lib/Protocols/AIP/Answer.vala

@@ -3,7 +3,7 @@ using Gee;
 
 namespace LibPeer.Protocols.Aip {
 
-    internal class Answer {
+    public class Answer {
 
         public Bytes in_reply_to { get; set; }
 

+ 7 - 0
src/lib/Protocols/AIP/ApplicationInformation.vala

@@ -19,6 +19,13 @@ namespace LibPeer.Protocols.Aip {
             instance = iref;
             application_namespace = app_namespace;
         }
+
+        public ApplicationInformation.from_instance(Instance instance) {
+            this.instance = instance.reference;
+            application_namespace = instance.application_namespace;
+        }
+
+        public signal void new_group_peer();
     }
 
 }

+ 54 - 8
src/lib/Protocols/AIP/ApplicationInformationProtocol.vala

@@ -7,7 +7,7 @@ using Gee;
 
 namespace LibPeer.Protocols.Aip {
 
-    class ApplicationInformationProtocol {
+    public class ApplicationInformationProtocol {
 
         internal const uint8 DATA_FOLLOWING_REQUEST = 'R';
         internal const uint8 DATA_FOLLOWING_QUERY = 'Q';
@@ -84,6 +84,52 @@ namespace LibPeer.Protocols.Aip {
             network.advertise(instance.reference);
         }
 
+        public void add_application(ApplicationInformation info) {
+            // Save reference to application
+            application_information.add(info);
+
+            // Join group for this application
+            join_query_group(info.namespace_bytes);
+
+            // Hook up signals
+            new_group_peer.connect((instance_ref, id) => {
+                if(id.compare(info.namespace_bytes) == 0) {
+                    info.new_group_peer();
+                }
+            });
+        }
+
+        public Query find_application_instance(ApplicationInformation app) {
+            // We must be in a query group for this application
+            assert(query_groups.has_key(app.namespace_bytes));
+
+            // Create the query
+            var query = new Query(new ByteComposer().add_byte(QUERY_APPLICATION).add_bytes(app.namespace_bytes).to_bytes());
+
+            // Send the query
+            initiate_query(query, query_groups.get(app.namespace_bytes));
+
+            // Return the query
+            return query;
+        }
+
+        public Query find_application_resource(ApplicationInformation app, Bytes resource_identifier) {
+            // We must be in a query group for this application
+            assert(query_groups.has_key(app.namespace_bytes));
+
+            // Resource identifiers must be 32 bytes long
+            assert(resource_identifier.length == 32);
+
+            // Create the query
+            var query = new Query(new ByteComposer().add_byte(QUERY_APPLICATION_RESOURCE).add_bytes(resource_identifier).add_bytes(app.namespace_bytes).to_bytes());
+
+            // Send the query
+            initiate_query(query, query_groups.get(app.namespace_bytes));
+
+            // Return the query
+            return query;
+        }
+
         protected void rx_advertisement(Advertisement advertisement) {
             // Send an inquiry
             muxer.inquire(instance, advertisement.instance_reference, new PeerInfo[] { advertisement.peer_info });
@@ -395,7 +441,7 @@ namespace LibPeer.Protocols.Aip {
             
         }
 
-        public void queue_query_answer(Query query) {
+        protected void queue_query_answer(Query query) {
             // Do we have peer info to send yet?
             if(peer_info.size > 0) {
                 // Yes, do it
@@ -407,7 +453,7 @@ namespace LibPeer.Protocols.Aip {
             }
         }
 
-        public void send_query_answer(Query query) {
+        protected void send_query_answer(Query query) {
             // Create some instance information
             var instance_info = new InstanceInformation(instance.reference, peer_info.to_array());
 
@@ -429,7 +475,7 @@ namespace LibPeer.Protocols.Aip {
             send_answer(answer);
         }
 
-        public void join_query_group(Bytes group) {
+        protected void join_query_group(Bytes group) {
             // Create the query group
             query_groups.set(group, new QueryGroup());
 
@@ -444,7 +490,7 @@ namespace LibPeer.Protocols.Aip {
             }
         }
 
-        public void send_group_query(Bytes group) {
+        protected void send_group_query(Bytes group) {
             // Construct a query asking for peers in the group
             var query = new Query(new ByteComposer().add_byte(QUERY_GROUP).add_bytes(group).to_bytes());
 
@@ -471,7 +517,7 @@ namespace LibPeer.Protocols.Aip {
             initiate_query(query, default_group);
         }
 
-        public void initiate_query(Query query, QueryGroup group) {
+        protected void initiate_query(Query query, QueryGroup group) {
             // Save a reference to the query
             queries.set(query.identifier, query);
             handled_query_ids.add(query.identifier);
@@ -480,7 +526,7 @@ namespace LibPeer.Protocols.Aip {
             send_query(query, group);
         }
 
-        public void send_query(Query query, QueryGroup group) {
+        protected void send_query(Query query, QueryGroup group) {
             // Does the query have any hops left?
             if(query.hops > MAX_QUERY_HOPS) {
                 return;
@@ -501,7 +547,7 @@ namespace LibPeer.Protocols.Aip {
             }
         }
 
-        public void send_answer(Answer answer) {
+        protected void send_answer(Answer answer) {
             // Get (and remove) the last item from the path list
             var send_to = answer.path[answer.path.length-1];
             answer.path.length --;

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

@@ -3,7 +3,7 @@ using LibPeer.Networks;
 
 namespace LibPeer.Protocols.Aip {
 
-    internal class InstanceInformation {
+    public class InstanceInformation {
 
         public InstanceReference instance_reference { get; private set; }
 

+ 7 - 7
src/lib/Protocols/AIP/Query.vala

@@ -3,17 +3,17 @@ using Gee;
 
 namespace LibPeer.Protocols.Aip {
 
-    internal class Query {
+    public class Query {
 
-        public Bytes identifier { get; set; }
+        public Bytes identifier { get; internal set; }
 
-        public Bytes data { get; set; }
+        public Bytes data { get; internal set; }
 
-        public uint8 max_replies { get; set; }
+        public uint8 max_replies { get; internal set; }
 
-        public uint8 hops { get; set; }
+        public uint8 hops { get; internal set; }
 
-        public InstanceReference[] return_path { get; set; }
+        public InstanceReference[] return_path { get; internal set; }
 
         public signal void on_answer(InstanceInformation answer);
 
@@ -62,7 +62,7 @@ namespace LibPeer.Protocols.Aip {
             data = stream.read_bytes(data_length);
         }
 
-        public void append_return_hop(InstanceReference instance) {
+        internal void append_return_hop(InstanceReference instance) {
             var paths = return_path;
             return_path = new InstanceReference[paths.length + 1];
             return_path[paths.length] = instance;

+ 1 - 1
src/lib/Protocols/AIP/QueryGroup.vala

@@ -3,7 +3,7 @@ using Gee;
 
 namespace LibPeer.Protocols.Aip {
 
-    internal class QueryGroup {
+    public class QueryGroup {
 
         private HashSet<InstanceReference> instances = new HashSet<InstanceReference>((a) => a.hash(), (a, b) => a.compare(b) == 0);
         private int target;

+ 41 - 0
src/toys/discoverer/Discoverer.vala

@@ -0,0 +1,41 @@
+using LibPeer.Networks.Simulation;
+using LibPeer.Protocols.Mx2;
+using LibPeer.Protocols.Aip;
+using LibPeer.Networks;
+
+using Gee;
+
+namespace Discoverer {
+
+    class DiscoverWorker : Object {
+
+        private Muxer muxer = new Muxer();
+        private Network network;
+        private ApplicationInformationProtocol aip;
+        private ApplicationInformation app_info;
+        private Instance app_instance;
+        private int id;
+
+        public DiscoverWorker(int id, Conduit conduit) throws Error, IOError {
+            this.id = id;
+            network = conduit.get_interface();
+            network.bring_up();
+            aip = new ApplicationInformationProtocol(muxer);
+            aip.add_network(network);
+
+            app_instance = new Instance("discovery_toy");
+            app_info = new ApplicationInformation.from_instance(app_instance);
+            app_info.new_group_peer.connect(group_peers_found);
+            aip.add_application (app_info);
+        }
+
+        private void group_peers_found() {
+            aip.find_application_instance(app_info).on_answer.connect(found_peer);
+        }
+
+        private void found_peer(InstanceInformation info) {
+            print("I found a peer!\n");
+        }
+
+    }
+}

+ 24 - 0
src/toys/discoverer/Main.vala

@@ -0,0 +1,24 @@
+using LibPeer.Networks.Simulation;
+
+namespace Discoverer {
+
+    class Main : Object {
+
+        public static int main(string[] args) {
+            print("Discoverer\n");
+            int count = int.parse(args[1]);
+
+            Conduit conduit = new Conduit();
+
+            DiscoverWorker[] pingas = new DiscoverWorker[count];
+            for (int i = 0; i < count; i++){
+                pingas[i] = new DiscoverWorker(i, conduit);
+            }
+
+            while(true) {};
+
+            return 0;
+        }
+    }
+
+}

+ 12 - 0
src/toys/discoverer/meson.build

@@ -0,0 +1,12 @@
+dependencies = [
+    dependency('glib-2.0'),
+    dependency('gobject-2.0'),
+    dependency('gio-2.0'),
+    dependency('gee-0.8'),
+    libpeer_dep
+]
+
+sources = files('Main.vala')
+sources += files('Discoverer.vala')
+
+executable('discoverer', sources, dependencies: dependencies)

+ 2 - 1
src/toys/meson.build

@@ -1,3 +1,4 @@
 
 subdir('exponential_pinger')
-subdir('give_file')
+subdir('give_file')
+subdir('discoverer')