-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorrespondent.pm
208 lines (175 loc) · 5.56 KB
/
Correspondent.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
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
package Correspondent;
# This package implements the protocols for communicating between the minder components.;
use strict;
use warnings;
#use Sys::Hostname;
use IO::Socket::INET;
use parent 'Wakeup';
use Class::Tiny qw(
address
port
peer_type
peer_id
RecentStamp
wakeup_time
),
{
}
;
# A hash of all the correspondents, keyed by Type:IPaddress
my %correspondents;
my $socket;
my $my_type;
my $my_id;
sub initialise {
my $self = shift;
$socket = shift;
$my_type = ClusterConfig::node()->{type};
$my_id =ClusterConfig::node()->{id};
}
sub create {
my $peer = shift;
my $correspondent;
my $address = $peer->{minder_ip};
my $port = $peer->{minder_port};
my $peer_type = $peer->{type};
my $peer_id = $peer->{id};
my $key = "|$peer_type|$peer_id|";
if ( exists $correspondents{$key} ) {
$correspondent = $correspondents{$key};
print "Found the correspondent for $key \n";
} else {
print "New correspondent with key $key\n";
my $Klass= "$my_type$peer_type";
$correspondent = $Klass->new({ peer_id => $peer_id});
$correspondent->{peer} = $peer;
$correspondent->{type} = $peer_type;
$correspondents{$key} = $correspondent;
}
$correspondent->{RecentStamp} = time();
return $correspondent;
}
sub find_correspondent {
my $correspondent;
my $address = shift;
my $port = shift;
my $peer_type = shift;
my $peer_id = shift;
my $key = "|$peer_type|$peer_id|";
if ( exists $correspondents{$key} ) {
$correspondent = $correspondents{$key};
print "Found the correspondent for $key \n";
} else {
print "New correspondent with key $key\n";
my $Klass= "$my_type$peer_type";
$correspondent = $Klass->new({ peer_id => $peer_id});
$correspondent->{peer} = ClusterConfig::nodeByTypeId($peer_type, $peer_id, $address, $port);
if ( $correspondent->{peer}->{minder_ip} ne $address ) {
print "Wrong ip address for correspondent. Config=$correspondent->{peer}->{minder_ip}, socket=$address.\n";
}
if ($correspondent->{peer}->{minder_port} ne $port) {
print "Wrong port for correspondent. Config=$correspondent->{peer}->{minder_ip}, socket=$address.\n";
}
$correspondent->{type} = $peer_type;
$correspondents{$key} = $correspondent;
}
$correspondent->{RecentStamp} = time();
return $correspondent;
}
sub register {
my $self = shift;
# print "Building with peer_type = $self->{peer_type}\n";
# print "Building with peer_id = $self->{peer_id}\n";
my $key = "|$self->{peer}->{type}|$self->{peer}->{id}|";
$correspondents{$key} = $self;
}
sub send {
my $self = shift;
my $message = shift;
my $ack_required = shift;
$self->{protocol} = "$my_type $my_id $self->{peer}->{type} $self->{peer}->{id} $message";
print "Sending $self->{protocol}\n";
my $socket_address = sockaddr_in($self->{peer}->{minder_port}, inet_aton($self->{peer}->{minder_ip}));
$socket->send($self->{protocol}, 0, $socket_address );
if (defined $ack_required) {
$self->set_wakeup('resend',3);
$self->{resend_count} = 0;
} else {
$self->set_wakeup('resend',-1);
}
}
sub send_all {
my $type = shift;
my $message = shift;
my $ack_required = shift;
print "Send all, type=$type, message= $message \n";
while ( (my $key, my $node) = each %correspondents )
{
print "Node = $key \n";
if ($node->{type} eq $type) {
$node->send($message,$ack_required);
}
}
}
sub send_one {
my $peer_type = shift;
my $peer_id = shift;
my $message = shift;
my $ack_required = shift;
my $key = "|$peer_type|$peer_id|";
print "Send one, type=$peer_type, message= $message \n";
my $node = $correspondents{$key} ;
print "Node = $key \n";
if ($node->{type} eq $peer_type) {
$node->send($message,$ack_required);
}
}
sub byTypeId {
my $peer_type = shift;
my $peer_id = shift;
my $key = "|$peer_type|$peer_id|";
my $correspondent = $correspondents{$key} ;
return $correspondent;
}
sub resend {
my $self= shift;
$self->{resend_count} ++;
$self->set_wakeup('resend',3);
my $socket_address = sockaddr_in($self->{peer}->{minder_port}, inet_aton($self->{peer}->{minder_ip}));
$socket->send($self->{protocol}, 0, $socket_address );
if ($self->{resend_count} % 200 == 1) {
print "Re-Sending $self->{resend_count} $self->{protocol}\n";
}
}
sub ack {
my $self = shift;
$self->set_wakeup('resend',-1);
}
sub process_udp_message {
my $self = shift;
my ($address, $port, $data) = @_;
my @parms = split ' ', $data;
# print "\n($address , $port) said: $data \n";
my $peer_type = shift @parms;
my $peer_id = shift @parms;
my $receiver_type = shift @parms;
my $receiver_id = shift @parms;
my $correspondent = find_correspondent($address, $port, $peer_type, $peer_id);
#print ref($correspondent), "\n";
if ( $parms[0] eq 'ack' ) {
$correspondent->ack();
} else {
$correspondent->receive(\@parms);
}
}
1;
# Here is how resend works:
#
# 1. Some messages do not need resend. In fact, this is the default.
# 2. If a message needs resend, it will be flagged as such
# 3. The send method keeps a copy of the message, and sets an alarm to send it later.
# 4. If an ack message is recevied, then we cancel the alarm.
# 5. If certain superceding messages are sent, then we cancel the alarm.
# If the alarm fires, we enter the resend method
# 6. The messages is resent, and a counter incremented, and the alarm set again.
# 7. If the message has been sent a few times without ack, then we log it.