 |
| View previous topic :: View next topic |
| Author |
Message |
WorldDrknss
Joined: 06 Jul 2007 Posts: 8
|
Posted: Fri Jul 06, 2007 2:36 pm Post subject: Client Loops |
|
|
I am new to php-gtk and was wondering if there is a way to have client loops without having the gui freezing until the loop has finished.
I am using Gtk::io_add_watch($socket,Gtk::IO_IN,'socket_receive'); for the server part of the but for the client side I need to do something like:
foreach($server as $srv){
connect to server;
send some data;
loop through received data;
match a string;
write info to database;
close connection;
}
TIA |
|
| Back to top |
|
 |
marc
Joined: 11 May 2006 Posts: 12 Location: Toulouse - France
|
Posted: Sat Jul 07, 2007 1:06 pm Post subject: |
|
|
there is 2 solutions :
- use io_add_watch and try not to block in an IO,
- fork some process with a pipe and control IO with that sub process. Child process need to notify IO and can read in blocking mode. This is a thread like implementation. Pear package has also a treading like framework. |
|
| Back to top |
|
 |
WorldDrknss
Joined: 06 Jul 2007 Posts: 8
|
Posted: Sat Jul 07, 2007 1:50 pm Post subject: |
|
|
I have the following:
| Code: | function pinger_start(){
$pinger = stream_socket_client('192.168.1.2:411', $errno, $errstr, 30);
Gtk::io_add_watch($pinger,Gtk::IO_IN,'pinger_receive');
global $PingerName;
$HUB_NICK = $PingerName->get_text();
$in = ("\$Key " . generate_key(fread($pinger, 73)) . "|");
$in .= ("\$BotINFO|");
$in .= ("\$HubINFO|");
$in .= ("\$ValidateNick $HUB_NICK|");
$in .= ("\$MyPass|");
$in .= ("\$Version 1.0|");
$in .= ("\$MyINFO \$ALL $HUB_NICK Import BOT <++ V:0.699,M:A,H:1/1/17,S:5>\$ \$0.5 \$NoMail@Hotmail.com\$0\$|");
$in .= ("<$HUB_NICK> Pinger Connected|");
$in .= ("\$Quit <$HUB_NICK>|");
fwrite($pinger, $in, strlen($in));
function pinger_receive($stream, $condition){
global $buffer;
global $console;
$iter = $buffer->get_end_iter();
$out = fread($stream, 2048);
if(preg_match('/\|\$HubINFO\s(.+\$)\|/', $out, $matches)){
$info = explode('$', $matches[0]);
$hubname = str_replace('HubINFO ', '', $info[1]); //Hub Name
$hubaddress = $info[2]; //Hub Address
$hubdescription = $info[3]; //Hub Description
$maxusers = $info[4]; //Max Users
$minshare = $info[5]; //Min Share
$minslot = $info[6]; //Min Slots
$maxhubs = $info[7]; //Max Hubs
$hubversion = $info[8]; //hub version
$log = date("[h:i:s]");
$buffer->insert($iter, $log." Pinged-> ".$hubaddress."\n");
$console->scroll_to_mark($buffer->get_insert(), 0);
fclose($stream);
}
if(preg_match('/\|\$BotINFO\s(.+\$)\|/', $out, $matches)){
$info = explode('$', $matches[0]);
$hubname = str_replace('HubINFO ', '', $info[1]); //Hub Name
$hubaddress = $info[2]; //Hub Address
$hubdescription = $info[3]; //Hub Description
$maxusers = $info[4]; //Max Users
$minshare = $info[5]; //Min Share
$minslot = $info[6]; //Min Slots
$maxhubs = $info[7]; //Max Hubs
$hubversion = $info[8]; //hub version
$log = date("[h:i:s]");
$buffer->insert($iter, $log." Pinged-> ".$hubaddress."\n");
$console->scroll_to_mark($buffer->get_insert(), 0);
fclose($stream);
}
Gtk::io_add_watch($stream,Gtk::IO_IN,'pinger_receive');
}
Gtk::timeout_add(60000, 'pinger_start');
} |
to do some testing, but it always come as so many bytes of buffer was lost and when it comes time to ping again, it says cannot redeclare pinger_receive |
|
| Back to top |
|
 |
WorldDrknss
Joined: 06 Jul 2007 Posts: 8
|
Posted: Sat Jul 07, 2007 1:54 pm Post subject: |
|
|
I would not know how to do option 2, - fork some process with a pipe and control IO with that sub process. Child process need to notify IO and can read in blocking mode. This is a thread like implementation. Pear package has also a treading like framework.
If you can supply an example of option two that would be great. |
|
| Back to top |
|
 |
marc
Joined: 11 May 2006 Posts: 12 Location: Toulouse - France
|
Posted: Sat Jul 07, 2007 2:11 pm Post subject: |
|
|
| WorldDrknss wrote: |
| Code: | function pinger_start(){
...
function pinger_receive($stream, $condition){
Gtk::timeout_add(60000, 'pinger_start');
}
}
|
it says cannot redeclare pinger_receive
|
do you realize you have writen a embeded function. I have never made that. Even I don't know if it is possible in php. |
|
| Back to top |
|
 |
marc
Joined: 11 May 2006 Posts: 12 Location: Toulouse - France
|
Posted: Sat Jul 07, 2007 2:15 pm Post subject: |
|
|
| WorldDrknss wrote: |
| Code: |
$out = fread($stream, 2048);
} |
|
should be some thing like that :
| Code: | $buffer = '';
while(!eof($stream)){
$data .= fread($stream, 1024*8);
if($data !== false)
$buffer .= $data;
} |
-> wrong is't blocking IO ... see next post
Last edited by marc on Sat Jul 07, 2007 3:27 pm; edited 1 time in total |
|
| Back to top |
|
 |
WorldDrknss
Joined: 06 Jul 2007 Posts: 8
|
Posted: Sat Jul 07, 2007 2:21 pm Post subject: |
|
|
| marc wrote: | | WorldDrknss wrote: |
| Code: |
$out = fread($stream, 2048);
} |
|
should be some thing like that :
$buffer = '';
while(!eof($stream)){
$data .= fread($stream, 1024* ;
if($data !== false)
$buffer .= $data;
} |
This causes the GUI to freeze until the loop as finished, but then again I did that without adding the IO watch.
Last edited by WorldDrknss on Sat Jul 07, 2007 2:24 pm; edited 1 time in total |
|
| Back to top |
|
 |
WorldDrknss
Joined: 06 Jul 2007 Posts: 8
|
Posted: Sat Jul 07, 2007 2:22 pm Post subject: |
|
|
| marc wrote: | | WorldDrknss wrote: |
| Code: | function pinger_start(){
...
function pinger_receive($stream, $condition){
Gtk::timeout_add(60000, 'pinger_start');
}
}
|
it says cannot redeclare pinger_receive
|
do you realize you have writen a embeded function. I have never made that. Even I don't know if it is possible in php. |
Do you know another alternative. I need that function to run at a certain time interval. Eventually it will be run every 12 hours. |
|
| Back to top |
|
 |
marc
Joined: 11 May 2006 Posts: 12 Location: Toulouse - France
|
Posted: Sat Jul 07, 2007 3:24 pm Post subject: |
|
|
| WorldDrknss wrote: |
| Code: | $buffer = '';
while(!eof($stream)){
$data .= fread($stream, 1024*8);
if($data !== false)
$buffer .= $data;
}
|
This causes the GUI to freeze until the loop as finished, but then again I did that without adding the IO watch. |
yes I was wrong. You need :
- to wait readding from io_add_watch notification,
- read as much as possible of data : $data = fread($stream, 1024* ;
- collect data into a buffer,
- verify that $data is OK ( if($data == false)
- if $data is not completed (need some more data), you need to wait an other IO event,
- you can try if(!eof($stream)), but it's not allway working with sockets and streams
- so you should have a protocol with message length or some thing to know when a message is fully available.
When a message is available, you can call a callback. All this work can be achieved if object oriented design but it will be a bit difficult if not.
Again, my visualProxy software has some classes for that task.
What is that ping protocole ? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
 |