コード例 #1
0
ファイル: swarming.py プロジェクト: bernard357/swarming
 def loop(self):
     self.ping = Ping('free.fr', 'yahoo.fr', 'voila.fr', 'www.doctissimo.fr')
     while True:
         try:
             self.lazy_loop()
             self.ping.lazy_start()
             r = self.ping.poll()
             if r is not None:
                 success, target, message = r
                 self.publish('ping/%s' % target, json.dumps(
                     [self.name, success, message]), qos=1)
             print ".",
         except socket.error as e:
             print "oups", e
コード例 #2
0
ファイル: swarming.py プロジェクト: bernard357/swarming
class MetaClient(object):

    def __init__(self, servers, name=None):
        self.client = None
        self.channels = set()
        self.setServers(servers)
        if name is None:
            self.name = socket.gethostname()
        else:
            self.name = name

    def setServers(self, servers):
        random.shuffle(servers)
        self.servers = servers
        self.n_server = 0

    def reconnect(self):
        server = self.servers[self.n_server]
        self.n_server = (self.n_server + 1) % len(self.servers)
        a = server.split(":")
        if len(a) == 1:
            ip, port = a[0], 1883
        else:
            ip, port = a[0], int(a[1])
        hostname = socket.gethostname()
        self.client = paho.Client('ping/%s' % hostname, False)
        self.client.will_set('rip/agent/%s' % hostname, qos=1)
        self.client.on_connect = self.on_connect
        #self.client.reconnect_delay_set(10, 3600, True)
        self.client.on_message = self.on_message
        self.client.on_disconnect = self.on_disconnect
        print "try to connect to", ip, port
        self.client.connect(ip, port=port)

    def lazy_loop(self):
        if self.client is None or self.client._sock is None:
            self.reconnect()
        self.client.loop()

    def subscribe(self, path):
        self.channels.add(path)

    def publish(self, topic, payload=None, qos=0, retain=False):
        self.client.publish(topic, payload=payload, qos=qos, retain=retain)

    def on_disconnect(self, mosq, obj, rc):
        print "disconnect", mosq

    def on_connect(self, mosq, obj, rc, dummy):
        print "connect", self.client._host, self.client._port, rc
        for channel in self.channels:
            mosq.subscribe(channel)

    def on_message(self, mosq, obj, msg):
        print msg.topic, msg.mid
        print msg.payload
        if msg.topic == "watch":
            print "change watch"
            targets = msg.payload.split(' ')
            random.shuffle(targets)
            self.ping.targets = targets
        # FIXME python3 hates string type mismatch
        #print("Message received on topic "+msg.topic+" with id "+str(msg.mid)+
        #" with QoS "+str(msg.qos)+" and payload "+msg.payload)

    def loop(self):
        self.ping = Ping('free.fr', 'yahoo.fr', 'voila.fr', 'www.doctissimo.fr')
        while True:
            try:
                self.lazy_loop()
                self.ping.lazy_start()
                r = self.ping.poll()
                if r is not None:
                    success, target, message = r
                    self.publish('ping/%s' % target, json.dumps(
                        [self.name, success, message]), qos=1)
                print ".",
            except socket.error as e:
                print "oups", e