summaryrefslogtreecommitdiff
path: root/ext/Amiga-ARexx/ARexx.pm
blob: 4fe2390d4aad69d6b8c3c9432fc65c606b99dee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package Amiga::ARexx;

use 5.016000;
use strict;
use warnings;
use Carp;

require Exporter;
#use AutoLoader;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration       use Amiga::Classes::ARexx ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
DoRexx
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
);

our $VERSION = '0.02';

require XSLoader;
XSLoader::load('Amiga::ARexx', $VERSION);

sub new
{
    my $class = shift;
    my $self = bless {}, $class;
    return $self->__init(@_);
}

sub __init
{
    my $self = shift;
    my %params = @_;
    my @tags = ();

    if(exists $params{'HostName'})
    {
        $self->{'__hostname'} = $params{'HostName'};
    } else { croak "HostName required";}

    $self->{'__host'} = Amiga::ARexx::Host_init($self->{'__hostname'});
    if (defined $self->{'__host'} && $self->{'__host'} != 0)
    {
    }
    else
    {
        croak "Unabel to initialise Arexx Host";
    }
    return $self;
}

sub wait
{
	my $self = shift;
	my %params = @_;
	my $timeout = -1;
	if ((exists $params{'TimeOut'}) && (defined $params{'TimeOut'}))
	{
		$timeout = $params{'TimeOut'};
		$timeout += 0; # force number
	}
	Amiga::ARexx::Host_wait($self->{'__host'},$timeout);

}

sub signal
{
	my $self = shift;
	return Amiga::ARexx::Host_signal($self->{'__host'});
}

sub getmsg
{
    my $self = shift;
    my $msg;
    my $msgobj;

    if(defined $self->{'__host'})
    {
    	$msg = Amiga::ARexx::Host_getmsg($self->{'__host'});
    	if($msg)
    	{
    	    $msgobj = Amiga::ARexx::Msg->new('Message' => $msg);
    	}
    }
    return $msgobj;
}

sub DESTROY
{
    my $self = shift;
    if(exists $self->{'__host'} && defined $self->{'__host'})
    {
        Amiga::ARexx::Host_delete($self->{'__host'});
        delete $self->{'__host'};
    }
}

sub DoRexx($$)
{
    my ($port,$command) = @_;
    my $rc = 0;
    my $rc2 = 0;
    my $result = Amiga::ARexx::_DoRexx($port,$command,$rc,$rc2);
    return ($rc,$rc2,$result);
}

package Amiga::ARexx::Msg;

use strict;
use warnings;
use Carp;

sub new
{
    my $class = shift;
    my $self = bless {}, $class;
    return $self->__init(@_);
}

sub __init
{
    my $self = shift;
    my %params = @_;

    if(exists $params{'Message'})
    {
        $self->{'__msg'} = $params{'Message'};
    } else { croak "Message required";}

    $self->{'__message'} = Amiga::ARexx::Msg_argstr($self->{'__msg'});
    return $self;
}

sub message
{
    my $self = shift;
    return $self->{'__message'};
}

sub reply($$$$)
{
    my ($self,$rc,$rc2,$result) = @_;
    if(exists $self->{'__msg'} && defined $self->{'__msg'})
    {
        Amiga::ARexx::Msg_reply($self->{'__msg'},$rc,$rc2,$result);
    }
}

sub setvar($$$)
{
    my ($self,$varname,$value) = @_;
    if(exists $self->{'__msg'} && defined $self->{'__msg'})
    {
        Amiga::ARexx::Msg_setvar($self->{'__msg'},$varname,$value);
    }
}

sub getvar($$)
{
    my ($self,$varname) = @_;
    if(exists $self->{'__msg'} && defined $self->{'__msg'})
    {
    	return Amiga::ARexx::Msg_getvar($self->{'__msg'},$varname);
    }
}

sub DESTROY
{
    my $self = shift;
    if(exists $self->{'__msg'} && defined $self->{'__msg'})
    {
        Amiga::ARexx::Msg_delete($self->{'__msg'});
        delete $self->{'__msg'};
    }
}

# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Amiga::ARexx - Perl extension for ARexx support

=head1 ABSTRACT

This a  perl class / module to enable you to use  ARexx  with
your perlscript. Creating a function host or executing scripts in other hosts.
The API is loosley modeled on the python arexx module supplied by with AmigaOS4.1

=head1 SYNOPSIS

    # Create a new host

    use Amiga::ARexx;
    my $host = Amiga::ARexx->new('HostName' => "PERLREXX" );                                                                          );

    # Wait for and process rexxcommands

    my $alive = 1;

    while ($alive)
    {
        $host->wait();
        my $msg = $host->getmsg();
        while($msg)
        {
            my $rc = 0;
            my $rc2 = 0;
            my $result = "";

            print $msg->message . "\n";
            given($msg->message)
            {
                when ("QUIT")
                {
                    $alive = 0;
                    $result = "quitting!";
                }
                default {
                    $rc = 10;
                    $rc2 = 22;
                }
            }
            $msg->reply($rc,$rc2,$result);

            $msg = $host->getmsg();
        }

    }

    # Send a command to a host

    my $port = "SOMEHOST";
    my $command = "SOMECOMMAND";
    my ($rc,$rc2,$result) = Amiga::ARexx->DoRexx($port,$command);



=head1 DESCRIPTION

The interface to the arexx.class in entirely encapsulated within the perl class, there
is no need to access the low level methods directly and they are not exported by default.

=head1 Amiga::ARexx METHODS

=head2 new

    my $host = Amiga::ARexx->new( HostName => "PERLREXX");                                                                            );


Create an ARexx host for your script / program.

=head3 HostName

The HostName for the hosts command port. This is madatory, the program will fail if not
provided.


=head2 wait

	$host->wait('TimeOut' => $timeoutinusecs );

Wait for a message to arive at the port.

=head3 TimeOut

optional time out in microseconds.


=head2 getmsg

    $msg = $host->getmsg();


Fetch an ARexx message from the host port. Returns an objrct of class Amiga::ARexx::Msg

=head2 signal

    $signal = $host->signal()

Retrieve the signal mask for the host port for use with Amiga::Exec Wait()

=head2 DoRexx

    ($rc,$rc2,$result) = DoRexx("desthost","commandstring");

Send the "commandstring" to host "desthost" for execution. Commandstring might be a specific command or scriptname.

=head1 Amiga::ARexx::Msg METHODS

=head2 message

	$m = $msg->message();

Retreive the message "command" as a string;


=head2 reply

	$msg->reply($rc,$rc2,$result)

Reply the message returning the results of any command. Set $rc = 0 for success and $result  to the result string if appropriate.

Set $rc to non zero for error and $rc2 for an additional error code if appropriate.

=head2 setvar

	$msg->setvar($varname,$value)

Set a variable in the language context sending this message.

=head2 getvar

    $value = $msg->getvar($varname)

Get the value of a variable in the language context sending this message.


=head2 EXPORT

None by default.

=head2 Exportable constants

None

=head1 AUTHOR

Andy Broad <andy@broad.ology.org.uk>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2013 by Andy Broad.

=cut