| 1 | #!/usr/bin/perl | ||||||||||
| 2 | # OxdClientSocket.pm, a number as an object | ||||||||||
| 3 | |||||||||||
| 4 | ##################################### | ||||||||||
| 5 | # Client Script: | ||||||||||
| 6 | # Copyright 2016 (c) Ourdesignz | ||||||||||
| 7 | # this program is distributed according to | ||||||||||
| 8 | # the terms of the Perl license | ||||||||||
| 9 | # Use at your own risk | ||||||||||
| 10 | ##################################### | ||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | package OxdClientSocket; # This is the "Class" | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | $VERSION = '0.01'; | |||||||||
| 18 | # makes all attributes available | ||||||||||
| 19 | 1 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use lib './modules'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use Attribute::Handlers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | #use strict; | ||||||||||
| 23 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | use JSON::PP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use Data::Dumper qw(Dumper); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 1 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use OxdPackages::OxdConfig; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | #use IO::Socket::Socks; | ||||||||||
| 33 | 1 | ▶ | use IO::Socket::SSL qw(debug0); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | #use Sys::Hostname; | ||||||||||
| 35 | #$| = 1; | ||||||||||
| 36 | 1 | ▶ | use constant BUFSIZE => 1024; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 0 | my $class = shift; | |||||||||
| 40 | 0 | my $self = { | |||||||||
| 41 | # @static | ||||||||||
| 42 | # @var object $socket Socket connection | ||||||||||
| 43 | _socket => shift, | ||||||||||
| 44 | |||||||||||
| 45 | # @var string $base_url Base url for log file directory and oxd-rp-setting.json file. | ||||||||||
| 46 | _base_url => dirname(__FILE__) | ||||||||||
| 47 | |||||||||||
| 48 | }; | ||||||||||
| 49 | |||||||||||
| 50 | 0 | bless $self, $class; | |||||||||
| 51 | 0 | return $self; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # Sending request to oxd server via socket | ||||||||||
| 55 | # | ||||||||||
| 56 | # @param string $data | ||||||||||
| 57 | # @param int $char_count | ||||||||||
| 58 | # @return object | ||||||||||
| 59 | 0 | ▶ | sub oxd_socket_request{ | ||||||||
|
Subroutine
oxd_socket_request: not called CC=7 SCAR=40.3
| |||||||||||
| 60 | 0 | my ($self,$data, $char_count) = @_; | |||||||||
| 61 | 0 | ▶ | $char_count = $char_count ? $char_count : 8192; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | #print $data; | ||||||||||
| 64 | 0 | my $oxdConfig = OxdConfig->new(); | |||||||||
| 65 | 0 | my $op_host = $oxdConfig->{'_op_host'}; | |||||||||
| 66 | 0 | my $oxd_host_port = $oxdConfig->{'_oxd_host_port'}; | |||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | $socket = new IO::Socket::INET ( PeerHost => '127.0.0.1', PeerPort => $oxd_host_port, Proto => 'tcp', Reuse => 1) or die "$!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | if (!$socket) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | $self->log("Client: socket::socket_connect is not connected, error: ",$!); | |||||||||
| 71 | 0 | die $!; | |||||||||
| 72 | }else{ | ||||||||||
| 73 | 0 | $self->log("Client: socket::socket_connect ", "socket connected"); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | if(!($socket->syswrite($data, length($data)))){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | $self->log("Client: socket::socket_connect ", "Error writing"); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | $self->log("Client: oxd_socket_request", $socket->syswrite($data, length($data))); | |||||||||
| 81 | 0 | $socket->syswrite($data, length($data)); | |||||||||
| 82 | |||||||||||
| 83 | 0 | sysread($socket, $result, $char_count); | |||||||||
| 84 | #print "$result\n"; | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | if($result){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | $self->log("Client: oxd_socket_response", $result); | |||||||||
| 88 | }else{ | ||||||||||
| 89 | 0 | $self->log("Client: oxd_socket_response", 'Error socket reading process.'); | |||||||||
| 90 | } | ||||||||||
| 91 | 0 | ▶ | if(close($socket)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | $self->log("Client: oxd_socket_connection", "disconnected."); | |||||||||
| 93 | } | ||||||||||
| 94 | 0 | return $result; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | # Showing errors and exit. | ||||||||||
| 98 | # @param string $error | ||||||||||
| 99 | # @return void | ||||||||||
| 100 | 0 | ▶ | sub error_message{ | ||||||||
|
Subroutine
error_message: not called CC=1 SCAR=6.9
| |||||||||||
| 101 | 0 | my ($self, $error) = @_; | |||||||||
| 102 | 0 | print '<div class="alert alert-danger"> ' . $error.'</div>'; | |||||||||
| 103 | 0 | exit($error); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Saving process in log file. | ||||||||||
| 107 | # @param string $process | ||||||||||
| 108 | # @param string $message | ||||||||||
| 109 | # @return void | ||||||||||
| 110 | 0 | ▶ | sub log{ | ||||||||
|
Subroutine
log: not called CC=2 SCAR=17.9
| |||||||||||
| 111 | 0 | my ($self, $process, $message) = @_; | |||||||||
| 112 | |||||||||||
| 113 | 0 | my $t = localtime; | |||||||||
| 114 | 0 | my $timeStamp = $t->mdy("-");# 02/29/2000 | |||||||||
| 115 | |||||||||||
| 116 | 0 | my $fileName = "oxd-perl-server-$timeStamp.log"; | |||||||||
| 117 | |||||||||||
| 118 | 0 | my $datestring = localtime(); | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $filename = "logs/$fileName"; | |||||||||
| 121 | 0 | ▶ | open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | my $person = "\n".$datestring."\n".$process." ".$message."\n"; | |||||||||
| 123 | 0 | say $fh "$person\n"; | |||||||||
| 124 | 0 | close $fh; | |||||||||
| 125 | #say "done\n"; | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 1 | 1; # this 1; is neccessary for our class to work | |||||||||