def __init__(self, port=None):
        super().__init__()
        self._conf = Config()
        self._sockets = SockCollection([], [])

        self._service_sock = self._create_socket_on_port(port=port)
        self._service_sock.listen(10)
        self._register_input_sock(self._service_sock)

        LOG.info('Server created on %s:%d' % self._service_sock.getsockname())
def get_protocol_driver(protocol=None):
    """Load appropriate protocol driver."""
    if protocol is None:
        try:
            protocol = Config().protocol
        except Exception as e:
            raise RuntimeError('Protocol not specified')
    if protocol == 'tcp':
        from sft.drivers.tcp import TCPDriver  # don't move to the top!
        return TCPDriver()
    elif protocol == 'udp':
        from sft.drivers.udp import UDPDriver  # don't move to the top!
        return UDPDriver()
    else:
        raise RuntimeError('Unknown protocol specified')
Beispiel #3
0
""" This module contains functionality for managing sessions.
"""

import logging
import time
from enum import Enum

from sft.common.commands.base import CommandBase, CommandFinished, CommandIds, ProgramFinished
from sft.common.commands.factory import CommandFactory
from sft.common.config import Config
from sft.common.utils.common import Singleton
from sft.common.utils.packets import get_heartbeat_payload

LOG = logging.getLogger(__name__)
_conf = Config()


class SessionStatus(Enum):
    """ Describes session status.

        active              -- fully active session
        inactive            -- fully inactive session, not need in processing, just saving the session state
        wait_for_activation -- state when session reactivate with 'connect' command, but no command executed yet
    """
    active = 0
    inactive = 1
    wait_for_activation = 2
    wait_for_close = 3


class Session:
import logging
import socket

from sft.common.commands.base import ErrorIds, ProgramFinished
from sft.common.config import Config
from sft.common.socket_manager import SocketManager
from sft.common.sessions.session_manager import SessionManager, SessionStatus

LOG = logging.getLogger(__name__)

_socket_manager = SocketManager()
_buffer_size = Config().tcp_buffer_size


def _handle_server_disconnection():
    # Socket closed by server
    # ToDo: Ask user for trying reactivation
    session = SessionManager().get_all_not_inactive_sessions()[0]
    if session.status == SessionStatus.wait_for_close:
        raise ProgramFinished(ErrorIds.SUCCESSFUL)
    raise Exception("Server socket closed unexpectedly")


def raw_data_reader(dummy_arg):
    """Receive data from tcp sockets.

       :param socket_list: List whith only one socket for reading data
       :return: [(server_addr, data), ]
    """

    sockets = _socket_manager.readable
import logging
import os

from sft.common.config import Config
from sft.common.commands.base import ServerCommandBase, CommandFinished, CommandIds, ErrorIds
from sft.common.utils.packets import get_payload, generate_packet, generate_header, get_error_code

STORAGE_PATH = os.path.expanduser(os.path.join("~", "sft-dir"))
if not os.path.exists(STORAGE_PATH):
    os.mkdir(STORAGE_PATH)

LOG = logging.getLogger(__name__)
_config = Config()

__all__ = ['Download']


class Download(ServerCommandBase):
    def __init__(self, first_packet):

        self._file_path = os.path.join(STORAGE_PATH, get_payload(first_packet))

        self._sended_bytes = 0
        self._generate_first_packet = True
        self._send_error = False
        self._send_pointer_moved = False
        self._wait_for_approve = False
        self._raise_command_finished = False

        if os.path.exists(self._file_path):
            self._file_size = os.path.getsize(self._file_path)
Beispiel #6
0
import logging
import time

from sft.common.config import Config
from sft.common.sessions.session_manager import SessionManager
from sft.common.socket_manager import SocketManager

LOG = logging.getLogger(__name__)
_get_not_inactive_sessions = SessionManager().get_all_not_inactive_sessions
_deactivate_session = SessionManager().deactivate_session
_conn_break_timeout = Config().connection_break_timeout
_socket_manager = SocketManager()


def server_state_check(dummy_arg):
    """Check last receive time in all active sessions.

       If time is expired, session will be deactivated.
    """
    # ToDo: Do sth with exceptional sockets
    active_sessions = _get_not_inactive_sessions()
    cur_time = time.time()

    for session in active_sessions:
        if cur_time - session.last_recv_time > _conn_break_timeout:
            LOG.warning(
                "Client %s:%d: seems that physical connection is broken" %
                session.client_address)
            _deactivate_session(session)
            LOG.warning("Client %s:%d: logical connection frozen" %
                        session.client_address)
Beispiel #7
0
 def __init__(self):
     super().__init__()
     self._conf = Config()
     self.cmd_dispatcher = Dispatcher()
 def __init__(self):
     super().__init__()
     self._conf = Config()