-
Notifications
You must be signed in to change notification settings - Fork 3
/
Generic.pm6
143 lines (92 loc) · 3.25 KB
/
Generic.pm6
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
use v6;
#
# Copyright © 2018-2019 Joelle Maslak
# All Rights Reserved - See License
#
use Net::BGP::Conversions;
use Net::BGP::Message::Notify::Open;
use StrictClass;
unit class Net::BGP::Message::Notify::Open::Generic:ver<0.1.1>:auth<cpan:JMASLAK>
is Net::BGP::Message::Notify::Open
does StrictClass;
method new() {
die("Must use from-raw or from-hash to construct a new object");
}
# Generic Types
method implemented-error-subcode(-->Int) { Int }
method implemented-error-subname(-->Str) { Str }
method error-subname(-->Str) { Str } # Undefined
method from-raw(buf8:D $raw where $raw.bytes ≥ 3) {
my $obj = self.bless(:data( buf8.new($raw) ));
if $raw[0] ≠ 3 { # Not a notify
die("Can only build a notification message");
}
if $raw[1] ≠ 2 { # Not an Open error
die("Can only build an Open error notification message");
}
# Validate the parameters parse.
# We could probably defer this - the controller will get to it,
# but this is safer.
# $obj.parameters;
return $obj;
};
method from-hash(%params is copy) {
# Delete unnecessary options
if %params<message-code>:exists {
if (%params<message-code> ≠ 3) { die("Invalid message type for NOTIFY"); }
%params<message-code>:delete
}
if %params<error-code>:exists {
if (%params<error-code> ≠ 2) { die("Invalid error type for Open"); }
%params<error-code>:delete
}
my @REQUIRED = «error-subcode raw-data»;
# Optional parameters
%params<raw-data> //= buf8.new;
if @REQUIRED.sort.list !~~ %params.keys.sort.list {
die("Did not provide proper options");
}
# Now we need to build the raw data.
my $data = buf8.new();
$data.append( 3 ); # Message type (NOTIFY)
$data.append( 2 ); # Error code (Open)
$data.append( %params<error-subcode> );
$data.append( %params<raw-data> );
return self.bless(:data( buf8.new($data) ));
};
method raw() { return $.data; }
# Register handler
INIT { Net::BGP::Message::Notify::Open.register(Net::BGP::Message::Notify::Open::Generic) }
=begin pod
=head1 NAME
Net::BGP::Message::Notify::Open::Generic - Generic Open Error BGP Notify Message
=head1 SYNOPSIS
# We create generic messages using the parent class.
use Net::BGP::Message;
my $msg = Net::BGP::Message.from-raw( $raw ); # Might return a child crash
=head1 DESCRIPTION
Generic Open error BGP Notify message type
=head1 Constructors
=head2 from-raw
Constructs a new object for a given raw binary buffer.
=head2 from-hash
This simply throws an exception, since the hash format of a generic message
is not designed.
=head1 Methods
=head2 message-code
Returns a string that describes what message type the command represents.
Currently understood types include C<Open>.
=head2 message-code
Contains an integer that corresponds to the message-code.
=head2 error-type
Error code of the notification.
=head2 error-subcode
Error subtype of the notification.
=head2 raw
Returns the raw (wire format) data for this message.
=head1 AUTHOR
Joelle Maslak <jmaslak@antelope.net>
=head1 COPYRIGHT AND LICENSE
Copyright © 2018-2019 Joelle Maslak
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod