Skip to content

Commit eb5ee03

Browse files
authored
Merge pull request UWPCE-PythonCert#6 from johnnzz/project-0308
Project 0308
2 parents 0d61877 + 36081e9 commit eb5ee03

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

students/johnn/project/dcd.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import logging
1414
import optparse
1515
import queue
16+
import socket
1617

1718
#
1819
parser = optparse.OptionParser()
@@ -22,14 +23,21 @@
2223

2324
(options, args) = parser.parse_args()
2425

26+
# hack to figure out my ip address
27+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
28+
s.connect(("8.8.8.8", 80))
29+
myaddr = s.getsockname()[0]
30+
s.close()
31+
2532
if options.admin_port is None:
2633
options.admin_port = "5561"
2734

2835
if options.pub_port is None:
2936
options.pub_port = "5556"
3037

31-
options.admin_interface = "tcp://*:{}".format(options.admin_port)
32-
options.pub_interface = "tcp://*:{}".format(options.pub_port)
38+
options.admin_interface = "tcp://{}:{}".format(myaddr,options.admin_port)
39+
options.pub_interface = "tcp://{}:{}".format(myaddr,options.pub_port)
40+
myinterfaces = ( options.admin_interface, options.pub_interface )
3341

3442
if options.debug is None:
3543
options.stream_log_level = logging.INFO
@@ -71,37 +79,57 @@ def decode_command(message):
7179
command, key, value = (None, None, None)
7280
return ( command, key, value )
7381

82+
def blob(config):
83+
return repr( (myinterfaces, config.data, config.peers) )
84+
7485
def admin(config):
7586
context = zmq.Context()
7687
admin = context.socket(zmq.REP)
7788
admin.bind(options.admin_interface)
7889
log.info("admin bound on {}".format(options.admin_interface))
90+
91+
talkback = context.socket(zmq.REQ)
92+
7993
while True:
8094
command, key, value = decode_command(admin.recv_string())
8195
response = ""
8296
log.info("received command {}, key {}, value {}".format(command, key, value))
8397
if command == "dump":
84-
log.info("DUMP: " + str(config.data))
85-
response = str(config.data)
98+
log.debug("dump request, responding {}".format(blob(config)))
99+
log.info("dump request")
100+
response = blob(config)
101+
admin.send_string(response)
86102
if command == "put":
87103
log.info("putting {} {}".format(key, value))
88104
config.pub_queue.put((key, value))
89105
config.sub_queue.put(key)
90106
response = "ack"
107+
admin.send_string(response)
91108
if command == "get":
92109
log.info("getting {}".format(key))
93110
try:
94111
config.sub_queue.put(key)
95112
value = config.get_value(key)
96-
reponse = value
113+
response = value
114+
admin.send_string(response)
97115
except KeyError:
98116
config.sub_queue.put(key)
99117
response = ""
118+
admin.send_string(response)
100119
if command == "link":
101-
log.info("linking {}".format(value))
102-
config.link_queue.put(value)
103-
response = "ack"
104-
admin.send_string(response)
120+
response = "initiating a link request to remote admin port at {}".format(value)
121+
log.debug(response)
122+
admin.send_string(response)
123+
log.debug("opening connection to " + str(value))
124+
talkback.connect(value)
125+
cmd = ("('dump', None, None)")
126+
log.debug("sending command " + cmd )
127+
talkback.send_string(cmd)
128+
message = talkback.recv_string()
129+
log.debug("got " + str(message) )
130+
remote_ports, data, peers = eval(message)
131+
remote_admin, remote_pub = remote_ports
132+
log.debug("remote_admin {}, remote_pub {}, data {}, peers {}".format(remote_admin, remote_pub, data, peers))
105133

106134
def pub(config):
107135
context = zmq.Context()
@@ -151,6 +179,7 @@ def __init__(self):
151179
self.sub_queue = queue.Queue()
152180
self.link_queue = queue.Queue()
153181
self.data = {}
182+
self.peers = {}
154183
def set_value(self, key, value):
155184
self.data[key] = value
156185
def get_value(self, key):

students/johnn/project/dcli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ parser.add_option("-s", "--server", dest="server",
1515
parser.add_option("-g", "--get", dest="get", action="store_true", help="Send a data query the server")
1616
parser.add_option("-p", "--put", dest="put", action="store_true", help="Set the data value on the server")
1717
parser.add_option("-k", "--key", dest="key", help="key to process")
18+
parser.add_option("-r", "--raw", dest="raw", help="debug only - send raw string to server")
1819
parser.add_option("-v", "--value", dest="value", help="key to process")
1920
parser.add_option("-e", "--echo_test", dest="test", help="Send an echo test to the server")
20-
parser.add_option("-l", "--link", dest="link", action="store_true", help="Link this server to another")
21+
parser.add_option("-l", "--link", dest="link", help="Link this server to another")
2122
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="Print debug information to the screen")
2223
parser.add_option("--dump", dest="dump", action="store_true", help="Show all values in the database")
2324

@@ -83,7 +84,10 @@ if options.test:
8384
send_message(build_message("echo_test", "", options.value))
8485

8586
if options.link:
86-
send_message(build_message("link", "", options.value))
87+
send_message(build_message("link", "", options.link))
8788

8889
if options.dump:
8990
send_message(build_message("dump", "", ""))
91+
92+
if options.raw:
93+
send_message(options.raw)

0 commit comments

Comments
 (0)