I've posted this over at Satckoverflow and here is the link in case any Scratcher's have already overcome the issue
http://stackoverflow.com/questions/1216 … disconnect
Offline
I don't know - need a Python expert
Simon
Offline
There are 2 things you need to do: 1) catch the socket exception 2) restart initialize the program.
I dunno python, ask Magnie for specifics, but here is some pseudo-code
try{
socket s = new Socket(localhost, 42001)
} catch SocketException{
restart();
}
i = read(s);
write(s, o);
Offline
One problem is that there is no exception
try:
data = self.scratch_socket.recv(BUFFER_SIZE)
dataraw = data[4:].lower()
print 'Length: %d, Data: %s' % (len(dataraw), dataraw)
if len(dataraw) == 0:
#This is probably due to client disconnecting
#I'd like the program to retry connecting to the client
time.sleep(2)just starts giving no data on each loop - so you can detect the condition len(dataraw) =0 - its just how do I (cleanly and thread safe) jump out of the loop and effectively get back to this bit of code
def create_socket(host, port):
while True:
try:
print 'Trying'
scratch_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratch_sock.connect((host, port))
break
except socket.error:
print "There was an error connecting to Scratch!"
print "I couldn't find a Mesh session at host: %s, port: %s" % (host, port)
time.sleep(3)
#sys.exit(1)
return scratch_sockSimon
Offline
That's quite...interesting.
Offline
For the second exception, you should probably do a little more clean up ( socket.close(), etc ) and then rather than break it, run create_socket(DEFAULT_HOST, PORT) and then have it continue rather than break. I'd have to see the program run to debug it more.
I don't see anything wrong with the create_socket() function. Could you explain a little more about what your problem is?
Offline
I don't see anything wrong with the create_socket() function. Could you explain a little more about what your problem is?
When Scratch is exited (or RSC disabled) then the loop just receives no data from the socket and gets to the
if len(dataraw) == 0:
#This is probably due to client disconnecting
#I'd like the program to retry connecting to the client
time.sleep(2)section.
Basically I want it to safely close things (threads/sockets/whatever's needed) and restart listening out for a new incoming connection like it does when you first run the program.
So simply,
listen for connection
start looping and receving data
- when data packets become zero length - exit the loop
and go back to listen for connection
Simon
Offline
Yeah, just put a create_socket(host, port) and continue after the time.sleep(2).
So something like:
if len(dataraw) == 0:
#This is probably due to client disconnecting
#I'd like the program to retry connecting to the client
time.sleep(2)
create_socket(host, port) # Continue to attempt to connect.
continue # Once connected, go back to the start of the loop and wait for new data.Offline
Nice try - does attempt to reconnect but once RSC re-enabled in Scratch it then sits printing out his loop
Trying Connected! Length:0, Data: Connecting...
I'm no expert but I thin I need to do something nice the to running threads
Simon
Offline
I think I've found a solution I've added this
if len(dataraw) == 0:
#This is probably due to client disconnecting
#I'd like the program to retry connecting to the client
#tell outer loop that Scratch has disconnected
if cycle_trace == 'running':
cycle_trace = 'disconnected'
breakand made this my outer program loop
cycle_trace = 'start'
while True:
if (cycle_trace == 'disconnected'):
print "Scratch disconnected"
cleanup_threads((listener, sender))
time.sleep(1)
cycle_trace = 'start'
if (cycle_trace == 'start'):
# open the socket
print 'Starting to connect...' ,
the_socket = create_socket(host, PORT)
print 'Connected!'
the_socket.settimeout(SOCKET_TIMEOUT)
listener = ScratchListener(the_socket)
sender = ScratchSender(the_socket)
cycle_trace = 'running'
print "Running...."
listener.start()
sender.start()
# wait for ctrl+c
try:
#just pause
time.sleep(0.5)
except KeyboardInterrupt:
cleanup_threads((listener,sender))
sys.exit()Simon
Offline