Package DebTorrent :: Module RawServer :: Class RawServer
[hide private]
[frames] | no frames]

Class RawServer

source code

Manage connections and tasks like a server.

Mostly just manages the tasks, any socket related duties are passed on to the SocketHandler.SocketHandler instance.

Instance Methods [hide private]
 
__init__(self, doneflag, timeout_check_interval, timeout, ipv6_enable=True, sockethandler=None, excflag=Event())
Initialize the instance and start the socket handler.
source code
threading.Event
get_exception_flag(self)
Get the flag used to indicate exceptions.
source code
 
_add_task(self, func, delay, id=None)
Add the task to the sorted list of tasks to execute.
source code
 
add_task(self, func, delay=0, id=None)
Add the task to the list of tasks to schedule.
source code
 
scan_for_timeouts(self)
Scan the open sockets for any timeouts.
source code
 
bind(self, port, bind='', reuse=True, ipv6_socket_style=1)
Bind to listen on a single port.
source code
int
find_and_bind(self, minport, maxport, bind='', reuse=True, ipv6_socket_style=1, randomizer=True)
Bind to listen on a single port within a range.
source code
SocketHandler.SingleSocket
start_connection_raw(self, dns, socktype, handler=None)
Initiate a new connection to a peer (setting the type of socket).
source code
SocketHandler.SingleSocket
start_connection(self, dns, handler=None, randomize=True)
Initiate a new connection to a peer.
source code
dictionary
get_stats(self)
Get some information about the bound interfaces and ports.
source code
 
pop_external(self)
Add the waiting externally added tasks to the list of tasks to process.
source code
 
sighandler(self, signalnum, frame)
Properly handle a SIGINT.
source code
boolean
listen_forever(self, handler)
Start the server listening on sockets and processing tasks.
source code
boolean
is_finished(self)
Check if the server is done listening.
source code
 
wait_until_finished(self)
Wait until the server is done listeneing.
source code
 
_kill_tasks(self)
Remove the pending list of tasks to remove from those tasks still pending.
source code
 
kill_tasks(self, id)
Remove tasks from the list of those pending to execute.
source code
 
exception(self, kbint=True)
Print an exception that has occurred.
source code
 
set_handler(self, handler, port=None)
Set the handler to use for a port (or the default handler).
source code
 
shutdown(self)
Shutdown the socket handler.
source code
Instance Variables [hide private]
list dead_from_write
not used
threading.Event doneflag
flag to indicate the program is to be shutdown
int exccount
number of exceptions that have occurred
threading.Event excflag
the flag to use to indicate an exception has occurred
list of (float, method, unknown) externally_added
externally queued tasks to add to funcs
threading.Event finished
whether the server is done listening
list of (float, method, unknown) funcs
the list of future methods to invoke, the time to invoke at, the method, and the ID to use to identify the method
dictionary servers
not used
dictionary single_sockets
not used
SocketHandler.SocketHandler sockethandler
the handler to use to manage all open sockets
list tasks_to_kill
the IDs of tasks to remove from funcs
float timeout
seconds to wait between closing sockets on which nothing has been received on
float timeout_check_interval
seconds to wait between checking if any connections have timed out
Method Details [hide private]

__init__(self, doneflag, timeout_check_interval, timeout, ipv6_enable=True, sockethandler=None, excflag=Event())
(Constructor)

source code 
Initialize the instance and start the socket handler.
Parameters:
  • doneflag (threading.Event) - flag to indicate the program is to be shutdown
  • timeout_check_interval (float) - seconds to wait between checking if any connections have timed out
  • timeout (float) - seconds to wait between closing sockets on which nothing has been received on
  • ipv6_enable (boolean) - allow the client to connect to peers via IPv6 (optional, defaults to True)
  • sockethandler (SocketHandler.SocketHandler) - the handler to use to manage all open sockets (optional, defaults to creating a new one)
  • excflag (threading.Event) - the flag to use to indicate an exception has occurred (optional, defaults to using a new flag)

get_exception_flag(self)

source code 
Get the flag used to indicate exceptions.
Returns: threading.Event
the exception flag

_add_task(self, func, delay, id=None)

source code 
Add the task to the sorted list of tasks to execute.
Parameters:
  • func (method) - the task to be run
  • delay (int) - the number of seconds to delay before running the task
  • id (unknown) - an identifier to later find the task by

add_task(self, func, delay=0, id=None)

source code 
Add the task to the list of tasks to schedule.
Parameters:
  • func (method) - the task to be run
  • delay (int) - the number of seconds to delay before running the task
  • id (unknown) - an identifier to later find the task by

bind(self, port, bind='', reuse=True, ipv6_socket_style=1)

source code 
Bind to listen on a single port.
Parameters:
  • port (int) - the port to listen on
  • bind (string) - the IP address to bind to (optional, defaults to all)
  • reuse (boolean) - whether to use SO_REUSEADDR to bind (optional, defaults to False). This allows the bind to work if the socket is still open in the TIME_WAIT state from a recently shutdown server.
  • ipv6_socket_style (int) - whether an IPv6 server socket will also field IPv4 connections (optional, defaults to yes)

find_and_bind(self, minport, maxport, bind='', reuse=True, ipv6_socket_style=1, randomizer=True)

source code 
Bind to listen on a single port within a range.
Parameters:
  • minport (int) - the minimum port to listen on
  • maxport (int) - the maximum port to listen on
  • bind (string) - the addresses to bind to (optional, defaults to the default IPv6 and IPv4 addreses). Parsed as a comma seperated list (can be IP addresses or hostnames).
  • reuse (boolean) - whether to use SO_REUSEADDR to bind (optional, defaults to False). This allows the bind to work if the socket is still open in the TIME_WAIT state from a recently shutdown server.
  • ipv6_socket_style (int) - whether an IPv6 server socket will also field IPv4 connections (optional, defaults to yes)
  • randomizer (boolean) - whether to randomize the range or use it sequentially
Returns: int
the port that was bound to

start_connection_raw(self, dns, socktype, handler=None)

source code 
Initiate a new connection to a peer (setting the type of socket).
Parameters:
  • dns ((string, int)) - the IP address and port number to contact the peer on
  • socktype (int) - the type of socket to open
  • handler (unknown) - the data handler to use to process data on the connection (optional, defaults to using the defualt handler)
Returns: SocketHandler.SingleSocket
the new connection made to the peer

start_connection(self, dns, handler=None, randomize=True)

source code 
Initiate a new connection to a peer.
Parameters:
  • dns ((string, int)) - the IP address and port number to contact the peer on
  • handler (unknown) - the data handler to use to process data on the connection (optional, defaults to using the defualt handler)
  • randomize (boolean) - whether to randomize the possible sockets or choose one sequentially
Returns: SocketHandler.SingleSocket
the new connection made to the peer

get_stats(self)

source code 
Get some information about the bound interfaces and ports.
Returns: dictionary
info about the bound interfaces

sighandler(self, signalnum, frame)

source code 
Properly handle a SIGINT.
Parameters:
  • signalnum (int) - the signal that was received
  • frame (frame) - the current stack frame (not used)
Raises:
  • KeyboardInterrupt - always

listen_forever(self, handler)

source code 
Start the server listening on sockets and processing tasks.
Parameters:
  • handler (unknown) - the default data handler to use to process data on connections
Returns: boolean
whether the server should be restarted

is_finished(self)

source code 
Check if the server is done listening.
Returns: boolean
whether the server is done listeneing

kill_tasks(self, id)

source code 
Remove tasks from the list of those pending to execute.
Parameters:
  • id (unknown) - an identifier find the tasks by

exception(self, kbint=True)

source code 
Print an exception that has occurred.
Parameters:
  • kbint (boolean) - whether the exception was from a KeyboardInterrupt (optional, defaults to False)

set_handler(self, handler, port=None)

source code 
Set the handler to use for a port (or the default handler).
Parameters:
  • handler (unknown) - the data handler to use to process data on the port
  • port (int) - the port to use the handler for (optional, defaults to setting the default handler)