+# cache/port.<node_id1>-<node_id2>@<endpoint>:
+# Retrieves the local port of link <node_id1>-<node_id2> on
+# node <node_id<endpoint>>.
+#
+# The local interface must have been created already and the
+# switch daemon must be running. We need to retrieve the port
+# number again if the interface is re-created, or the switch
+# daemon is restarted.
+cache/port.%: cache/iface.% cache/switch.$$(call get,%)
+ @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+ @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
+ sliver-ovs get-local-endpoint L$(call linkpart,$(*F)) > $@ 2> $(call log,$@)
+
+
+# linkid=$(call linkpart,%)
+# nodeid=$(call get,%)
+# iface1=cache/iface.%
+# iface2=cache/iface.$(call opp,%)
+
+# cache/endpoint.<node_id1>-<node_id2>@<endpoint>:
+# Sets the other side (IP address, UDP port) info for link <node_id1>-<node_id2>
+# on <node_id<endpoint>>.
+#
+# We need the IP address and the UDP port of the other side and the interace of this side.
+# We need to set the info again if any of these change.
+cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
+ @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+ @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
+ $$(cat cache/host.$(call rget,$(*F))) \
+ $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@)
+ @touch $@
+
+####################
+CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
+clean: $(CLEANTARGETS)
+distclean:
+ rm -rf L log cache
+.PHONY: clean distclean
+
+####################
+graph.dot:
+ ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
+graph.ps: graph.dot
+ dot -Tps < $^ > $@
+
+####################
+# googlemap stuff
+# Uses:
+# GMAP_SERVER, the hostname that runs an ndnmap instance
+# GMAP_PROBES, a list of tuples of the form
+# <linkid>=<nodeid>=<internal_linkid>=<frequency>
+# e.g.
+# SENDER-MUX=SENDER=3=0.5
+# which would mean, the link 'SENDER-MUX' should be monitored at the 'SENDER' node and is coded
+# in links.json with id=3, every half second
+# sliver-ovs gmap-probe is designed to run in background,
+# and so that a new run will kill any previously running instance
+gprobes: $(foreach probe,$(GMAP_PROBES),gprobe-$(probe))
+
+gprobe-%: FORCE
+ $(SSH) -n $(call solve,$(word 2,$(subst @, ,$(*F)))) $(SUDO) \
+ sliver-ovs gmap-probe L$(word 1,$(subst @, ,$(*F))) $(GMAP_SERVER) $(word 3,$(subst @, ,$(*F))) $(word 4,$(subst @, ,$(*F)))
+
+jsons: routers.json links.json geocode.json
+.PHONE: jsons
+
+routers.json: $(CONF)
+ @{ echo "["; \
+ sep=" "; \
+ for n in $(CONF_NODE_IDS); do \
+ echo " $$sep\"$$n\""; \
+ sep=", "; \
+ done; \
+ echo "]"; \
+ } > $@
+
+links.json: $(foreach l,$(LINKS),cache/json.$(l))
+ @{ echo "["; \
+ cat $^ | sed '2,$$s/^/, /'; \
+ echo "]"; \
+ } > $@
+
+cache/json.%: $(CONF)
+ @echo "{\"id\": $($*_linkid), \"start\": \"$(call get,$*@1)\", \"end\": \"$(call get,$*@2)\"}" > $@
+
+geocode.json: $(foreach n,$(CONF_NODE_IDS),cache/geocode.$(n))
+ @{ echo "{"; \
+ sep=" "; \
+ for f in $^; do \
+ echo -n "$$sep"; \
+ cat $$f; \
+ sep=", "; \
+ done; \
+ echo "}"; \
+ } > $@
+
+cache/geocode.%: cache/loc.% $(CONF)
+ @{ echo "\"$*\": {"; \
+ echo " \"name\": \"$(HOST_$*)\","; \
+ echo " \"shortname\": \"$*\","; \
+ echo " \"site\": \"\","; \
+ echo " \"backbone\": false,"; \
+ echo " \"position\": [ $(shell cat cache/loc.$*) ]"; \
+ echo " }"; \
+ } > $@
+
+cache/loc.%: $(CONF)
+ @wget -O - 'http://freegeoip.net/csv/$(HOST_$*)' | \
+ awk -F, '{ printf "%s, %s\n", $$8, $$9 }' > $@