| 1 | package POE::Component::Client::Bayeux::Transport::LongPolling; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use POE; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use HTTP::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use POE::Component::Client::Bayeux::Utilities qw(decode_json_response); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 3 | ▶ | use base qw(POE::Component::Client::Bayeux::Transport); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 0 | ▶ | sub extra_states { | ||||||||||||
|
Subroutine
extra_states: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 13 | # return an array of method names in this class that I want exposed | ||||||||||||||
| 14 | 0 | return ( qw( openTunnelWith tunnelResponse ) ); | |||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 0 | ▶ | sub check { | ||||||||||||
|
Subroutine
check: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 18 | 0 | my ($kernel, $heap, $types, $version, $xdomain) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2]; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 0 | ▶ | sub tunnelInit { | ||||||||||||
|
Subroutine
tunnelInit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 22 | 0 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | |||||||||||||
| 23 | |||||||||||||||
| 24 | # Allow parent class to do error checking | ||||||||||||||
| 25 | #$class->SUPER::tunnelInit(@_); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | my %connect = ( | ||||||||||||||
| 28 | channel => '/meta/connect', | ||||||||||||||
| 29 | clientId => $heap->{parent_heap}{clientId}, | ||||||||||||||
| 30 | 0 | connectionType => 'long-polling', | |||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | $kernel->yield('openTunnelWith', \%connect); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | sub openTunnelWith { | ||||||||||||
|
Subroutine
openTunnelWith: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 37 | 0 | my ($kernel, $heap, @messages) = @_[KERNEL, HEAP, ARG0 .. $#_]; | |||||||||||||
| 38 | 0 | my $pheap = $heap->{parent_heap}; | |||||||||||||
| 39 | 0 | $pheap->{_polling} = 1; | |||||||||||||
| 40 | |||||||||||||||
| 41 | # Ensure clientId is defined | ||||||||||||||
| 42 | 0 | foreach my $message (@messages) { | |||||||||||||
| 43 | 0 | $message->{clientId} = $pheap->{clientId}; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | $pheap->{client}->logger->debug(">>> LongPolling tunnel >>>\n".Dumper(\@messages)); | |||||||||||||
| 47 | |||||||||||||||
| 48 | # Create an HTTP POST request, encoding the messages into JSON | ||||||||||||||
| 49 | my $request = HTTP::Request->new('POST', $pheap->{remote_url}, | ||||||||||||||
| 50 | [ 'content-type', 'text/json' ], | ||||||||||||||
| 51 | 0 | $pheap->{json}->encode(\@messages), | |||||||||||||
| 52 | ); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | # Create a UUID so I can collect meta info about this request | ||||||||||||||
| 55 | 0 | my $uuid = $pheap->{uuid}->create_str(); | |||||||||||||
| 56 | 0 | $heap->{_tunnelsOpen}{$uuid} = { opened => time() }; | |||||||||||||
| 57 | |||||||||||||||
| 58 | # Use parent user agent to make request | ||||||||||||||
| 59 | 0 | $kernel->post( $pheap->{ua}, 'request', 'tunnelResponse', $request, $uuid ); | |||||||||||||
| 60 | |||||||||||||||
| 61 | # TODO: use $heap->{parent_heap}{advice}{timeout} as a timeout for this connect to reply | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub tunnelResponse { | ||||||||||||
|
Subroutine
tunnelResponse: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 65 | 0 | my ($kernel, $heap, $request_packet, $response_packet) = @_[KERNEL, HEAP, ARG0, ARG1]; | |||||||||||||
| 66 | 0 | my $pheap = $heap->{parent_heap}; | |||||||||||||
| 67 | 0 | $pheap->{_polling} = 0; | |||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | my $request_object = $request_packet->[0]; | |||||||||||||
| 70 | 0 | my $request_tag = $request_packet->[1]; # from the 'request' post | |||||||||||||
| 71 | 0 | my $response_object = $response_packet->[0]; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | my $meta = delete $heap->{_tunnelsOpen}{$request_tag}; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | my $json; | |||||||||||||
| 76 | 0 | eval { | |||||||||||||
| 77 | 0 | $json = decode_json_response($response_object); | |||||||||||||
| 78 | }; | ||||||||||||||
| 79 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | # Ignore errors if shutting down | ||||||||||||||
| 81 | 0 | ▶ | return if $pheap->{_shutdown}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | die $@; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | $pheap->{client}->logger->debug("<<< LongPolling tunnel <<<\n".Dumper($json)); | |||||||||||||
| 86 | |||||||||||||||
| 87 | 0 | foreach my $message (@$json) { | |||||||||||||
| 88 | 0 | $kernel->post( $heap->{parent}, 'deliver', $message ); | |||||||||||||
| 89 | 0 | ▶ | if ($message->{channel} eq '/meta/connect') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 0 | ▶ | $pheap->{advice} = $message->{advice} || {}; | ||||||||||||
|
Condition0%0/2
$message->{'advice'} || {}
MC/DC0%0/1
$message->{'advice'} || {}
$message->{'advice'}
Truth table0%0/2
A
A= $message->{'advice'}
| |||||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | $kernel->yield('tunnelCollapse'); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | ▶ | sub tunnelCollapse { | ||||||||||||
|
Subroutine
tunnelCollapse: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||
| 98 | 0 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | |||||||||||||
| 99 | 0 | my $pheap = $heap->{parent_heap}; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | my $reconnect; | |||||||||||||
| 102 | 0 | ▶ | if ($pheap->{advice}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | $reconnect = $pheap->{advice}{reconnect}; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | 0 | ▶ | if (delete $pheap->{_reconnect}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | $reconnect = 'handshake'; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | 0 | ▶ | if ($reconnect) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 0 | ▶ | if ($reconnect eq 'none') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 110 | 0 | die "Server asked us not to reconnect"; | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | elsif ($reconnect eq 'handshake') { | ||||||||||||||
| 113 | 0 | $pheap->{_initialized} = 0; | |||||||||||||
| 114 | 0 | $pheap->{_connected} = 0; | |||||||||||||
| 115 | 0 | $kernel->yield('_stop'); | |||||||||||||
| 116 | 0 | $kernel->post( $heap->{parent}, 'handshake' ); | |||||||||||||
| 117 | 0 | return; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 0 | ▶ | return if (! $pheap->{_initialized}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 0 | ▶ | if (delete $pheap->{_disconnect}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 0 | $pheap->{_connected} = 0; | |||||||||||||
| 124 | 0 | return; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 0 | ▶ | if ($pheap->{_polling}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 0 | $pheap->{client}->logger->debug("tunnelCollapse: Wait for polling to end"); | |||||||||||||
| 129 | 0 | return; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 0 | ▶ | if ($pheap->{_connected}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | my %connect = ( | ||||||||||||||
| 134 | channel => '/meta/connect', | ||||||||||||||
| 135 | clientId => $pheap->{clientId}, | ||||||||||||||
| 136 | 0 | connectionType => 'long-polling', | |||||||||||||
| 137 | ); | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | $kernel->yield('openTunnelWith', \%connect); | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | ▶ | sub sendMessages { | ||||||||||||
|
Subroutine
sendMessages: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 144 | 0 | my ($kernel, $heap, $messages) = @_[KERNEL, HEAP, ARG0]; | |||||||||||||
| 145 | 0 | my $pheap = $heap->{parent_heap}; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | foreach my $message (@$messages) { | |||||||||||||
| 148 | 0 | $message->{clientId} = $pheap->{clientId}; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | $pheap->{client}->logger->debug(">>> LongPolling >>>\n".Dumper($messages)); | |||||||||||||
| 152 | |||||||||||||||
| 153 | # Create an HTTP POST request, encoding the messages into JSON | ||||||||||||||
| 154 | my $request = HTTP::Request->new('POST', $pheap->{remote_url}, | ||||||||||||||
| 155 | [ 'content-type', 'text/json' ], | ||||||||||||||
| 156 | 0 | $pheap->{json}->encode($messages), | |||||||||||||
| 157 | ); | ||||||||||||||
| 158 | |||||||||||||||
| 159 | # Use parent user agent to make request | ||||||||||||||
| 160 | 0 | $kernel->post( $pheap->{ua}, 'request', 'deliver', $request ); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | ▶ | sub deliver { | ||||||||||||
|
Subroutine
deliver: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 164 | 0 | my ($kernel, $heap, $request_packet, $response_packet) = @_[KERNEL, HEAP, ARG0, ARG1]; | |||||||||||||
| 165 | 0 | my $pheap = $heap->{parent_heap}; | |||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | my $request_object = $request_packet->[0]; | |||||||||||||
| 168 | 0 | my $request_tag = $request_packet->[1]; # from the 'request' post | |||||||||||||
| 169 | 0 | my $response_object = $response_packet->[0]; | |||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | my $json = decode_json_response($response_object); | |||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | $pheap->{client}->logger->debug("<<< LongPolling <<<\n" . Dumper($json)); | |||||||||||||
| 174 | |||||||||||||||
| 175 | 0 | foreach my $message (@$json) { | |||||||||||||
| 176 | 0 | $kernel->post( $heap->{parent}, 'deliver', $message ); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | 3 | 1; | |||||||||||||