[PATCH] fff-hoods: Use alfred for hood file redistribution

Adrian Schmutzler freifunk at adrianschmutzler.de
Mi Nov 1 23:44:39 CET 2017


Please read inline comments. UNTESTED.

Signed-off-by: Adrian Schmutzler <freifunk at adrianschmutzler.de>
---
 .../fff-hoods/files/lib/functions/fff/keyxchange   | 28 ++++++++++++++++++
 .../fff/fff-hoods/files/usr/sbin/configurehood     | 34 ++++++++++++++++++++--
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/src/packages/fff/fff-hoods/files/lib/functions/fff/keyxchange b/src/packages/fff/fff-hoods/files/lib/functions/fff/keyxchange
index 9aeb434..2d645d0 100644
--- a/src/packages/fff/fff-hoods/files/lib/functions/fff/keyxchange
+++ b/src/packages/fff/fff-hoods/files/lib/functions/fff/keyxchange
@@ -31,3 +31,31 @@ getUpgradePath() {
 	fi
 	return 0
 }
+
+getJsonTimestamp() {
+	json=$1
+	
+	ts=0
+	if [ -n "$json" ] && json_load "$json" &>/dev/null ; then
+		json_select "hood" &>/dev/null && json_get_var ts timestamp
+	fi
+	echo "$ts"
+}
+
+getJsonFromAlfred() {
+	datafile=$1
+
+	tsold=0
+	jsonused=""
+
+	while read -r line
+	do
+		json="$(echo "$line" | sed 's/[^,]*,\s*"//' | sed 's/"\s*},\s*$//' | sed 's/\\"/"/g')"
+		ts="$(getJsonTimestamp "$json")"
+		if [ "$ts" -gt "$tsold" ] ; then
+			jsonused="$json"
+			tsold="$ts"
+		fi
+	done < "$datafile"
+	echo "$jsonused"
+}
diff --git a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
index 8ffaced..981f7aa 100755
--- a/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
+++ b/src/packages/fff/fff-hoods/files/usr/sbin/configurehood
@@ -9,6 +9,9 @@ sectorlocal=/etc/sectorfile
 sectortmp=/tmp/sectorfile
 sectorcopy=/www/hood/sectorfile
 
+alfredfile=/tmp/alfredhood
+alfrednum="65"
+
 rm -f "$hoodfile"
 
 # Gatewaycheck function
@@ -65,6 +68,29 @@ long=$(uci -q get system. at system[0].longitude)
 # if we have Internet, we download the Hoodfile from the keyxchangev2
 if hasInternet ; then
 	wget -T15 -t5 "http://keyserver.freifunk-franken.de/v2/?lat=$lat&long=$long" -O "$hoodfile"
+	
+	# Alfred will always have only one "entry" per sender address. Thus, old data will be overwritten by new one.
+	# If the commented lines are enabled, the behavior will be altered: The router will only send data if no up-to-date information is already present.
+	
+	#hoodread="$(cat "$hoodfile")"
+	#alfred -r "$alfrednum" > "$alfredfile"
+	#alfrednewest="$(getJsonFromAlfred "$alfredfile")"
+	#rm -f "$alfredfile"
+	#tsinet="$(getJsonTimestamp "$hoodread")"
+	#tsalfred="$(getJsonTimestamp "$alfrednewest")"
+	#if [ "$tsinet" -gt "$tsalfred" ] ; then
+		cat "$hoodfile" | alfred -s "$alfrednum"
+	#fi
+	
+	# Note that data sent this way has a lifetime. The following case would be possible:
+	# 1. The first uplink router will send data via alfred
+	# 2. All routers won't send any data, since the data from the first is already present and up-to-date
+	# 3. The alfred data send by the first router expires -> No alfred data present
+	# 4. A random router will find no data and paste its own, restarting the cycle
+	# Note that between 3. and 4. there is a gap where no data is present.
+	# -> Advantage: The alfred information will be much smaller, leading to smaller files and less CPU for parsing (json = about 1 kB per Sender)
+	# -> Disadvantage: There will be frequent gaps with no data available
+
 	rm -f "$sectortmp"
 	[ -s "$sectorlocal" ] || rm -f "$sectorcopy" # If internet present, no custom config should be distributed, except when local file is present
 	#UPLINK: No uplink download if internet present
@@ -115,8 +141,12 @@ else
 			wget -T15 -t5 "http://[fe80::1%w2sta]:2342/sectorfile" -O "$sectortmp" || wget -T15 -t5 "http://[fe80::1%w5sta]:2342/sectorfile" -O "$sectortmp"
 		fi
 	else
-		echo "We have a Gateway in Range, we load the keyxchangev2data from fe80::1"
-		wget -T15 -t5 "http://[fe80::1%br-mesh]:2342/keyxchangev2data" -O "$hoodfile"
+		echo "We have a Gateway in Range, we collect files from the neighborhood"
+		alfred -r "$alfrednum" > "$alfredfile"
+		alfrednewest="$(getJsonFromAlfred "$alfredfile")"
+		rm -f "$alfredfile"
+		[ -n "$alfrednewest" ] && echo "$alfrednewest" > "$hoodfile"
+		
 		#UPLINK: Do nothing
 	fi
 fi
-- 
2.7.4



Mehr Informationen über die Mailingliste franken-dev