File Coverage

blib/lib/Dicom/DCMTK/DCMQRSCP/Config.pm
Criterion Covered Total %
statement 140 145 96.5
branch 45 54 83.3
condition 33 39 84.6
subroutine 11 11 100.0
pod 3 3 100.0
total 232 252 92.0


line stmt bran cond sub pod time code
1             package Dicom::DCMTK::DCMQRSCP::Config;
2              
3 5     5   182848 use strict;
  5         9  
  5         205  
4 5     5   25 use warnings;
  5         8  
  5         337  
5              
6 5     5   2780 use Class::Utils qw(set_params);
  5         78418  
  5         116  
7              
8             our $VERSION = 0.04;
9              
10             # Constructor.
11             sub new {
12 8     8 1 1198310 my ($class, @params) = @_;
13 8         25 my $self = bless {}, $class;
14              
15             # Defaults.
16 8         38 $self->_default;
17              
18             # Comment.
19 8         21 $self->{'comment'} = 1;
20              
21             # Process params.
22 8         44 set_params($self, @params);
23              
24             # Object.
25 6         277 return $self;
26             }
27              
28             # Parse configuration.
29             sub parse {
30 3     3 1 7302 my ($self, $data) = @_;
31 3         12 $self->_default;
32 3         6 my $stay = 0;
33 3         24 foreach my $line (split m/\n/ms, $data) {
34 47 100 100     242 if ($line =~ m/^\s*#/ms || $line =~ m/^\s*$/ms) {
35 10         20 next;
36             }
37 37 100 100     513 if (($stay == 0 || $stay == 1)
    100 100        
    100 100        
    100 100        
    100 100        
    100 66        
    100 100        
    100 100        
    100 66        
    100 66        
    50 66        
      33        
38             && $line =~ m/^\s*(\w+)\s*=\s*"?(\w+)"?\s*$/ms) {
39              
40 11         20 $stay = 1;
41 11         48 $self->{'global'}->{$1} = $2;
42              
43             # Begin of host table.
44             } elsif ($stay == 1
45             && $line =~ m/^\s*HostTable\s+BEGIN\s*$/ms) {
46              
47 3         7 $stay = 2;
48              
49             # End of host table.
50             } elsif ($stay == 2
51             && $line =~ m/^\s*HostTable\s+END\s*$/ms) {
52              
53 3         7 $stay = 1;
54              
55             # Host in host table.
56             } elsif ($stay == 2
57             && $line =~ m/^\s*(\w+)\s*=\s*\(([\d\.\w\s,]+)\)\s*$/ms) {
58              
59 6         63 $self->{'host_table'}->{$1} = [split m/\s*,\s*/ms, $2];
60              
61             # Symbolic names in host table.
62             } elsif ($stay == 2
63             && $line =~ m/^\s*(\w+)\s*=\s*([\w\s,]+)\s*$/ms) {
64              
65 1         11 $self->{'host_table_symb'}->{$1}
66             = [split m/\s*,\s*/ms, $2];
67              
68             # Begin of AE table.
69             } elsif ($stay == 1 && $line =~ m/^\s*AETable\s+BEGIN\s*$/ms) {
70 3         8 $stay = 3;
71              
72             # End of AE table
73             } elsif ($stay == 3 && $line =~ m/^\s*AETable\s+END\s*$/ms) {
74 3         8 $stay = 1;
75              
76             # AE item.
77             } elsif ($stay == 3
78             && $line =~ m/^\s*(\w+)\s+([\/\w]+)\s+(\w+)\s+\(([^)]+)\)\s+(.*)$/ms) {
79              
80 4         34 my ($maxStudies, $maxBytesPerStudy)
81             = split m/\s*,\s*/ms, $4;
82 4         45 $self->{'ae_table'}->{$1} = {
83             'StorageArea' => $2,
84             'Access' => $3,
85             'Quota' => {
86             'maxStudies' => $maxStudies,
87             'maxBytesPerStudy' => $maxBytesPerStudy,
88             },
89             'Peers' => $5,
90             };
91              
92             # Begin of vendor table
93             } elsif ($stay == 1
94             && $line =~ m/^\s*VendorTable\s+BEGIN\s*$/ms) {
95              
96 1         3 $stay = 4;
97              
98             # End of vendor table.
99             } elsif ($stay == 4
100             && $line =~ m/^\s*VendorTable\s+END\s*$/ms) {
101              
102 1         7 $stay = 1;
103              
104             # Item in vendor table.
105             } elsif ($stay == 4
106             && $line =~ m/^\s*"([^"]+)"\s*=\s*(\w+)\s*$/ms) {
107              
108 1         5 $self->{'vendor_table'}->{$2} = $1;
109             }
110             }
111 3         17 return;
112             }
113              
114             # Serialize to configuration.
115             sub serialize {
116 4     4 1 22 my $self = shift;
117 4         9 my @data;
118 4         17 $self->_serialize_global(\@data);
119 4         16 $self->_serialize_hosts(\@data);
120 4         18 $self->_serialize_vendors(\@data);
121 4         14 $self->_serialize_ae(\@data);
122 4         36 return join "\n", @data;
123             }
124              
125             # Set variables to defaults.
126             sub _default {
127 11     11   29 my $self = shift;
128            
129             # AE table.
130 11         51 $self->{'ae_table'} = {};
131              
132             # Global parameters.
133 11         67 $self->{'global'} = {
134             'NetworkTCPPort' => undef,
135             'MaxPDUSize' => undef,
136             'MaxAssociations' => undef,
137             'UserName' => undef,
138             'GroupName' => undef,
139             };
140              
141             # Host table.
142 11         32 $self->{'host_table'} = {};
143              
144             # Host table symbolic names.
145 11         30 $self->{'host_table_symb'} = {};
146              
147             # Vendor table.
148 11         27 $self->{'vendor_table'} = {};
149              
150 11         29 return;
151             }
152              
153             # Serialize AE titles.
154             sub _serialize_ae {
155 4     4   9 my ($self, $data_ar) = @_;
156 4 50       6 if (! keys %{$self->{'ae_table'}}) {
  4         14  
157 0         0 return;
158             }
159 4 100       13 if ($self->{'comment'}) {
160 3 50       4 if (@{$data_ar}) {
  3         10  
161 3         5 push @{$data_ar}, '';
  3         7  
162             }
163 3         6 push @{$data_ar}, '# AE Table.';
  3         7  
164             }
165 4         9 push @{$data_ar}, 'AETable BEGIN';
  4         20  
166 4         8 foreach my $key (sort keys %{$self->{'ae_table'}}) {
  4         14  
167 5         14 my $storage_area = $self->{'ae_table'}->{$key}->{'StorageArea'};
168 5         9 my $access = $self->{'ae_table'}->{$key}->{'Access'};
169 5         11 my $peers = $self->{'ae_table'}->{$key}->{'Peers'};
170             my $max_studies = $self->{'ae_table'}->{$key}->{'Quota'}
171 5         11 ->{'maxStudies'};
172             my $max_bytes_per_study = $self->{'ae_table'}->{$key}
173 5         11 ->{'Quota'}->{'maxBytesPerStudy'};
174 5         8 push @{$data_ar}, "$key $storage_area $access ".
  5         28  
175             "($max_studies, $max_bytes_per_study) $peers";
176             }
177 4         8 push @{$data_ar}, 'AETable END';
  4         9  
178 4         8 return;
179             }
180              
181             # Serialize global parameters.
182             sub _serialize_global {
183 4     4   11 my ($self, $data_ar) = @_;
184 4 100       8 if (! map { defined $self->{'global'}->{$_} ? $_ : () }
  20 50       66  
185 4         18 keys %{$self->{'global'}}) {
186              
187 0         0 return;
188             }
189 4 100       15 if ($self->{'comment'}) {
190 3 50       5 if (@{$data_ar}) {
  3         10  
191 0         0 push @{$data_ar}, '';
  0         0  
192             }
193 3         6 push @{$data_ar}, '# Global Configuration Parameters.';
  3         7  
194             }
195 4         8 foreach my $key (sort keys %{$self->{'global'}}) {
  4         26  
196 20 100       54 if (! defined $self->{'global'}->{$key}) {
197 6         15 next;
198             }
199 14         26 my $value = $self->{'global'}->{$key};
200 14 100       67 if ($value !~ m/^\d+$/ms) {
201 2         5 $value = '"'.$value.'"';
202             }
203 14         23 push @{$data_ar}, $key.' = '.$value;
  14         40  
204             }
205 4         12 return;
206             }
207              
208             # Serialize hosts table.
209             sub _serialize_hosts {
210 4     4   10 my ($self, $data_ar) = @_;
211 4 50       11 if (! keys %{$self->{'host_table'}}) {
  4         35  
212 0         0 return;
213             }
214 4 100       14 if ($self->{'comment'}) {
215 3 50       5 if (@{$data_ar}) {
  3         9  
216 3         5 push @{$data_ar}, '';
  3         7  
217             }
218 3         6 push @{$data_ar}, '# Host Table.';
  3         7  
219             }
220 4         8 push @{$data_ar}, 'HostTable BEGIN';
  4         9  
221 4         10 foreach my $key (sort keys %{$self->{'host_table'}}) {
  4         14  
222 7         26 my ($ae, $host, $port) = @{$self->{'host_table'}->{$key}};
  7         22  
223 7         12 push @{$data_ar}, "$key = ($ae, $host, $port)";
  7         23  
224             }
225 4         8 foreach my $key (sort keys %{$self->{'host_table_symb'}}) {
  4         13  
226 1         4 push @{$data_ar}, "$key = ".
227 1         3 (join ", ", @{$self->{'host_table_symb'}->{$key}});
  1         7  
228             }
229 4         7 push @{$data_ar}, 'HostTable END';
  4         26  
230 4         36 return;
231             }
232              
233             # Serialize vendors table.
234             sub _serialize_vendors {
235 4     4   9 my ($self, $data_ar) = @_;
236 4 100       9 if (! keys %{$self->{'vendor_table'}}) {
  4         13  
237 3         7 return;
238             }
239 1 50       5 if ($self->{'comment'}) {
240 1 50       2 if (@{$data_ar}) {
  1         4  
241 1         2 push @{$data_ar}, '';
  1         3  
242             }
243 1         3 push @{$data_ar}, '# Vendor Table.';
  1         3  
244             }
245 1         2 push @{$data_ar}, 'VendorTable BEGIN';
  1         4  
246 1         3 foreach my $key (sort keys %{$self->{'vendor_table'}}) {
  1         4  
247 1         3 my $desc = '"'.$self->{'vendor_table'}->{$key}.'"';
248 1         2 push @{$data_ar}, "$desc = $key";
  1         4  
249             }
250 1         3 push @{$data_ar}, 'VendorTable END';
  1         4  
251 1         3 return;
252             }
253              
254             1;
255              
256             __END__