# Path: servicebus/command.py
# def cmd(cmd):
#     """
#     Execute a command, return stdout and stderr in tuple
#     """
#     logging.debug("Execute Command: %s" % cmd)
#     process = None
#     try:
#         process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
#         return process.communicate()
#     finally:
#         if process is not None:
#             process.stdout.close()
#             process.stderr.close()


import time
import logging
from threading import Thread
from servicebus.command import cmd


DIDA_TIMEOUT = 60


class PingWatcher:
    def __init__(self, receiver):
        self.receiver = receiver

    def do_watch(self):
        ip = self.receiver.host
        while True:
            ret = self.ping(ip)
            if not ret and self.receiver.connected:
                self.receiver.connected = False
                logging.info("Ping Error!")
                return True
            time.sleep(DIDA_TIMEOUT)
            if not self.receiver.connected:
                logging.info("Message receiver exited, PingWatcher out.")
                return True
        return False


# Path: servicebus/command.py
# def cmd(cmd):
#     """
#     Execute a command, return stdout and stderr in tuple
#     """
#     logging.debug("Execute Command: %s" % cmd)
#     process = None
#     try:
#         process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
#         return process.communicate()
#     finally:
#         if process is not None:
#             process.stdout.close()
#             process.stderr.close()


# Path: servicebus/watcher.py
import time
import logging
from threading import Thread
from servicebus.command import cmd



DIDA_TIMEOUT = 60


class PingWatcher:
    def __init__(self, receiver):
        self.receiver = receiver

    def do_watch(self):
        ip = self.receiver.host
        while True:
            ret = self.ping(ip)
            if not ret and self.receiver.connected:
                self.receiver.connected = False
                logging.info("Ping Error!")
                return True
            time.sleep(DIDA_TIMEOUT)
            if not self.receiver.connected:
                logging.info("Message receiver exited, PingWatcher out.")
                return True
        return False


    def do_ping(self, ip):