-
Notifications
You must be signed in to change notification settings - Fork 1
/
evergreen_sip_client.pm
207 lines (181 loc) · 5.3 KB
/
evergreen_sip_client.pm
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
#!/usr/bin/perl
#
package evergreen_sip_client;
use DateTime;
use utf8;
use IO::Socket::INET;
use IO::Select;
use Data::Dumper;
use DateTime::Format::Duration;
our $lastdt;
sub new
{
my $class = shift;
my $self =
{
server => shift,
port => shift,
timeout => shift,
log => shift,
connection => shift,
lastx => '',
lastr => '',
lastxdt => undef
};
bless $self, $class;
return $self;
}
sub start
{
my $self = @_[0];
sipconnect($self);
return 1 if is_healthy($self);
return 0;
}
sub sipconnect
{
my $self = @_[0];
# auto-flush on socket
$| = 1;
$self->{connection}->close() if($self->{connection});
undef $self->{connection};
# print "SIP Connect\n".$self->{server}."\n".$self->{port}."\n".SOCK_STREAM;
# create a connecting socket
$self->{connection} = new IO::Socket::INET (
PeerHost => $self->{server},
PeerPort => $self->{port},
Proto => 'tcp',
Type => SOCK_STREAM
);
die "cannot connect to the server $!\n" unless $self->{connection};
my $socket = $self->{connection};
$socket->autoflush;
binmode ($socket, ":utf8");
# my $select = new IO::Select();
# $select->add($socket);
# sleep 2;
# print "connected to the server\n";
# print "sipclient:\n";
# print "sock->connected : ", ($self->{connection}->connected || ''), "\n";
# print "sock->protocol : ", ($self->{connection}->protocol || ''), "\n";
# print "sock->sockdomain : ", ($self->{connection}->sockdomain || ''), "\n";
# print "sock->socktype : ", ($self->{connection}->socktype || ''), "\n";
# print "sock->timeout : ", ($self->{connection}->timeout || ''), "\n";
# print "\n";
}
sub send
{
my $self = shift;
my $data = shift;
return _send($self,$data);
}
sub _send
{
my $self = shift;
my $data = shift;
while (!is_healthy($self))
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] We are not connected to the server, we are attempting to connect now");
sipconnect($self);
}
my $socket = $self->{connection};
$socket->autoflush;
my $select = new IO::Select();
$select->add($socket);
if($select->can_write(.25) )
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] sipclient sending '$data'");
$socket->send($data);
$self->{lastx} = $data;
$self->{lastxdt} = DateTime->now(time_zone => "local");
}
else
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] Unable to transmit");
return 'NOT CONNECTED\r';
}
my $response = "";
my $alldata = "";
$lastdt = DateTime->now(time_zone => "local");
my $skipdurationcheck = 0;
my $loops = 0;
while ( 1 ) #$socket->recv($data,1)
{
$| = 1;
$loops++;
$socket->autoflush;
if($select->can_read(.1))
{
$socket->recv($response,1024);
if($response ne '')
{
$lastdt = DateTime->now(time_zone => "local");
$skipdurationcheck = 1;
$self->{lastr} = $response;
$alldata.=$response;
$self->{log}->addLogLine("SIPCLIENT Thread[$$] alldata = $alldata");
if(length($alldata) > 0)
{
if ( (ord(substr($alldata,-1)) eq "10") || (ord(substr($alldata,-1)) eq "13") )
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] return carrage detected");
last;
}
}
}
else
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] Data was blank client $loops loops") if(($loops % 30) == 0);
$self->{log}->addLogLine("SIPCLIENT Thread[$$] data: $alldata\n") if(($loops % 30) == 0);
}
}
# Let's assume that we are not going to get data beyond what we already have
# after waiting for .5 second
if( ( duration($self) > .5 ) && $alldata ne '' )
{
$self->{log}->addLogLine("SIPCLIENT Thread[$$] it's been longer than .5 seconds and we have $alldata\n");
last;
}
if(!is_healthy($self)) {last;}
if( (duration($self) > $self->{timeout}) && !$skipdurationcheck )
{
# We're dead, let's seppuku
return "NOT CONNECTED\r";
}
}
# print "received response: $alldata\n";
return $alldata;
}
sub duration
{
my $self = shift;
my $afterProcess = DateTime->now(time_zone => "local");
my $difference = $afterProcess - $lastdt;
my $format = DateTime::Format::Duration->new(pattern => '%M:%S');
my $duration = $format->format_duration($difference);
my @s = split(/:/,$duration);
my $minutes = @s[0]+0;
my $seconds = @s[1]+0;
my $seconds = ($minutes*60) + $seconds;
return $seconds;
}
sub is_healthy
{
my $self = shift;
# print Dumper($self->{connection}->error);
return !$self->{connection}->error;
}
sub breakdown
{
my $self = shift;
$self->{connection}->close();
shutdown($self->{connection}, 2);
}
sub DESTROY
{
my $self = shift;
breakdown($self);
undef $self->{connection};
undef $self;
}
1;