This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-08-28 13:02:32

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Scratch/Python Remote Sensor Connection socket issue

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

 

#2 2012-08-28 13:16:02

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: Scratch/Python Remote Sensor Connection socket issue

i don't know python but would break be good enough?


http://i.imgur.com/1QqnHxQ.png

Offline

 

#3 2012-08-28 14:38:50

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Re: Scratch/Python Remote Sensor Connection socket issue

I don't know - need a Python expert  smile
Simon

Offline

 

#4 2012-08-28 15:07:27

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Scratch/Python Remote Sensor Connection socket issue

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);


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#5 2012-08-28 16:19:29

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Re: Scratch/Python Remote Sensor Connection socket issue

One problem is that there is no exception  sad

Code:

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

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_sock

Simon

Offline

 

#6 2012-08-28 16:55:32

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Scratch/Python Remote Sensor Connection socket issue

That's quite...interesting.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#7 2012-08-28 17:05:24

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Scratch/Python Remote Sensor Connection socket issue

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

 

#8 2012-08-28 19:08:18

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Re: Scratch/Python Remote Sensor Connection socket issue

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

Code:

                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

 

#9 2012-08-28 21:05:26

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Scratch/Python Remote Sensor Connection socket issue

Yeah, just put a create_socket(host, port) and continue after the time.sleep(2).  smile

So something like:

Code:

                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

 

#10 2012-08-29 04:07:20

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Re: Scratch/Python Remote Sensor Connection socket issue

Nice try - does attempt to reconnect but once RSC re-enabled in Scratch it then sits printing out his loop

Code:

Trying
Connected!
Length:0, Data:
Connecting...

I'm no expert but I thin I need to do something nice the to running threads  smile

Simon

Offline

 

#11 2012-08-29 18:06:32

SimpleScratch
Scratcher
Registered: 2007-05-25
Posts: 100+

Re: Scratch/Python Remote Sensor Connection socket issue

I think I've found a solution I've  added this

Code:

                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'
                        break

and made this my outer program loop

Code:

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

 

Board footer