3 Einträge, 1 Seite |
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
use threads;
use threads::shared;
use Thread::Queue;
my $toBeSentMessages = Thread::Queue->new;#the queue with the messages which are supposed to be sent
my @receivedMessages : shared;##the queue with the messages which are received
my $result : shared;
sub new{
my ($proto, $userSuffix, $noOfThreads, $host, $port ) = @_;
my $class = ref($proto) || $proto;
for my $i(1..$noOfThreads) {
threads->create("opThread", "$userSuffix$i", $host, $port);
}
my $self = {};
bless ($self, $class);
return $self;
}
sub send{
my ($self, $message)=@_;
$ToSentMessages->enqueue($message);
}
sub receive{
lock (@receivedMessages);
cond_wait(@receivedMessages);
return (pop(@receivedMessages));
}
sub opThread {
my ($user, $host, $port)=@_;
my $socket=deapi->new($user, $user, $host, $port);
print "Start of Child\n";
while () {
print "Inside While\n";
sleep(1);
my $message=$ToSentMessages->dequeue_nb();
if (defined($message)) {
print "Sending Message $message\n";
$api->send($message);
}
if ($api->can_read()) {
print "Receiving OP Message\n";
$result =(deapi::parse($api->receive()));
lock (@ReceivedMessages);
push(@ReceivedMessages, $result);
cond_signal(@ReceivedMessages);
}
}
}
3 Einträge, 1 Seite |