Re: How do you share an object(Telnet Sesion) between threads?

Steve Cookson - gmail <[email protected]> Wed, 30 Sep 2015 12:44:28 +0100
Newsgroups gmane.comp.lang.perl.wxperl
Message-ID <[email protected]>
Hi James,

I'm not sure what you already know, but any OO-style module should be of 
the form $obj=Foo::Bar->new();
$obj is a pointer that holds the class and the object as the first two 
variables and you should be able to access anything from it.

     use  Net::Telnet  ();
     $obj  =  Net::Telnet->new(Timeout=>  20);

Maybe you could take the while(1) out of the connect() sub routine and then

     return $telnetsession;

As the last line of the routine.

Here is an example of how it can work:

http://www.perlfect.com/articles/telnet.shtml

I also suggest that while(1), is a bit uncompromising, 
while(!$common{connected}) would be more controlled and using a for loop 
to try a limited number of times, even more so.

Good luck,

Regards

Steve.

On 29/09/15 18:35, James Lynes wrote:
> How do you share an object(Telnet Sesion) between threads?
>
> I need the Telnet session below available to 3 threads(connect, 
> disconnect, & scan). $telnetsession is of type Net::Telnet per 
> print(ref($telnetsession)).
>
> This is part of a larger wxPerl script. All of the display and 
> interlocking between the GUI and threads seems to working corectly.
>
> Crossposted to wxperl-users.
>
> Thanks,
> James
>
> <code>
> # Define the Thread shared data area - All threads access this scaler 
> data fine
> my  %common : shared;
>     $common{ip} = "127.0.0.1";        # Localhost
>     $common{port} = "7356";        # Local Port as defined in gqrx
>     $common{telnetsession} = 0;        # Object Pointer ??? Problem ???
>     $common{tnerror} = 0;        # Status
>     $common{connect} = 0;        # Command
>     $common{connected} = 0;        # Status
>     $common{disconnect} = 0;        # Command
>     $common{scanstart} = 0;        # Command
>     $common{scanstarted} = 0;        # Status
>     $common{beginf} = 0;        # Scan - Beginning Frequency
>     $common{endf} = 0;            # Scan - Ending Frequency
>     $common{step} = 0;            # Scan - Frequency Step
>     $common{squelch} = 0;        # Scan - Minimum RSSI(Recieved Signal 
> Strength Indicator)
>     $common{rssi} = 0;            # Scan - Latest RSSI
>     $common{pause} = 0;            # Scan - Time between scans - msec
>     $common{listen} = 0;        # Scan - Time to Listen to a strong 
> signal - msec
>     $common{mode} = 0;            # Scan - Demodulator Type
>     $common{stopthreads} = 0;        # Command
>
>
> # Connect the Telnet Session - #1
> sub Connect {
>     while(1) {
>         if($common{stopthreads}) {print "\nConnect Thread 
> Terminated\n"; return};
>         if($common{connect}) {
>             if(!$common{connected}) {
>                print "Open Telnet Connection to gqrx\n";
>                my $telnetsession = Net::Telnet->new(Timeout => 2, port 
> => $common{port},
>                                         Errmode => sub 
> {$common{tnerror} = 1;});
>                $telnetsession->open($common{ip});
>                $common{telnetsession} = 
> shared_clone($telnetsession);      # Line 92 Errors
>                $common{connected} = 1;
>                $common{connect} = 0;
>             }
>         }
>         threads->yield();
>     }
> }
> </code>
>
> <code>
> Error message:
> Thread 1 terminated abnormally: Unsupported ref type: GLOB at 
> ./threadedgqrxLite.pl line 92 thread 1
> </code>
>
>