artem78 has asked for the wisdom of the Perl Monks concerning the following question:

I`m writing simple xmpp client using Net::XMPP and Win32::GUI. I want to know how to make recieving loop assynchronous.

First I try to moved loop to another thread. But it fails with many errors:

Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value within %status in numeric eq (==) at C:/str +awberry/perl/site/lib/XML/Stream.pm line 1506. Use of uninitialized value in subtraction (-) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1507. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1669. Use of uninitialized value in numeric eq (==) at C:/strawberry/perl/si +te/lib/XML/Stream.pm line 1673. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/XML/Stream.pm line 1439. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1440. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. Use of uninitialized value in hash element at C:/strawberry/perl/site/ +lib/XML/Stream.pm line 1443. ....

So I use Win32::GUI::Timer, which call $client->Process(1) every 3 seconds. But function need some time to execute, so window freezes for a moment. How can I avoid these and call Process() asynchronously?

use Win32::GUI(); use Net::XMPP; use feature 'say'; my $wnd = Win32::GUI::Window->new( -size => [320, 240] ); $wnd->AddTextfield( -name => 'messages', -size => [$wnd->ScaleWidth, 130], -pos => [0, 0], -multiline => 1 ); $wnd->AddTextfield( -name => 'new_message', -size => [$wnd->ScaleWidth, 20], -pos => [0, 140], -multiline => 1 ); $wnd->AddButton( -text => 'Send', -size => [80, 24], -pos => [$wnd->ScaleWidth / 2 - 40, 170], -onClick => \&send_message ); $wnd->Center; $wnd->Show; my $client = new Net::XMPP::Client; $client->SetCallBacks( message => sub { my ($mid, $message) = @_; say 'message recieved'; $wnd->messages->Text($wnd->messages->Text() . '[' . $message-> +GetFrom('jid')->GetUserID . '] ' . $message->GetBody . "\r\n") } ); my $result = $client->Connect( hostname => 'xmpphostname', port => '5222', timeout => 7, ssl => 0, tsl => 0 ); if ($result) { $client->AuthSend( username => 'user1', password => '123' ); } if ($client->Connected) { say 'connected to server'; } else { say 'error connecting to server: ' . $!; exit 0; } my $pres = Net::XMPP::Presence->new(); $pres->SetType('available'); $client->Send($pres); my $timer = $wnd->AddTimer('timer', 3000); Win32::GUI::Dialog(); sub timer_Timer() { if ($client->Connected) { $client->Connect() unless defined $client->Process(1); } else { $client->Connect() or die('cant connect'); } } sub send_message() { my $msg = $wnd->new_message->Text(); $client->MessageSend( to => 'user2@xmpphostname', type => 'chat', body => $msg ); $wnd->messages->Text($wnd->messages->Text() . "[me] $msg\r\n"); $wnd->new_message->Text(''); }

Replies are listed 'Best First'.
Re: XMPP client with Win32::GUI
by BrowserUk (Patriarch) on Jun 17, 2015 at 20:55 UTC

    Warning: there is a lot of guess work in this untested code. I don't have Net::XMPP (or even know what XMPP is), and don't have an XMPP server to connect to, even if I installed it.

    Here's my modified untested version of your program:

    use threads; ## provides async. use Thread::Queue; ## provides inter-thread communicatio +n. use Win32::GUI(); use Net::XMPP; use feature 'say'; my $Qrecv = new Thread::Queue; ## For passing received mesgs from the +client (main) thread to the gui thread. my $Qsend = new Thread::Queue; ## For passing msgs to send from the gu +i thread to the client thread sub timer_Timer() { while( $Qrecv->pending ) { + ## if/while there are inbound messages my $text = $Qrecv->dequeue; + ## dequeue them $wnd->messages->Text( $wnd->messages->Text() . $text . "\r\n" +); ## add them to the gui. } } sub send_message() { my $msg = $wnd->new_message->Text(); + ## get the user input $Qsend->enqueue( $msg ); + ## queue it to the client $wnd->messages->Text($wnd->messages->Text() . "[me] $msg\r\n"); + ## add it to the window $wnd->new_message->Text(''); + ## clear the input field } ## All the gui code lives in here and runs asynchronously to the main +thread. my $thread = async { my $wnd = Win32::GUI::Window->new( -size => [320, 240] ); $wnd->AddTextfield( -name => 'messages', -size => [$wnd->ScaleWidt +h, 130], -pos => [0, 0], -multiline => 1 ); $wnd->AddTextfield( -name => 'new_message', -size => [$wnd->ScaleW +idth, 20], -pos => [0, 140], -multiline => 1 ); $wnd->AddButton( -text => 'Send', -size => [80, 24], -pos => [$wnd +->ScaleWidth / 2 - 40, 170], -onClick => \&send_message ); $wnd->Center; $wnd->Show; my $timer = $wnd->AddTimer('timer', 100); ## Check for message eve +ry 1/10th of a second Win32::GUI::Dialog(); ## process the dialog until it closes. }; my $client = new Net::XMPP::Client; $client->SetCallBacks( message => sub { my ($mid, $message) = @_; say 'message recieved'; $Qrecv->enqueue( + ## queue the formated input msg to the gui th +read '[' . $message->GetFrom('jid')->GetUserID . '] ' . $mes +sage->GetBody ); } ); my $result = $client->Connect( hostname => 'xmpphostname', port => '52 +22', timeout => 7, ssl => 0, tsl => 0 ); if ($result) { $client->AuthSend( username => 'user1', password => '123' ); } if ($client->Connected) { say 'connected to server'; } else { say 'error connecting to server: ' . $!; exit 0; } my $pres = Net::XMPP::Presence->new(); $pres->SetType('available'); $client->Send($pres); while( my $msg = $Qsend->dequeue ) { $client->MessageSend( to => 'user2@xmpphostname', type => 'chat', +body => $msg ); } =comment Don't know what to do with this bit??? Is it necessary? if ($client->Connected) { $client->Connect() unless defined $client->Process(1); } else { $client->Connect() or die('cant connect'); } =cut

    Once again; that is untested. You will almost certainly have to correct some of my errors. Yell if you need help.

    Please don't be upset by my brutal reformatting of your very nicely formatted code; I just needed to see everything on the screen at once in order to make a guess at what changes were required.

    Some explanation: essentially, I've just decoupled the two parts of your application using a pair of queues to pass messages between them.

    At the moment, the only way to terminate the program will be ^C or ^break. That can be addressed, but I didn't want to complicate it with more guesswork.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Looking at it a bit more the final loop in the main code should probably be more like this:

      while( 1 ) { $client->Connect() unless defined $client->Process(1); while( $Qsend->pending ) { my $msg = $Qsend->dequeue; $client->MessageSend( to => 'user2@xmpphostname', type => 'cha +t', body => $msg ); } }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked