Thread Perl in C umsetzen: Umsetzen von Perl in C-Code (17 answers)
Opened by student at 2004-06-10 11:58

student
 2004-06-10 11:58
#29262 #29262
User since
2004-06-10
1 Artikel
BenutzerIn
[default_avatar]
Hallo,
Is jemand in der Lage den folgenden Perl-Code in C-Code umzusetzen?
Wenn es sein muß, dann gebe ich auch etwas Trinkgeld!

Code: (dl )
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#use strict;

#require "ipc.ph";
#require "sem.ph";

use IO::Socket;
use IO::File;
use POSIX ":sys_wait_h";

sub start_log #(Dateiname)
 {
 my $filename = shift;
 open(FH,">>".$filename) || return 0;
 chmod($filename,644);
 # Autoflush setzen fuer FH
 my $oldfh = select(FH); $| = 1; select($oldfh);
 # Lock freigeben
 flock(FH,8);
 # warn und die umleiten
 $SIG{} = \&log_warn;
 $SIG{} = \&log_die;
 return 1;
 }

sub end_log
 {
 close(FH);
 }

sub log_info #(Message)
 {
 my $time = localtime;
 my $mesg = join(' ',@_) || "Oops!";
 $mesg = $time . " [INFO] " . $mesg . "\n";
 flock(FH,2);
 print FH $mesg;
 flock(FH,8);
 }

sub log_warn #(Message)
 {
 my $time = localtime;
 my $mesg = join(' ',@_) || "Oops!";
 $mesg = $time . " [ERROR] " . $mesg . "\n";
 flock(FH,2);
 print FH $mesg;
 flock(FH,8);
 }

sub log_die #(Message)
 {
 my $time = localtime;
 my $mesg = join(' ',@_) || "Oops!";
 $mesg = $time . " [FATAL] " . $mesg . "\n";
 flock(FH,2);
 print FH $mesg;
 flock(FH,8);
 close(FH);
 die @_;
 }


# Port Nr 7777 wg. Firewall Konfiguration im PDV Labor
use constant MYPORT => 7777;
#
my $sock = '';
my $client = '';
my $pid;
my %child_pids = (); # Hash fuer Prozessnummern
my %key_ids = (); # Hash fuer shared mem keys
my %typedef = (); # Hash fuer typedefs
my %sizeof = (); # Hash fuer sizeofs

# signal handler
sub catcher
 {
 my $pid;
 $SIG{CHLD} = \&catcher;
 foreach $pid (keys(%child_pids))
   {
   if(waitpid($pid, WNOHANG)) # Zombies verhindern
     {
     print "Terminated: $pid\n";
     delete $child_pids{$pid};
     }
   }
 }

start_log ("pdvServer.log");
log_info("Server Start");

$SIG{CHLD} = \&catcher;

# mit pack/unpack werden die Daten binaer gespeichert/entpackt
# typedef und sizeof 'Arbeiten wie in C', wird von perl-Programmierern gehasst!
$typedef{SHORT} = 'S';
$typedef{LONG} = 'L';
$typedef{DOUBLE} = 'd';
$sizeof{SHORT} = length(pack($typedef{SHORT},()));
$sizeof{LONG} = length(pack($typedef{LONG},()));
$sizeof{DOUBLE} = length(pack($typedef{DOUBLE},()));

# #####
# Hier geht das Haupt-Programm los
#

# socket erstellen
$sock = new IO::Socket::INET(LocalPort => MYPORT,
                            Reuse     => 1,
                            Listen    => 20)
   or die "can't create local socket: $@\n";

print STDERR "Accepting connections on Port ", MYPORT, "...\n";

# server laeuft in Endlos-Schleife
while (1)
 {
 # wartet auf neue Verbindungen von Clients
 $client = $sock->accept();
 # accept wird auch von einem signal 'unterbrochen', darum hier noch ne Abfrage
 if ($client)
   {
   # Verbindung ist aufgebaut
   print STDERR "Accepted connection from ",
       $client->peerhost(), ":", $client->peerport(), "\n";

   # Erzeugen eines Kindprozesses.
   $pid = fork();
   if ($pid == 0) # Kindprozess
     {
     &serverChild;
     exit(0);
     }
     else # else Eltern-Prozess
       {
       print STDERR "Prozess $pid started\n ";
       $child_pids{$pid} = 1;
       $client->close; # not needed in parent
     }
   }
 }

# #####
# Kind-Prozess fuer jeden verbundenen Client
#

sub serverChild
{
my $id;

$sock->close; # not needed in child
$ipaddr = inet_aton($client->peerhost());
print $client "Hi ".gethostbyaddr($ipaddr, AF_INET). " nice to meet you ...\n";
while (<$client>)
 {
 chomp;

 log_info($client->peerhost()." sent: $_");
 
 SWITCH:
   {
   @mycmd=split(' ',$_);
   # START Abtastrate Punkte -> UniqueID zurueck;
   if ($mycmd[0] =~ /START/i)
     {
     if ($#mycmd < 2)
       {
       print $client "NEE usage: START Abtastrate Punkte\n";
       }
     else
       {
       $id = time()/$$;

       $IPC_PRIVATE = 0;
       $IPC_RMID = 0;
       
       # Speicherbedarf
       # Punkteanzahl (Long), id (Double), status (Long)
       # und die Daten (Short)
       #
       $size = $mycmd[2] * $sizeof{SHORT} + $sizeof{LONG} + $sizeof{DOUBLE} + $sizeof{LONG};
       
       $key = shmget($IPC_PRIVATE, $size, 0777);
       unless ($key)
         {
         log_info("Could not get shared memory");
         print $client "NEE Could not get shared memory\n";
         last SWITCH;
         }
       else
         {
         log_info("shared mem key START : $key");

         # hier 'eigentlich'
         # process fork mit vererbung von $key, $id, status 0, Punkteanzahl und Abtastrate
         # wenn fertig status auf 1 setzen, das wars ...
         #
         # hier kann ich nun "einfach" das memory beschreiben .... und beim DATA zurueckgeben
         
         # 'Header' schreiben
         $offset = 0;
         shmwrite $key, pack($typedef{LONG},$mycmd[2]),$offset,$sizeof{LONG};
         $offset += $sizeof{LONG};
         shmwrite $key, pack($typedef{DOUBLE},$id),$offset,$sizeof{DOUBLE};
         $offset += $sizeof{DOUBLE};
         $status = 0;
         $statusOffset = $offset;
         shmwrite $key, pack($typedef{LONG},$status),$offset,$sizeof{LONG};
         $offset += $sizeof{LONG};
         
         # dummy Daten sin(x)/x
         for($i=0; $i <$mycmd[2]; $i++)
           {
$test =  sin($i+1)/($i+1) * 2047 + 2048;
print "$test\n";

           shmwrite $key, pack($typedef{SHORT},sin($i+1)/($i+1) * 2047 + 2048),$offset,$sizeof{SHORT};
           $offset += $sizeof{SHORT};
           }
         $status = 1;
         shmwrite $key, pack($typedef{LONG},$status),$statusOffset,$sizeof{LONG};
         
         $key_ids{$id} = $key;
         print $client "OK $id\n"
         }
       }
     last SWITCH;
     }
   if ($mycmd[0] =~ /DATA/i)
     {
     if ($#mycmd < 1)
       {
       print $client "NEE id needed\n";
       }
     else
       {
       #wenn in key_ids von id ein key steht mit diesem testen ob status ok und dann mit diesem
       #key die daten auslesen und schicken. dann memory loeschen.
       $id = $mycmd[1];
       $key = $key_ids{$id};
       if ($key)
         {
         log_info("shared mem key DATA : $key");
         #
         $offset = 0;
         shmread $key, $points,$offset,$sizeof{LONG};
         $offset += $sizeof{LONG};
         $points = unpack($typedef{LONG},$points);
         shmread $key, $id,$offset,$sizeof{DOUBLE};
         $offset += $sizeof{DOUBLE};
         $id = unpack ($typedef{DOUBLE}, $id);
         shmread $key, $status,$offset,$sizeof{LONG};
         $offset += $sizeof{LONG};
         $status = unpack ($typedef{LONG}, $status);
 
         if ($status == 1)
           {
           print $client "OK $points daten werden gesendet\n";

           for($i=0; $i <$points; $i++)
             {
             shmread $key,$data,$offset,$sizeof{SHORT};
             #print $client "offset: $offset;data[$i] = ". unpack ($typedef{SHORT},$data). "\n";
             #print $client "offset: $offset;data[$i] = $data \n";
# Auf vielfachen Wunsch des(r) Studenten ...
             print $client "$i:$data\n";
             $offset += $sizeof{SHORT};
             }

           print $client "\nOK Done\n";
           shmctl($key, $IPC_RMID,0); # loescht shared memory
           delete $key_ids{$id};
           }
         else
           {
           print $client "NEE buffer not ready\n";
           }
         }
       else
         {
         print $client "NEE kein key fuer die id gefunden\n";
         }
       }
     last SWITCH;
     }
   if ($mycmd[0] =~ /STOP/i)
     {
     print $client "OK not implemented id would be needed\n";
     last SWITCH;
     }
   if ($mycmd[0] =~ /ENDE/i)
     {
     print $client "OK will end task and close connection\n";
     # hier alle shared memory Bereiche zu dem Prozess loeschen.
     foreach $key (values(%key_ids))
       {
       print "Remove: $key\n";
       shmctl($key, $IPC_RMID,0);
       }
     $client->close;
     exit(0);
     last SWITCH;
     }
   print $client "NEE unknown command\n";
   }
 }
}


Gruß Student

View full thread Perl in C umsetzen: Umsetzen von Perl in C-Code