Connections¶
HSMS has active and passive connections. The active connection is the one making the connection, the passive one is waiting for the incoming connection.
The implementation for the active connection is secsgem.hsms.connections.HsmsActiveConnection
.
For the passive connection there are two implementations:
secsgem.hsms.connections.HsmsPassiveConnection
handles only one connection at a time.secsgem.hsms.connections.HsmsMultiPassiveConnection
together withsecsgem.hsms.connections.HsmsMultiPassiveServer
handle multiple connections from different peers.
All connection classes are based on the secsgem.hsms.connections.HsmsConnection
class, which provides common functionality for all connection types.
The connection process for active and passive connections can be started with the secsgem.hsms.connections.HsmsPassiveConnection.enable()
function, and stopped with the secsgem.hsms.connections.HsmsPassiveConnection.disable()
function.
Delegates¶
All connections work with delegates. When a connection is established/terminated or a packet is received a method of the passed delegate object will be called. The connections support the following delegates:
- on_connection_established(connection)
- on_connection_packet_received(response)
- on_connection_before_closed(connection)
- on_connection_closed(connection)
Sample delegate class:
class DelegateSample:
def on_connection_established(self, connection):
print "Connection established"
def on_connection_packet_received(self, connection, packet):
print "Packet received", packet
def on_connection_before_closed(self, connection):
print "Connection about to be terminated"
def on_connection_closed(self, connection):
print "Connection terminated"
Active connection¶
For the active connection the first parameter is the IP address of the peer, the second parameter is the port of the peer. The third parameter is the session id the peer is configured for.
Example:
>>> delegate = DelegateSample()
>>> conn = secsgem.HsmsActiveConnection('10.211.55.33', 5000, 0, delegate)
>>> conn.enable()
Connection established
Packet received header: {session_id:0x0000, stream:00, function:04, p_type:0x00, s_type:0x07, system:0x00000000, require_response:0}
Packet received header: {session_id:0x0000, stream:00, function:01, p_type:0x00, s_type:0x07, system:0x00000000, require_response:0}
Connection about to be terminated
Connection terminated
>>> conn.disable()
Passive connection¶
For the passive connection the first parameter is the expected IP address of the peer, the second parameter is the port to listen on. The third parameter is the session id the peer is configured for.
Example:
>>> delegate = DelegateSample()
>>> conn = secsgem.HsmsPassiveConnection('10.211.55.33', 5000, 0, delegate)
>>> conn.enable()
Connection established
Packet received header: {session_id:0xffff, stream:00, function:00, p_type:0x00, s_type:0x01, system:0x00000001, require_response:0}
Packet received header: {session_id:0x0000, stream:00, function:03, p_type:0x00, s_type:0x07, system:0x00000000, require_response:0}
Connection about to be terminated
Connection terminated
>>> conn.disable()
Multi-passive connection¶
In this mode one listening port handles the incoming connections for more than one peer.
A instance of secsgem.hsms.connections.HsmsMultiPassiveServer
is created and connection is created using its secsgem.hsms.connections.HsmsMultiPassiveServer.create_connection()
method.
The parameters of the method are the same as for the Passive connection. For every available peer a connection must be created using this method.
Example:
>>> delegate = DelegateSample()
>>> server = secsgem.HsmsMultiPassiveServer(5000)
>>> conn = server.create_connection('10.211.55.33', 5000, 0, delegate)
>>> conn.enable()
>>> server.start()
Connection established
Packet received header: {session_id:0xffff, stream:00, function:00, p_type:0x00, s_type:0x01, system:0x00000003, require_response:0}
Packet received header: {session_id:0x0000, stream:00, function:03, p_type:0x00, s_type:0x07, system:0x00000000, require_response:0}
Connection about to be terminated
Connection terminated
>>> conn.disable()
>>> server.stop()
Connection manager¶
The secsgem.hsms.connectionmanager.HsmsConnectionManager
can be used to manage multiple active and passive connections.
It creates and removes secsgem.hsms.connections.HsmsActiveConnection
and secsgem.hsms.connections.HsmsMultiPassiveServer
/secsgem.hsms.connections.HsmsMultiPassiveConnection
dynamically.
>>> manager=secsgem.HsmsConnectionManager()
>>> handler=manager.add_peer("connection", '10.211.55.33', 5000, False, 0)
>>> handler.enable()
>>> handler.send_linktest_req()
secsgem.hsms.HsmsPacket({'header': secsgem.hsms.HsmsHeader({'function': 0, 'stream': 0, 'p_type': 0, 'system': 13, 'session_id': 65535, 'require_response': False, 's_type': 6}), 'data': ''})
>>> handler.disable()
>>> manager.stop()
Connection manager works with handlers which take care of a lot of the required communication on the matching level (secsgem.hsms.protocol.HsmsProtocol
, secsgem.secs.handler.SecsHandler
and secsgem.gem.handler.GemHandler
).