Exemple #1
0
    def close(self, active=True):
        """
        This method will close the socket. The active parameter will specify
        whether the socket is closed on this side or was closed on the other
        side. If the socket was closed on this side, no attempt to reconnect
        will be attempted. Otherwise, a reconnect will be attempted.
        """
        if not self.__socket or not self.__connected:
            return

        if self.__unix_socket:
            if self.__listener:
                close_what = "Unix Domain Socket listening on %s" \
                             % (self.__port)
            else:
                close_what = "Unix Domain Socket connection to %s" \
                             % (self.__port)
        else:
            if self.__listener:
                close_what = "server socket on port %d" % (self.__port)
            else:
                close_what = "TCP connection to %s:%d" \
                             % (self.__host, self.__port)

        self.__connected = False
        mgr = IOManager()
        mgr.unregister(self)

        if not self.__socket is None:
            try:
                self.__socket.close()
                self.__socket.shutdown()
            except socket.error as e:
                pass

        if active:
            self.__logger.info("Closed %s" % close_what)
        else:
            self.__logger.info("%s closed." % close_what)

        if self.__unix_socket and self.__listener:
            try:
                os.unlink(self.__port)
            except:
                pass

        self.__socket = None
        self.__fileno = None

        # If remote end closed connection, attempt to reconnect
        if not active and           \
           not self.__listener and  \
           not self.__handler and   \
           self.__giveup != 0:
            mgr = IOManager()
            mgr.register_unconnected(self)
        else:
            self.handle_shutdown()
Exemple #2
0
    def _handle_attempt(self):
        """
        This method is called when the socket is not
        connected, once every loop of the IOManager.
        """
        self.handle_loop_disconnected()
        if time.time() < self.__last_attempt + self.__timeout:
            return False

        self.__last_attempt = time.time()
        if self.__listener:
            result = self.__listen()
        else:
            result = self.__connect()

        if not result:
            self.__attempt += 1
            action = "bind to port" if self.__listener else "connect"
            if self.__giveup >= 0 and self.__attempt >= self.__giveup:
                self.__logger.critical("Failed to %s for %d times. Giving up." \
                                       % (action, self.__attempt))
                mgr = IOManager()
                mgr.unregister_unconnected(self)
            elif self.__giveup >= 0:
                self.__logger.critical("Failed to %s for %d/%d times. " \
                                       "Retrying in %d seconds."        \
                                       % (action, self.__attempt,       \
                                          self.__giveup, self.__timeout))
            else:
                self.__logger.critical("Failed to %s for %d times. " \
                                       "Retrying in %d seconds."     \
                                       % (action, self.__attempt,    \
                                          self.__timeout))
        else:
            self.__attempt = 0
Exemple #3
0
 def error_test(self, method_kind, phase, error_class):
     kwargs_key = phase + '_kwargs'
     manager = IOManager(**{kwargs_key: {'required': CustomType}})
     method_name = '_'.join([method_kind, phase])
     method = getattr(manager, method_name)
     with pytest.raises(error_class):
         method(iovalue=object())
Exemple #4
0
    def __listen(self):
        """
        This method is called from the connection manager when the socket is a
        server socket and should bind to the port specified.
        """
        if self.__socket or not self.__listener:
            return False

        if self.__unix_socket:
            if os.path.exists(self.__port):
                try:
                    os.unlink(self.__port)
                except:
                    self.__logger.error("Socket %s exists and cannot be " \
                                        "removed" % self.__port)
                    return False

            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port

        try:
            sock.bind(address)
            sock.listen(5)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.warning("Could not bind to %s: %s" \
                                  % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions usually mean bad settings. Try one more time but
            # do not keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__logger.info("Socket listening on %s initialized" % to_what)

        self.__socket = sock
        self.__fileno = self.__socket.fileno()
        self.__connected = True
        mgr = IOManager()
        mgr.register(self)
        return True
Exemple #5
0
    def _handle_write(self):
        """
        This method is called from the IOManager thread when there the socket
        is writable and marked writable in the IO poller. It uses the stringio
        object of the current send buffer to get the next data to send and
        makes sure that the stringio object is set at the correct position,
        based on the amount of bytes actually sent.
        """
        with self.__send_lock:
            if not self.__send_buffer:
                # No data to write, make sure that this
                # socket is marked not writable in the
                # IOManager
                if self.connected():
                    mgr = IOManager()
                    mgr.set_writable(self, False)
                raise IOWrapperEnd()

            cur_id, cur_buffer = self.__send_buffer[0]
            cur_buffer.seek(self.__buffer_start_pos)
            data = cur_buffer.read(self.__buffer_size)
            if not data:
                # If the buffer ran out of data,
                # remove it from the queue and return.
                # The next attempt to write will set
                # the socket non-writable if there is
                # no next send buffer.
                cur_buffer.close()
                self.__send_buffer.pop(0)
                self.__send_buffer_ids.remove(cur_id)
                self.__buffer_start_pos = 0

                return

            sent_bytes = 0
            try:
                sent_bytes = self.__socket.send(data)
                self.__buffer_start_pos += sent_bytes
            except socket.error as err:
                if err.errno == errno.EAGAIN:  # Resource temporarily unavailable
                    raise IOWrapperEnd()
                raise

            if self.__log_output:
                self.__logger.debug("Sent data: %s" % repr(data[:sent_bytes]))

            self.__total_bytes += sent_bytes
            return sent_bytes
Exemple #6
0
    def __init__(self, name):
        self.name = name

        self.regex_name = '^(' + re.escape(name) + "|" + re.escape(
            unidecode(name)) + "),? ?"

        logger.info("%s is initializing. Running version %s" %
                    (name, self.version))

        # Create input manager
        self.io_manager = IOManager(self)

        # 1st. Setup Inputs
        self.io_manager.setup()

        # 2nd. Setup Skills
        self.setup_skills()

        # 3rd. Setup default skill
        self.default_skill = Default(self)
Exemple #7
0
    def __connect(self):
        """
        This method is called by the _do_attempt method when the socket is a
        connecting socket that should try to connect
        """
        if self.__socket:
            return False

        if self.__unix_socket:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            address = (self.__port)
            to_what = "Unix Domain Socket %s" % self.__port
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = (self.__host, self.__port)
            to_what = "TCP port %d" % self.__port
        try:
            sock.connect(address)
            self.__set_socket_options(sock)
        except socket.error as e:
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            return False
        except Exception as e:
            # Other exceptions mean bad settings. Try one more time but do not
            # keep on trying forever..
            self.__logger.error("Could not connect to %s: %s" \
                                % (to_what, str(e)))
            self.__giveup = 2
            return False

        self.__socket = sock
        self.__connected = True
        self.__fileno = self.__socket.fileno()

        mgr = IOManager()
        mgr.register(self)
        if not self.send_buffer_empty():
            mgr.set_writable(self, True)

        return True
Exemple #8
0
    def __init__(self, argv):

        self.iom = IOManager(argv)
        self.keys = {}

        self.IF = ImageFactory()

        # get keys from rootfile, iterate over the enum
        # and see what's in the root file
        for i in xrange(larcv.kProductUnknown):
            product = larcv.ProductName(i)

            self.keys[product] = []

            producers = self.iom.iom.producer_list(i)

            for p in producers:
                self.keys[product].append(p)

        # run subrun and event start at zero
        self.run = -1
        self.subrun = -1
        self.event = -1
Exemple #9
0
    def send(self, data, optional=False):
        """
        This method enqueues new data to be send. It wraps the data by pickling
        it if configured to do so and creates a new StringIO buffer to store the
        new data.
        """
        if self.__listener:
            if not self.__client_sockets:
                self.__logger.warning("No clients connected to send data")
                return False
            with self.__receive_lock:
                for wrapper in self.__client_sockets:
                    wrapper.send(data)
            return True

        if self.__use_pickle:
            try:
                data = pickle.dumps(data)
            except pickle.PicklingError:
                self.__logger.error("Cannot pickle data %s" % repr(data))
                return False

        with self.__send_lock:
            set_writable = False
            if not self.__send_buffer:
                set_writable = True

            id = self.__send_id
            self.__send_id += 1
            buffer = stringio.StringIO(data)
            self.__send_buffer.append((id, buffer))
            self.__send_buffer_ids.add(id)

            if set_writable and self.connected():
                mgr = IOManager()
                mgr.set_writable(self, True)
            return id
Exemple #10
0
    def __init__(
        self,
        host="localhost",  # The host to connect to or serve
        port=49152,  # The port to connect to or serve
        giveup=5,  # Amount of reconnect attempts
        use_socket=None,  # Pass in a connected socket object to use
        retry_timeout=1,  # Timeout between connection attempts
        server=None,  # True for a server, False for clients
        use_pickle=True,  # Whether to pickle/unpickle 
        #    send/received data
        bufsize="auto",  # Number of bytes to send/receive at once
        linger=.1,  # Whether to set the SO_LINGER option
        handler=False,  # Set to true to make this a incoming 
        #    connection handler
        logger=None  # Pass in a logger to use
    ):
        """
        Initialize the socket. The parameters define the behavior of the socket.
        
        Host should be an IP address or hostname to which to connect or on which
        to listen. If the server argument is not specifically set, the hostname
        will also define whether this will be a server or client socket: if host
        is empty, it will be a server socket listening on all interfaces,
        otherwise it will be a client socket. 

        port should be the port to listen on. When the port is numeric it is
        assumed to be a TCP port. When it is a string, it will be cast to an int
        specifying a TCP port. If the conversion failed, it is assumed to be a
        Unix Socket, located in /tmp. This will raise an exception if the socket
        is trying to connect to a remove Unix Socket, which is impractical and
        defeats the purpose.

        giveup is the amount of retries performed to connect to a socket or bind
        to a port.

        use_socket can be used to pass in an already connected socket. The same
        socket options will be set on this socket as newly created ones.

        retry_timeout defines the number of seconds to wait between connection/
        binding attempts of unconnected sockets.

        server is a boolean that specifies whether this will be a server or a
        client socket. A server socket will bind and listen to a port, a client
        socket will attempt to connect to a port. The default value decides this
        automatically, based on the value of the host parameter as described
        above.

        use_pickle is a boolean specifying whether communication on this socket
        will proceed using the Pickle protocol. If set to true, all outgoing
        data will be pickled and all incoming data will be unpickled.

        bufsize specifies the amount of data to send or receive at once. The
        default is auto, meaning it will be set depending on other options. When
        the socket to use is a Unix Domain Socket, it will use 128 KiB. If it is
        a client socket connecting to a local socket, it will be a 16 KiB and if
        it is a server socket or a client socket connecting to a non-local host,
        a buffer of 8 KiB is used.

        linger is a number specifying whether the socket will be set in
        linger mode (see man page for setsockopt(2)). When in linger mode, the
        call to close on the socket will block for at most a set number of
        seconds and will then forcefully close the socket. Otherwise, the close
        call is non-blocking and the socket will get in a TIME_WAIT state, which
        effectively blocks the port for up to 4 minutes, possibly resulting in
        'Port already bound' messages. If the linger parameter is 0, LINGER will
        not be used; otherwise this is the amount of seconds to set.

        handler can be set to True to activate incoming connection handler 
        behavior. This basically makes sure that no reconnetion attempt is 
        performed when the connection is closed by the other end.
        """

        self.__socket = None
        self.__fileno = None
        self.__client_sockets = []
        self.__connected = False
        self.__listener = False
        self.__handler = handler
        self.__use_pickle = use_pickle

        self.__send_lock = threading.RLock()
        self.__receive_lock = threading.RLock()
        self.__receive_buffer = []
        self.__recv_buffer = ""
        self.__send_buffer = []
        self.__send_buffer_ids = set([])
        self.__buffer_start_pos = 0
        self.__send_id = 0

        self.__set_port(host, port)

        self.__set_buffer_size(bufsize)
        self.__linger = float(linger)
        self.__total_bytes = 0

        self.__log_input = False
        self.__log_output = False

        if server is None and self.__host == "":
            self.__listener = True
        else:
            self.__listener = server

        if logger:
            self.__logger = logger
        else:
            self.__logger = logging.getLogger('Borg.Brain.Util.ThreadedSocket')

        self.__logger.addHandler(nullhandler.NullHandler())

        self.__last_attempt = 0
        self.__timeout = retry_timeout
        self.__giveup = giveup
        self.__attempt = 0

        if use_socket:
            self.__socket = use_socket
            self.__fileno = self.__socket.fileno()
            self.__port = port
            self.__connected = True
            self.__set_socket_options(self.__socket)
            mgr = IOManager()
            mgr.register(self)
        else:
            mgr = IOManager()
            mgr.register_unconnected(self)
Exemple #11
0
        loadUi('../ui/tbs_window.ui', self.tbs_window)
        self.tbs_window.setWindowTitle('Tarım Bilgi Sistemi')

        OceanViewGui.setButtonIcon(self.tbs_window.backtohomeButton,
                                   '../ui/icon/back.png')
        self.tbs_window.backtohomeButton.clicked.connect(
            self.on_backtohomeButton_clicked)

        webView = QtWebEngineWidgets.QWebEngineView(self.tbs_window.webWidget)
        webView.setUrl(
            QtCore.QUrl("https://tbs.tarbil.gov.tr/Authentication.aspx"))
        webView.setObjectName("webView")


if __name__ == '__main__':
    config = Config('../config.ini')
    engine = Engine(
        iomanager=IOManager(),
        analyzer=Analyzer(config=config, database=Database(config)),
        calibrator=Calibrator(input_dir=config.calibration_input_dir,
                              output_dir=config.calibration_output_dir,
                              calibration_eqns=config.calibration_equation),
        config=config)

    app = QApplication(sys.argv)

    widget = OceanViewGui(engine=engine, config=config)
    widget.show()

    sys.exit(app.exec_())
Exemple #12
0
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('csv_path')
    parser.add_argument('roster_path')
    parser.add_argument('output_path')
    args       = parser.parse_args()
    csv_pth    = args.csv_path
    roster_pth = args.roster_path
    out_path   = args.output_path
    roster     = []

    #grab the roster file and create a roster list
    with open(roster_pth) as r_file:
        for line in r_file:
            roster.append(line)

    c_data = ConfigData()

    manager  = IOManager(c_data, roster)
    students = manager.read(csv_pth)
    alg      = AlgorithmManager()
    teams    = alg.runMain(students)
    manager.write(out_path, teams)
   




    
Exemple #13
0
 def startManager(self):
     '''
     This starts the IOManager
     '''
     self.c_data = ConfigData()
     self.manager = IOManager(self.c_data, self.roster)
Exemple #14
0
#!/usr/bin/env python3
'''This module is the main body of the emulator.'''

from sys import argv
from vm import Chip8
from iomanager import IOManager


def load_rom(input_file):
    '''Load ROM file from the command line.'''
    try:
        with open(input_file, 'rb') as rom_file:
            return rom_file.read().hex().upper()
    except IOError:
        print("Error: file not found")
        exit(1)


if __name__ == "__main__":
    GAME_ROM = load_rom(argv[1])

    chip8 = Chip8(GAME_ROM)
    io_manager = IOManager(chip8)
Exemple #15
0
 def make_iomanager(self, process_kind, iospec, **kwargs):
     key = process_kind + '_kwargs'
     kwargs.setdefault(key, {})
     kwargs[key].update({'required': iospec})
     return IOManager(**kwargs)
Exemple #16
0
    d1 = Day("Tuesday")
    d1.insertTime(10)
    d1.insertTime(13)
    d2 = Day("Wednesday")
    d2.insertTime(10)
    d2.insertTime(14)

    s1 = Student("Streisand, Barbara", "")
    days = [d1, d2]
    filters3 = {}
    filters3['Meeting Times'] = days
    s1.setFilters(filters3)

    s2 = Student("Cobain, Curt", "")
    days = [d2]
    filters4 = {}
    filters4['Meeting Times'] = days
    s2.setFilters(filters4)

    t2 = Team(2, 4)

    t2.insertStudent(s1)
    t2.insertStudent(s2)

    team_lst = [t1, t2]

    manager = IOManager(roster)
    students = manager.read(csv_pth)
    manager.write(out_path, team_lst)
Exemple #17
0
 def method_test(self, method_name):
     iomanager = IOManager()
     method_callable = getattr(iomanager, method_name)
     method_callable(iovalue=object())