:pserver:anonymous@intra.tektonica.com:/opt/cvs - gsmperl - Fri Dec 21 07:37 CET...
[gsmperl.git] / GSM / SMS / Transport.pm
1 package GSM::SMS::Transport;\r
2 use strict;\r
3 use vars qw( $VERSION );\r
4 \r
5 use Carp;\r
6 use GSM::SMS::Config;\r
7 use Data::Dumper;\r
8 \r
9 $VERSION = '0.11';\r
10 \r
11 # Constructo\r
12 ##########################################################################\r
13 sub new {\r
14         my $proto = shift;\r
15         my $class = ref($proto) || $proto;\r
16         my $self = {};  \r
17         bless($self, $class);   \r
18 \r
19         # process constructor parameters\r
20         $self->{"__CONFIG_FILE__"} = shift;\r
21         \r
22         # initialize class parameters\r
23         #       anonymous array to transports\r
24         $self->{"__TRANSPORTS__"} = [];\r
25         \r
26         # read config file\r
27         unless ( $self->{"__CONFIG__"} = read_config( $self->{"__CONFIG_FILE__"} )) {\r
28                 croak("Could not load config file : " . $self->{"__CONFIG_FILE__"} . "!");\r
29         }\r
30 \r
31         # print Dumper $self->{'__CONFIG__'};\r
32 \r
33         # initialise transport\r
34         my $ress = $self->_init( $self->{"__CONFIG__"} );\r
35         if ( $ress == -1) {\r
36                 return undef;\r
37         }\r
38         return $self;\r
39 }\r
40 \r
41 # Send\r
42 ##########################################################################\r
43 sub send {\r
44         my ($self, $msisdn, $pdu) = @_;\r
45 \r
46         my $transport = $self->_route($msisdn);\r
47         return $transport->send($msisdn, $pdu) if $transport;\r
48         return -1;\r
49 }\r
50 \r
51 # Receive\r
52 ##########################################################################\r
53 sub receive {\r
54         my ($self) = @_;\r
55         my $pdu;\r
56 \r
57         foreach my $transporter ( @{$self->get_transports()} ) {\r
58                 if (!$transporter->receive(\$pdu)) {\r
59                         return $pdu;\r
60                 }\r
61         }\r
62         return $pdu;\r
63 }\r
64 \r
65 # Get an array containing the transports\r
66 ##########################################################################\r
67 sub get_transports {\r
68         my $self = shift;\r
69         return $self->{"__TRANSPORTS__"};\r
70 }\r
71 \r
72 # Close\r
73 #       Clean up ...\r
74 ##########################################################################\r
75 sub close {\r
76         my $self = shift;\r
77         \r
78         foreach my $transport ( @{$self->{"__TRANSPORTS__"}} ) {\r
79                 $transport->close();\r
80         }\r
81 }\r
82 \r
83 ##########################################################################\r
84 # P R I V A T E\r
85 ##########################################################################\r
86 \r
87 # Route\r
88 #       Give us the handle to the correct transport to use\r
89 #       Implements the routing.\r
90 #       Routing is now only done by the 'prefix' config parameter\r
91 sub _route {\r
92         my ($self, $msisdn) = @_;\r
93         \r
94         # print "route... ";\r
95         foreach my $transport ( @{$self->{"__TRANSPORTS__"}} ) {\r
96                 # print "--> $transport\n";\r
97                 if ( $transport->has_valid_route($msisdn) ) {\r
98                         # print "found ($transport)\n";\r
99                         return $transport;\r
100                 }\r
101         }\r
102         # print "END\n";\r
103         return undef;\r
104 }\r
105 \r
106 # Init\r
107 #       Create the actual transports and initialize them\r
108 sub _init {\r
109         my ($self, $conf) = @_;\r
110 \r
111          # print Dumper $conf;\r
112 \r
113         foreach my $transport ( keys %$conf ) {\r
114                 next if $transport =~ /default/;\r
115 \r
116                  #print "T: $transport ... ";\r
117 \r
118                 # load transport class, using the preferences\r
119                 my $transport_config = get_config($conf, $transport);\r
120                 my $transport_type = $transport_config->{"type"};\r
121 \r
122                  # print Dumper $transport_config;\r
123         \r
124                  # print "starting up $transport_type..\n";\r
125         \r
126                 my $transport_instance = eval(\r
127                         "use GSM::SMS::Transport::$transport_type; my \$n = GSM::SMS::Transport::$transport_type->new(\$transport_config); return \$n;"\r
128                                                                          );\r
129 \r
130                  # print $@;\r
131 \r
132                 # print "use Tektonica::iSMS::Transport::$transport_type; my \$n = Tektonica::iSMS::Transport::$transport->new(\$transport_config); return \$n;";\r
133 \r
134                 # If we didn't succeeded in loading the transport, there is\r
135                 # sometghing wrong. better bail out now and inform user, \r
136                 # otherwise he can get the impression all is fine, while not!\r
137                 return -1 unless $transport_instance;\r
138                 \r
139                 push( @{$self->{"__TRANSPORTS__"}}, $transport_instance );\r
140                 # print "\n";\r
141         }\r
142         return 0;\r
143 }\r
144 \r
145 1;\r
146 \r
147 =head1 NAME\r
148 \r
149 GSM::SMS::Transport\r
150 \r
151 =head1 DESCRIPTION\r
152 \r
153 This can be best seen as a factory for the transports defined in the GSM::SMS::Transport::* modules.\r
154 When given a config file, it dynamically loads the transports defined in that config file, and initializes them.\r
155  \r
156 =head1 METHODS\r
157 \r
158 =head2 new( $configfile )\r
159 \r
160 Create a new transport layer with the settings as in the config file. Please look in the example config file for the transport specific configuration settings.\r
161 \r
162 =head2 send( $msisdn, $pdu )\r
163 \r
164 Send a PDU message to the the msisdn. The transport layer will choose a transport according to the regular expression defined in the config file. This regexp matches against the msisdn.\r
165 \r
166 =head2 $pdu = $t->receive()\r
167 \r
168 Receive a pdu message from the transport layer. Undef when no new message available.\r
169 \r
170 =head2 close()\r
171 \r
172 Shut down transport layer, calls the transport specific close method.\r
173 \r
174 \r
175 =head1 AUTHOR\r
176 \r
177 Johan Van den Brande <johan@vandenbrande.com>\r