-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccn_management.py
118 lines (100 loc) · 4.95 KB
/
ccn_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python3.5
import subprocess
import time
from readers import read_face
faces_ids = {}
# Open a relay for ccn-lite in the background
def open_relay():
close_relay()
print("Opening relay...")
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-relay -v trace -s ndn2013 -u 9998 -x /tmp/mgmt-relay.sock -d /home/pi/ccn-lite/test/ndntlv > /home/pi/ccn.log 2>&1 &"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return
# Close a relay for ccn-lite
def close_relay():
delete_sockets()
print("Closing relay...")
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock debug halt | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml > /home/pi/ccn-closing.log 2>&1 &"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return
# Automatically restart relay
def restart_relay():
time.sleep(1)
close_relay()
time.sleep(1)
open_relay()
# Delete the old sockets
def delete_sockets():
print("Deleting temporary sockets...")
bash_command = "rm /tmp/mgmt-relay* &"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
bash_command = "rm /tmp/.ccn-light-ctrl-* &"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return
# Create face based on address
def add_face(address):
#print("Adding face for: "+address)
node = address.split("168.1.")[1]
bash_command = "FACEID=$(/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock newUDPface any " + address + " 9998 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml | grep FACEID | sed -e 's/^[^0-9]*\([0-9]\+\).*/\1/')"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock debug dump | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml > /home/pi/face_dump.log 2>&1"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
face_id = read_face()
if face_id is None:
return
delete_face(node)
time.sleep(1)
faces_ids[node] = face_id
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock prefixreg /node" + node + " " + face_id + " ndn2013 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return
# Create face for dtn
def add_dtn_face(address):
#print("Adding face for: "+address)
node = address.split("168.1.")[1]
bash_command = "FACEID=$(/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock newUDPface any 127.0.0.1 6666 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml | grep FACEID | sed -e 's/^[^0-9]*\([0-9]\+\).*/\1/')"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock debug dump | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml > /home/pi/face_dump.log 2>&1"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
face_id = read_face()
if face_id is None:
return
delete_face(node)
time.sleep(1)
faces_ids[node] = face_id
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock prefixreg /node" + node + " " + face_id + " ndn2013 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return
# Create face of node via a different address
def add_other_face(node, address):
#print("Adding neighbor: " + node)
bash_command = "FACEID=$(/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock newUDPface any " + address + " 9998 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml | grep FACEID | sed -e 's/^[^0-9]*\([0-9]\+\).*/\1/')"
#print("Adding forwarding rule through: " + address)
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock debug dump | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml > /home/pi/face_dump.log 2>&1"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
face_id = read_face()
if face_id is None:
return
delete_face(node)
time.sleep(1)
faces_ids[node] = face_id
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock prefixreg /node" + node + " " + face_id + " ndn2013 | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
#print("Added neighbor: " + node + " through: " + address)
return
# Delete a face of a node -NOT address
def delete_face(node):
if node in faces_ids:
face_to_delete = faces_ids[node]
else:
return
bash_command = "/home/pi/ccn-lite/build/bin/ccn-lite-ctrl -x /tmp/mgmt-relay.sock destroyface " + face_to_delete + " | /home/pi/ccn-lite/build/bin/ccn-lite-ccnb2xml"
subprocess.Popen(bash_command, stdout=subprocess.PIPE, shell=True)
return