This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Run a proc delayed

Help for those learning Tcl or writing their own scripts.
Post Reply
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Run a proc delayed

Post by Elfriede »

Hi : Its me again ^^

Here's my code:

Code: Select all

} elseif {$http_status == "timeout"} {
	sendmsg # "\002\00304\[ERROR\]\002\003 \002Connection timed out - $the_url ... Retrying ..."
	set http_data [::http::data $http_handle]
	::http::cleanup $http_handle
	set File [::mysql::escape [getPage $the_url]]
	.....

proc getPage { the_url } {
	::http::register https 443 ::tls::socket
	set token [::http::geturl $the_url -binary 1 -timeout 10000]
	set data [::http::data $token]
	::http::cleanup $token
	return $data
}
As u can see i have a timeout of 10s in the getPage proc. What i'm looking for is that this part:
set File [::mysql::escape [getPage $the_url]]
would be called for eg lets say 20 seconds later, instead of needing/using such a high timeout in my getPage proc. Everything ive tried till yet ended up in errors :/
d
doggo
Halfop
Posts: 97
Joined: Tue Jan 05, 2010 7:53 am
Contact:

Post by doggo »

Code: Select all

} elseif {$http_status == "timeout"} { 
   sendmsg # "\002\00304\[ERROR\]\002\003 \002Connection timed out - $the_url ... Retrying ..." 
   set http_data [::http::data $http_handle] 
   ::http::cleanup $http_handle 
   set File [::mysql::escape [getPage $the_url]] 
   ..... 

proc getPage { the_url } { 

	putlog "wait"
	after 3000 ;# Simulates deep thought
	putlog "now"

   ::http::register https 443 ::tls::socket 
   set token [::http::geturl $the_url -binary 1 -timeout 10000] 
   set data [::http::data $token] 
   ::http::cleanup $token 
   return $data 
} 
you could give this a go :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I would not recommend using the after command in blocking mode, as eggdrop is single-threaded, and blocking behavior prevents the eggdrop from taking any actions at all during the block.

I'd rather recommend using the utimer command to schedule a piece of code to be executed at a later time, or if possible, use the -command option with your http-request to use an event-driven mode:

Code: Select all

::http::register https 443 ::tls::socket 
::http::geturl "http://www.example.com/" -binary 1 -command handle_response

proc handle_response {token} {
  if {[::http::status $token] == "ok"} {
    set File [::mysql::escape [::http::data $token]]
    utimer 20 [list handle_final $File]
  }
  ::http::cleanup
}

proc handle_final {File} {
  #this is called 20 seconds after the http-transaction is completed...
  putlog "The MySQL-escaped filename was $File"
}
NML_375
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

I really appreciate ur answer and im quite sure i do "understand", but im unsure how to use that on my proc. Actually it looks like:

Code: Select all

::http::register https 443 ::tls::socket
if {![catch {set http_handle [http::geturl $the_url -binary 1 -timeout $::check(timeout)]} error]} {
	set http_status [http::status $http_handle]
	if {$http_status == "ok"} {
		if {[http::ncode $http_handle] == 200} {
			set http_data [::http::data $http_handle]
			::http::cleanup $http_handle
			set File [::mysql::escape $http_data]
			::mysql::encoding $db_handle binary
			set sql [::mysql::sel $db_handle "INSERT INTO ...
		}
	} elseif {$http_status == "timeout"} {
		sendmsg $channel "\002\00304\[ERROR\]\002\003 \002Connection timed out - $the_url ... Retrying ..."
		set http_data [::http::data $http_handle]
		::http::cleanup $http_handle
		set File [::mysql::escape [getPage $the_url]]

		::mysql::encoding $db_handle binary
		set sql [::mysql::sel $db_handle "INSERT INTO...

	} elseif {[http::ncode $http_handle] == 404} {
The first part, as shown here, uses a timeout of 3 seconds. In case of timeout, ur example code should retry xx seconds delayed, but i dunno the best way to use it. May u please help :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Elfriede,
What you need to do, is to split your proc up into several pieces, thus allowing you to call the second http-transaction separately from the first one using a timer.
NML_375
g
game_over
Voice
Posts: 29
Joined: Thu Apr 26, 2007 7:22 am

Post by game_over »

And why not call your procedure again. I mean

Code: Select all

proc somename {args} {
....
::http::register https 443 ::tls::socket
if {![catch {set http_handle [http::geturl $the_url -binary 1 -timeout $::check(timeout)]} error]} {
   set http_status [http::status $http_handle]
   if {$http_status == "ok"} {
      if {[http::ncode $http_handle] == 200} {
         set http_data [::http::data $http_handle]
         ::http::cleanup $http_handle
         set File [::mysql::escape $http_data]
         ::mysql::encoding $db_handle binary
         set sql [::mysql::sel $db_handle "INSERT INTO ...
      }
   } elseif {$http_status == "timeout"} {
      sendmsg $channel "\002\00304\[ERROR\]\002\003 \002Connection timed out - $the_url ... Retrying ..."
     somename $args; #you have already defined all that you need

   } elseif {[http::ncode $http_handle] == 404} { 
......
}
if you use you method you must re-define error like -timeout again and again

, and nml375 is right after block eggdrop for some time but -timeout option all so use after in http.tcl
Post Reply