Last active
December 15, 2015 17:39
-
-
Save rossdylan/5297592 to your computer and use it in GitHub Desktop.
CSH xkcd hashing cluster master server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import zmq | |
| import requests | |
| import random | |
| import sys | |
| def gen_seed(rand, host): | |
| data = rand.randint(0,hash(host) % sys.maxint) | |
| print data | |
| return data | |
| class CSHAlma(object): | |
| def __init__(self, domain, pub_port, rep_port): | |
| self.domain = domain | |
| self.pub_port = pub_port | |
| self.rep_port = rep_port | |
| self.context = zmq.Context() | |
| self.pub_socket = self.context.socket(zmq.PUB) # clients ask for data | |
| self.rep_socket = self.context.socket(zmq.REP) | |
| self.best_score = 450 | |
| self.best_text = "" | |
| self.nodes = {} | |
| self.rand = random.Random() | |
| def start(self): | |
| self.pub_socket.bind("tcp://*:{0}".format(self.pub_port)) | |
| self.rep_socket.bind("tcp://*:{0}".format(self.rep_port)) | |
| print("Listening on {0} and {1}".format(self.pub_port, self.rep_port)) | |
| poller = zmq.Poller() | |
| poller.register(self.rep_socket, zmq.POLLIN) | |
| while True: | |
| socks = dict(poller.poll()) | |
| if socks.get(self.rep_socket) == zmq.POLLIN: | |
| message = self.rep_socket.recv() | |
| print message | |
| if message.startswith("register"): | |
| data = message.split(" ") | |
| host = data[1] | |
| seed = gen_seed(self.rand, host) | |
| self.rep_socket.send(str(seed) + " " + str(self.best_score)) | |
| self.nodes[host] = seed | |
| print("Node '{0}' has registered".format(host)) | |
| else: | |
| data = message.split(" ") | |
| score = int(data[1]) | |
| if score < self.best_score: | |
| self.best_score = score | |
| self.best_text = data[0] | |
| print("New Best: {0} - '{1}'".format(self.best_score, self.best_text)) | |
| requests.post("http://almamater.xkcd.com/?edu={0}".format(self.domain), {'hashable': self.best_text}) | |
| self.pub_socket.send("best " + str(self.best_score)) | |
| self.rep_socket.send("ok") | |
| def main(): | |
| if len(sys.argv) == 4: | |
| domain = sys.argv[1] | |
| pub_port = int(sys.argv[2]) | |
| rep_port = int(sys.argv[3]) | |
| print(domain) | |
| print(pub_port) | |
| print(rep_port) | |
| master = CSHAlma(domain, pub_port, rep_port) | |
| master.start() | |
| else: | |
| print("Usage: cshalma domain pub_port rep_port") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment