Package legume :: Module server :: Class Server
[frames] | no frames]

Class Server

               object --+    
                        |    
netshared.NetworkEndpoint --+
                            |
                           Server


A server. To allow network clients to communicate with this class
call listen() with a network address then periodically call update()
to ensure data is kept flowing and connects/disconnects are handled.

Instance Methods
 
__init__(self, message_factory=messages.message_factory)
Create an instance of a new Server endpoint.
 
disconnect(self, peer_address)
Disconnect a peer by specifying their address.
 
disconnect_all(self)
Disconnect all connected clients
 
get_peer_by_address(self, peer_address)
Obtain a ServerPeer instance by specifying the peer's address
 
listen(self, address)
Begin listening for incoming connections.
 
update(self)
Pumps buffers and dispatches events.
 
send_message_to_all(self, message)
Send a non-reliable packet to all connected peers.
 
send_reliable_message_to_all(self, message)
Send a reliable message to all connected peers.
 
__del__(self) (Inherited from legume.netshared.NetworkEndpoint)
 
do_read(self, callback) (Inherited from legume.netshared.NetworkEndpoint)
 
get_state(self) (Inherited from legume.netshared.NetworkEndpoint)
 
is_active(self) (Inherited from legume.netshared.NetworkEndpoint)
 
setTimeout(self, timeout) (Inherited from legume.netshared.NetworkEndpoint)

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  CONNECTED = 104 (Inherited from legume.netshared.NetworkEndpoint)
  CONNECTING = 103 (Inherited from legume.netshared.NetworkEndpoint)
  DISCONNECTED = 100 (Inherited from legume.netshared.NetworkEndpoint)
  ERRORED = 101 (Inherited from legume.netshared.NetworkEndpoint)
  LISTENING = 102 (Inherited from legume.netshared.NetworkEndpoint)
  MTU = 1400 (Inherited from legume.netshared.NetworkEndpoint)
Properties
  peercount
Number of connected peers.
  peers
A list of connected peers.
  OnMessage
  OnConnectRequest
  OnError
  OnDisconnect
  socket (Inherited from legume.netshared.NetworkEndpoint)
  state (Inherited from legume.netshared.NetworkEndpoint)
  timeout (Inherited from legume.netshared.NetworkEndpoint)

Inherited from object: __class__

Method Details

__init__(self, message_factory=messages.message_factory)
(Constructor)

 

Create an instance of a new Server endpoint. Use the message_factory
parameter to specify an alternative to the global messages.message_factory
instance::

    mf = legume.messages.MessageFactory()
    server = legume.Server(message_factory=mf)

Overrides: object.__init__

disconnect(self, peer_address)

 
Disconnect a peer by specifying their address.
Equivalent to::

    server.get_peer_by_address(peer_address).disconnect()

listen(self, address)

 
Begin listening for incoming connections.
address is a tuple of the format (hostname, port)
This method change the class state to LISTENING::

    # Begin listening on port 4000 on all IP interfaces
    server = legume.Server()
    server.listen(('', 4000))

update(self)

 
Pumps buffers and dispatches events. Call regularly to ensure
buffers do not overfill or connections time-out::

    server = legume.Server()
    server.listen(('', 4000))

    while True:
        server.update()
        # Other update tasks here..
        time.sleep(0.001)

send_message_to_all(self, message)

 
Send a non-reliable packet to all connected peers.
packet is an instance of a legume.message.BaseMessage subclass::

    message = ExampleMessage()
    message.chat_message.value = "Hello!"
    message.sender.value = "@X3"
    server.send_message_to_all(message)

send_reliable_message_to_all(self, message)

 
Send a reliable message to all connected peers. message is an
instance of a legume.message.BaseMessage subclass.


Property Details

peercount

Number of connected peers.

Get Method:
unreachable.peercount(self) - Number of connected peers.

peers

A list of connected peers.

Get Method:
unreachable.peers(self) - A list of connected peers.

OnMessage

Get Method:
_getOnMessage(self)
Set Method:
_setOnMessage(self, event)

OnConnectRequest

Get Method:
_getOnConnectRequest(self)
Set Method:
_setOnConnectRequest(self, event)

OnError

Get Method:
_getOnError(self)
Set Method:
_setOnError(self, event)

OnDisconnect

Get Method:
_getOnDisconnect(self)
Set Method:
_setOnDisconnect(self, event)