/
KIWISocket.pm
161 lines (152 loc) · 3.67 KB
/
KIWISocket.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
#================
# FILE : KIWISocket.pm
#----------------
# PROJECT : OpenSUSE Build-Service
# COPYRIGHT : (c) 2006 SUSE LINUX Products GmbH, Germany
# :
# AUTHOR : Marcus Schaefer <ms@suse.de>
# :
# BELONGS TO : Operating System images
# :
# DESCRIPTION : This module is used to provide socket
# : operations which can be used for setting
# : up a milestone server
# :
# :
# STATUS : Development
#----------------
package KIWISocket;
#==========================================
# Modules
#------------------------------------------
use strict;
use warnings;
use base qw (Exporter);
use Carp qw (cluck);
use FileHandle;
use Socket;
use KIWIQX qw (qxx);
my @EXPORT_OK = qw ();
#==========================================
# Constructor
#------------------------------------------
sub new {
# ...
# Create a new KIWISocket object which is used to create
# a server socket for communication to the outside. The module
# provides methods for writing reports based on XML and is used
# to provide milestone information of the kiwi process
# ---
#==========================================
# Object setup
#------------------------------------------
my $this = {};
my $class = shift;
bless $this,$class;
#==========================================
# Module Parameters
#------------------------------------------
my $kiwi = shift;
my $port = shift;
#==========================================
# Constructor setup
#------------------------------------------
$port = int $port;
if ($port <= 0) {
return;
}
my $server = $this -> serverSocket ($port);
if (! defined $server) {
return;
}
#==========================================
# Store object data
#------------------------------------------
$this->{kiwi} = $kiwi;
$this->{port} = $port;
$this->{server} = $server;
return $this;
}
#==========================================
# serverSocket
#------------------------------------------
sub serverSocket {
my $this = shift;
my $port = shift;
my $proto = getprotobyname("tcp");
socket(SD, PF_INET, SOCK_STREAM, $proto) ||
return;
setsockopt(SD, SOL_SOCKET, SO_REUSEADDR, pack("l",1)) ||
return;
bind(SD, sockaddr_in($port,INADDR_ANY)) ||
return;
listen(SD,SOMAXCONN) ||
return;
return *SD;
}
#==========================================
# acceptConnection
#------------------------------------------
sub acceptConnection {
my $this = shift;
my $server = $this->{server};
my $paddr = accept (CD,$server);
if (! $paddr) {
return;
}
$this->{client} = *CD;
return $this->{client};
}
#==========================================
# closeConnection
#------------------------------------------
sub closeConnection {
my $this = shift;
my $client = $this->{client};
if (defined $client) {
close $client;
}
return;
}
#==========================================
# write
#------------------------------------------
sub writeTo {
my $this = shift;
my $message = shift;
my $client = $this->{client};
print $client $message;
flush $client;
return $this;
}
#==========================================
# read
#------------------------------------------
sub readFrom {
my $this = shift;
my $client = $this->{client};
if (! defined $client) {
return;
}
my $line = <$client>;
flush $client;
chop $line;
chop $line;
return $line;
}
#==========================================
# Destructor
#------------------------------------------
sub DESTROY {
my $this = shift;
my $client = $this->{client};
my $server = $this->{server};
if (defined $client) {
close $client;
}
if (defined $server) {
close $server;
}
return $this;
}
1;