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

#1 2012-02-09 10:28:37

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

ping test destroys program startup

Hi guys,

I'm working on a program at the moment in Panther that, during startup attempts a ping connection with my website to see if the connection is working (ready for later use). However, I am experiencing some minor problems with that and I was wondering if anyone could suggest improvements?

here is my script:

(the PHP server-side script on my website returns 1 if it is viewed along with the data packet "ping=test", two if it is viewed but fails to receive the packet and obviously nothing if the connection is not made at all)

//^^^more setup scripts^^^
if  <(connect to internet?) = [true]>
if <not <<contents of file at url [accidentalgames.com/ping.php?ping=test]> = [1]>>//returns 1 for a success
set [connect to internet v] to [false]
end
end
//vvv more setup scripts vvv

Amazing. They adopted this new block thing and it can't even handle booleans.... I actually had to make the (contents of url []) reporter a boolean just so that it would let me nest that many blocks together and still offer space for a string argument...

anyway, if there is a sound internet connection, everything works fine. However, if there is no internet, the user gets a message that appears by their mouse saying "trouble resolving server name" with the options "give up" or "keep trying". Give up will allow the script to set the connect variable to false, and continue as normal.

HOWEVER, why must it ask this? It's clear there was an error in connecting and the user is informed of this by my program anyway, why should they get this unattractive message pop up? Is there a way around this?

Furthermore, on networks that require additional login information (such as the one at my University or my favourite cafe, the script simply stopped there if I hadn't yet entered these connection details.

Last edited by sparks (2012-02-09 10:44:34)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2012-02-09 10:37:12

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: ping test destroys program startup

sparks wrote:

Hi guys,

I'm working on a program at the moment in Panther that, during startup attempts a ping connection with my website to see if the connection is working (ready for later use). However, I am experiencing some minor problems with that and I was wondering if anyone could suggest improvements?

here is my script:

//^^^more setup scripts^^^
if  <(connect to internet?) = [true]>
if <not <<contents of file at url [accidentalgames.com/ping.php?ping=test]> = [1]>>//returns 1 for a success
set [connect to internet v] to [false]
end
end
//vvv more setup scripts vvv

Amazing. The adopted this new block thing and it can't even handle booleans.... I actually had to make the (contents of url []) reporter a boolean just so that it would let me nest that many blocks together and still offer space for a string argument...

anyway, if there is a sound internet connection, everything works fine. However, if there is no internet, the user gets a message that appears by their mouse saying "trouble resolving server name" with the options "give up" or "keep trying". Give up will allow the script to set the connect variable to false, and continue as normal.

HOWEVER, why must it ask this? It's clear there was an error in connecting and the user is informed of this by my program anyway, why should they get this unattractive message pop up? Is there a way around this?

Furthermore, on networks that require additional login information (such as the one at my University or my favourite cafe, the script simply stopped there if I hadn't yet entered these connection details.

I am unaware of how you do it in Panther

but doing it with length_of(response) > 0

would be a much failsafe solution . further it sounds that its inside panther "trouble resolving server name" , its a morphic message in smalltalk aye ?

Last edited by fanofcena (2012-02-09 10:38:21)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#3 2012-02-09 10:40:40

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

fanofcena wrote:

I am unaware of how you do it in Panther

but doing it with length_of(response) > 0

would be a much failsafe solution . further it sounds that its inside panther "trouble resolving server name" , its a morphic message in smalltalk aye ?

I disagree that length_of(response) > 0? would be a better way to do this. That would mean if the site was down and displayed a 404 message it would register as a successful connection. Furthermore, if the page is accessed but has failed to receive the url package "ping=test" it returns 2.

EDIT: I probably should have explained the return values of the server side PHP script in the first post, sorry if that was unclear

It is a morphic smalltalk message, yes  smile

[slightly off topic]MRN hasn't been online in a good two months, hasn't she?[/slightly off topic]

Last edited by sparks (2012-02-09 10:43:13)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#4 2012-02-09 10:52:35

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: ping test destroys program startup

sparks wrote:

fanofcena wrote:

I am unaware of how you do it in Panther

but doing it with length_of(response) > 0

would be a much failsafe solution . further it sounds that its inside panther "trouble resolving server name" , its a morphic message in smalltalk aye ?

I disagree that length_of(response) > 0? would be a better way to do this. That would mean if the site was down and displayed a 404 message it would register as a successful connection. Furthermore, if the page is accessed but has failed to receive the url package "ping=test" it returns 2.

EDIT: I probably should have explained the return values of the server side PHP script in the first post, sorry if that was unclear

It is a morphic smalltalk message, yes  smile

[slightly off topic]MRN hasn't been online in a good two months, hasn't she?[/slightly off topic]

@Spark oh now i see but still due to implict type conversions i am quite scared of using "1" as a value check , aslo is the server side responce only 1 , no headers ???!??!?!?

and if thats a morphic message then I doubt there can be a work arround , because i dont think there is a mechanism like

try{

}catch(E){

}

in panther ? is it ??


[re_open_off_topic]

Yes she has , but i am sure shes working on something awesome and enjoying her timeout  smile 
[/re_open_off_topic]

Last edited by fanofcena (2012-02-09 10:56:47)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#5 2012-02-09 11:00:47

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

It most certainly is returning only 1 because providing the internet connection is fine it works perfectly. I'm trying to get the catch message to go away, it doesn't need to check the connection again with the user's consent.

ping.php

Code:

<?php
//ping script for game connection testing
//1 = connection success, data recieved.
//2 = connection success, data NOT recieved.
if($_GET['ping'] != ""){
    echo "1";
}
if($_GET['ping'] == ""){
    echo "2";
}
?>

http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#6 2012-02-09 11:07:19

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: ping test destroys program startup

What exactly happens when there it no internet connection?

EDIT: Looks like there is a squeak error.

Use this

Code:

[
    "Connection code here"
] onError: [   "connection failed code here"   ].

I think that is right...

Last edited by MathWizz (2012-02-09 11:11:12)


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#7 2012-02-09 11:15:13

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: ping test destroys program startup

@Sparks .. sorry offtopic but I told Mrn about you talking bout her on skype and MRN says she was loaded a lot because of schoolwork and had to go offline for a while , she is very sorry for abondoning the collaboration you two were working on .
Oh and she will be back soon!


Coming back to topic..

You server will surely still send a bunch of headers as response but i think they gets parsed [hopefully].


and @MathWizz you are a lifesavour!! lol

Last edited by fanofcena (2012-02-09 11:20:07)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#8 2012-02-09 11:20:15

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

ping     r

Code:

|t1|
[ t1_'http://www.accidentalgames.com/ping.php?ping=test'.
^ self readFromUrl:  t1. ] onError: [ ^'error'  ].

Doesn't work. Reports error.

@fanofecta, oh! Thanks for contacting her, I was just a bit worried something had happened to her, so that's cool  smile  Tell her I look forward to her return. Also, you're right, some servers do automatically generate header code, such as site11, and those appear in Panther block reads, but for some reason my site is happy to not add things to my HTML for me, possibly because I paid for the site....

Last edited by sparks (2012-02-09 11:24:08)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#9 2012-02-09 11:27:31

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: ping test destroys program startup

Sorry, it's ifError, not onError.


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#10 2012-02-09 12:03:22

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: ping test destroys program startup

sparks wrote:

ping     r

Code:

|t1|
[ t1_'http://www.accidentalgames.com/ping.php?ping=test'.
^ self readFromUrl:  t1. ] onError: [ ^'error'  ].

Doesn't work. Reports error.

@fanofecta, oh! Thanks for contacting her, I was just a bit worried something had happened to her, so that's cool  smile  Tell her I look forward to her return. Also, you're right, some servers do automatically generate header code, such as site11, and those appear in Panther block reads, but for some reason my site is happy to not add things to my HTML for me, possibly because I paid for the site....

@sparks no problem  smile  , and well i have my bad-expereinces with PHP , you can open a C socket to a PHP server sometime and see what output any page puts its usually a couple of HTTP headers and then the messages , i think Squeak has pretty higher level sockets or HTTP communication and hence it parses them out  [ Keep me updated on ur project seems like something intresting to me ... I look forward to see it in action ]    smile


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#11 2012-02-09 12:06:18

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

fanofcena wrote:

[ Keep me updated on ur project seems like something intresting to me ... I look forward to see it in action ]    smile

You can choose to watch the release of the project by entering your email or username on my website page herewink

Math, the block doesn't appear to treat it as an error. It simply brings up the same box, then reports "could not resolve server at www.accidentalgames.com".

Last edited by sparks (2012-02-09 12:11:05)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#12 2012-02-09 18:29:13

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: ping test destroys program startup

The method causing the problem is Network-Protocols > HTTPSocket > get the page > httpGetDocument:args:accept:request:.   smile

Offline

 

#13 2012-02-09 18:39:25

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

There's no way to solve this problem without editing the smalltalk code, then? I may need to move Panther copies later :S


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#14 2012-02-09 18:55:54

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: ping test destroys program startup

sparks wrote:

There's no way to solve this problem without editing the smalltalk code, then? I may need to move Panther copies later :S

Or you can let me edit the Smalltalk code for you  wink

Add this method to Network-Protocols > HTTPSocket > get the page:

Code:

noFailGetHTTPDoc: t1 args: t2 accept: t3 request: t4 
    | t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 |
    Socket initializeNetwork.
    t11 _ (t1 asLowercase beginsWith: 'http://')
                ifTrue: [t1 copyFrom: 8 to: t1 size]
                ifFalse: [t1].
    t11 _ t11 copyUpTo: $#.
    t5 _ t11 copyUpTo: $/.
    t12 _ t11 copyFrom: t5 size + 1 to: t11 size.
    (t5 includes: $:)
        ifTrue: 
            [t16 _ t5 indexOf: $:.
            t7 _ (t5 copyFrom: t16 + 1 to: t5 size) asNumber.
            t5 _ t5 copyFrom: 1 to: t16 - 1]
        ifFalse: [t7 _ self defaultPort].
    t12 size = 0 ifTrue: [t12 _ '/'].
    t2 ifNotNil: [t12 _ t12 , (self argString: t2)].
    HTTPProxyServer isNil
        ifTrue: 
            [t17 _ t5.
            t18 _ t7]
        ifFalse: 
            [t12 _ 'http://' , t5 , ':' , t7 printString , t12.
            t17 _ HTTPProxyServer.
            t18 _ HTTPProxyPort].
    self flag: #XXX.
    t6 _ NetNameResolver addressForName: t17 timeout: 20.
    3
        timesRepeat: 
            [t8 _ HTTPSocket new.
            t8 connectTo: t6 port: t18.
            (t8 waitForConnectionUntil: (self deadlineSecs: 30))
                ifFalse: 
                    [t8 destroy.
                    ^ 'Server ' , t17 , ' is not responding'].
            Transcript cr; cr; show: t1; cr.
            t8 sendCommand: 'GET ' , t12 , ' HTTP/1.0' , CrLf , (t3 ifNil: ['']
                        ifNotNil: ['ACCEPT: ' , t3 , CrLf]) , 'ACCEPT: text/html' , CrLf , HTTPBlabEmail , t4 , 'User-Agent: Squeak 1.31' , CrLf , 'Host: ' , t5 , ':' , t7 printString , CrLf.
            t13 _ t8 getResponseUpTo: CrLf , CrLf ignoring: (String with: CR).
            t9 _ t13 at: 1.
            t14 _ t13 at: 3.
            t9 isEmpty
                ifTrue: [t15 _ 'server aborted early']
                ifFalse: 
                    [t8 header: t9.
                    t10 _ t8 getHeader: 'content-length'.
                    t10 ifNotNil: [t10 _ t10 asNumber].
                    t19 _ t8 getHeader: 'content-type'.
                    t8 responseCode first = $3
                        ifTrue: 
                            [t20 _ t8 getHeader: 'location'.
                            t20
                                ifNotNil: 
                                    [Transcript show: 'redirecting to ' , t20;
                                     cr.
                                    t8 destroy.
                                    ^ self
                                        httpGetDocument: t20
                                        args: t2
                                        accept: t3]].
                    t15 _ t8 getRestOfBuffer: t14 totalLength: t10.
                    t8 responseCode = '401' ifTrue: [^ t9 , t15 contents].
                    nil].
            t8 destroy.
            t15 class ~~ String ifTrue: [^ MIMEDocument
                    contentType: t19
                    content: t15 contents
                    url: t1].
            t15 = 'server aborted early' ifTrue: []]

Then use this code for the block (assumin t1 = the URL):

Code:

|t2 |
t2 _ (HTTPSocket noFailGetHTTPDoc: t1 args: nil accept: 'application/octet-stream' request: '') content.
(t1 isKindOf: String)
        ifTrue: [^ t1].
    ^ (RWBinaryOrTextStream with: t1 content) reset

Let me know if you have any problems.   smile

Offline

 

#15 2012-02-10 23:09:09

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: ping test destroys program startup

sparks wrote:

fanofcena wrote:

[ Keep me updated on ur project seems like something intresting to me ... I look forward to see it in action ]    smile

You can choose to watch the release of the project by entering your email or username on my website page herewink

wink  , done .. btw consider on using some HTML5  wink  for design


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#16 2012-02-11 02:40:36

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ping test destroys program startup

Thanks for watching! ... it is using HTML5, its not like I can use any other...

Last edited by sparks (2012-02-11 02:40:57)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

Board footer