Browse Source

Allow to request only a certain family of targets

Baptiste Jonglez 10 years ago
parent
commit
dfe81db7d2
2 changed files with 30 additions and 12 deletions
  1. 28 10
      peerfinder.py
  2. 2 2
      scripts/run.sh

+ 28 - 10
peerfinder.py

@@ -110,6 +110,22 @@ def init_db():
     db.create_all()
 
 
+def get_targets(uuid):
+    """Returns the queryset of potential targets for the given participant
+    UUID, that is, targets that have not already been handed out to this
+    participant.
+    """
+    participant = Participant.query.filter_by(uuid=uuid, active=True).first_or_404()
+    # We want to get all targets that do not have a relationship with the
+    # given participant.  Note that the following lines manipulate SQL
+    # queries, which are only executed at the very end.
+    # This gives all targets that have already been sent to the given
+    # participant.
+    already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
+    # This takes the negation of the previous set.
+    return Target.query.filter(~Target.id.in_(already_done))
+
+
 @app.route('/')
 def homepage():
     return render_template('home.html')
@@ -137,21 +153,23 @@ def create_participant():
 @app.route('/target/<uuid>')
 def get_next_target(uuid):
     """"Returns the next target to ping for the given participant"""
-    participant = Participant.query.filter_by(uuid=uuid, active=True).first_or_404()
-    # We want to get all targets that do not have a relationship with the
-    # given participant.  Note that the following lines manipulate SQL
-    # queries, which are only executed at the very end.
-    # This gives all targets that have already been sent to the given
-    # participant.
-    already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
-    # This takes the negation of the previous set.
-    todo = Target.query.filter(~Target.id.in_(already_done))
-    target = todo.first()
+    target = get_targets(uuid).first()
     if target is not None:
         return "{} {}".format(target.id, target)
     else:
         return ""
 
+@app.route('/target/<uuid>/<family>')
+def get_next_target_family(uuid, family):
+    """Same as above, but for a specific family ("ipv4" or "ipv6")"""
+    if family not in ("ipv4", "ipv6"):
+        return "Invalid family, should be ipv4 or ipv6\n"
+    predicate = lambda t: t.is_v4() if family == "ipv4" else t.is_v6()
+    targets = [t for t in get_targets(uuid).all() if predicate(t)]
+    if not targets:
+        return ""
+    return "{} {}".format(targets[0].id, targets[0])
+
 @app.route('/result/report/<uuid>', methods=['POST'])
 def report_result(uuid):
     if {'rtt', 'target'}.issubset(request.form):

+ 2 - 2
scripts/run.sh

@@ -4,7 +4,7 @@
 UUID="00000000-0000-0000-0000-000000000000"
 PEERFINDER="http://172.23.184.112:8888"
 NB_PINGS=5
-SLEEP=20
+SLEEP=10
 
 [ -z "$1" ] && FAMILY="ipv4" || FAMILY="ipv6"
 
@@ -13,7 +13,7 @@ SLEEP=20
 while :
 do
     sleep "$SLEEP"
-    curl "$PEERFINDER"/target/"$UUID" > /tmp/"$FAMILY" 2> /dev/null
+    curl "$PEERFINDER"/target/"$UUID"/"$FAMILY" > /tmp/"$FAMILY" 2> /dev/null
     read -r id ip < /tmp/"$FAMILY"
     # Avoid empty fields
     [ -z "$id" -o -z "$ip" ] && continue