Hi All
I recently succeeded in getting a number of computers running JMRI to share sensor information with each other using sockets. The only problem is that I cannot work out how two close the socket.
If the socket has been opened on a port and then closed (due to a disconnection of some sort) it is not possible to re-open the socket as the port is still in use form the previous time I'd opened the socket. The only way I can close it is to shut JMRI and re-open it which is a real pain.
A lot of the things I have in below we're in an attempt to correctly close the socket.
I open a socket using the following code:
start_new_thread(Simulator.links,('East',Simulator.sim2EastPort))
def links(name,port):
Simulator.log( name+' handle')
Simulator.log( 'port:'+str(port))
if Simulator.interlocking == 'West':
host = Simulator.westIp
else:
host = Simulator.eastIp
s = socket.socket()
s.bind((host,port))
Simulator.log( name+' RX Server Started')
while Simulator.closePort != name:
try:
s.listen(1)
Simulator.log( name+' RX listening')
except:
Simulator.log( name+' RX failed')
pass
c, addr = s.accept()
Simulator.log( name+' RX Connection from: '+str(addr))
while True and Simulator.closePort != name :
data = c.recv(2048)
if not data:
break
try:
if data[-1] == '4':
InstanceManager.sensorManagerInstance().getSensor(data[:-1]).setKnownState(Sensor.INACTIVE)
if data[-1] == '2':
InstanceManager.sensorManagerInstance().getSensor(data[:-1]).setKnownState(Sensor.ACTIVE)
except:
Simulator.log( 'failed to find sensor:'+data[:-1])
c.close()
s.close()
Simulator.log( name+' RX closed port')
if Simulator.closePort != name:
return 1
else:
Simulator.closePort = None
return 0
links = SPSstaticMethod(links)
Can anyone help?
Thanks.
Kev
(PS, I'm no programmer, I got this far by trial, error and lots of copy and paste.)