anthill_ruby 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Artem Rufanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,22 @@
1
+ Introduction:
2
+ The AntHill is a powerful networking library which implements network fail-over and scaling functionality for any user applications.
3
+
4
+ Report a bug:
5
+ Try to search for similar issues in our bug database (http:://www.majoron.com/support/) before report a bug.
6
+ If you did not find anything similar then please submit the new bug. Please use following tips to report a bug:
7
+ The basics: what you did, what you wanted to happen, and what actually happened.
8
+ If you don't understand the error message - please ask for help in forum.
9
+ Be brief, but don't leave any important details out.
10
+ Use plain English describing the problem.
11
+ Please describe one problem in each separate bug report.
12
+ Please do not report bugs for old versions.
13
+
14
+ Release Notes:
15
+
16
+ Version 0.5
17
+ -----------
18
+ -Implemented binary encoder/decoder. Support primitive datatypes
19
+ -Implemented a lot of unit tests to verify coding and decoding
20
+
21
+ Copyright (c) 2011 arufanov, released under the MIT license.
22
+
@@ -0,0 +1,17 @@
1
+ require 'date'
2
+ Gem::Specification.new do |s|
3
+ s.name = %q{anthill_ruby}
4
+ s.version = "0.5.2"
5
+ s.date = Date.today.to_s
6
+ s.summary = %q{The AntHill is a powerful networking library which implements network fail-over and scaling functionality for any user applications.}
7
+ s.description = %q{The AntHill is a powerful networking library which implements network fail-over and scaling functionality for any user applications.}
8
+ s.author = %q{Artem Rufanov}
9
+ s.email = %q{developers@majoron.com}
10
+ s.homepage = %q{http://www.majoron.com/project/anthill/ruby}
11
+ s.files = Dir.glob('**/*') - Dir.glob('distrib/**/*') - Dir.glob('lib/api/**/*') - Dir.glob('doc/*.xpr')
12
+ s.bindir = 'bin'
13
+ s.executables = Dir.glob('bin/*').collect {|f| File.basename(f)}
14
+ s.require_paths << 'doc' << 'examples' << 'lib' << 'test'
15
+ s.has_rdoc = true
16
+ s.required_ruby_version = '>= 1.8.6'
17
+ end
@@ -0,0 +1,21 @@
1
+ Introduction:
2
+ To see the latest list of the change log please visit the Change Log page at www.majoron.com.
3
+
4
+ Legend:
5
+ Follow notation is used at change log, roadmap and known bugs. Each bug begins with a version,
6
+ then follow category of the bug inside {}. It can be bug report, feature request and etc.
7
+ Then follow component inside []. After follow bug number at bug tracking system between // signs.
8
+ And then follow a short description of the bug.
9
+
10
+ Example:
11
+ For example bug: "1.0 { Feature Request } [ AntHill ] / 380 / STLport support required" means
12
+ that bug was created for 1.0 version of the AntHill component, bug is feature request with
13
+ 380 number at bug tracking system. And bug requires STLPort support implementation.
14
+
15
+
16
+ Version 0.5
17
+ -----------
18
+ 0.5 { Feature Request } [ AntHill Core ] / X / Support two types: 0B 05 04 and 0B0504
19
+ 0.5 { Feature Request } [ AntHill Core ] / X / Make single namespace AntHill
20
+ 0.5 { Feature Request } [ AntHill Core ] / X / Ruby 1.9.x support has been added
21
+ 0.5 { Feature Request } [ AntHill Core ] / 774 / Write to readme.txt files how to report a bug.
@@ -0,0 +1 @@
1
+ #1
@@ -0,0 +1,19 @@
1
+ Introduction:
2
+ To see the latest list of the known bugs please visit the Known bugs page at www.majoron.com.
3
+
4
+ Legend:
5
+ Follow notation is used at change log, roadmap and known bugs. Each bug begins with a version,
6
+ then follow category of the bug inside {}. It can be bug report, feature request and etc.
7
+ Then follow component inside []. After follow bug number at bug tracking system between // signs.
8
+ And then follow a short description of the bug.
9
+
10
+ Example:
11
+ For example bug: "1.0 { Feature Request } [ AntHill ] / 380 / STLport support required" means
12
+ that bug was created for 1.0 version of the AntHill component, bug is feature request with
13
+ 380 number at bug tracking system. And bug requires STLPort support implementation.
14
+
15
+
16
+ Version 0.5
17
+ -----------
18
+ 0.5 { --- } [ AntHill Core ] / 782 / Codec tests, puts test from AntHill SMPP Cpp
19
+ 0.5 { Bug Report } [ AntHill Core ] / 771 / Good message for unparsable dump
@@ -0,0 +1,44 @@
1
+ #
2
+ # Copyright 2007 Majoron.com (developers@majoron.com)
3
+ # Original sources are available at www.majoron.com
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ module Majoron
20
+ module AntHill
21
+ # Module contains encoder and decoder errors
22
+ module CodecErrors
23
+ DECODER_INV_SKIPPING = "Unable to padding. Offset is: %d."
24
+ DECODER_INV_PADDING = "Unable to padding. Offset is: %d."
25
+ DECODER_INV_CHAR = "Unable to decode char. Offset is: %d."
26
+ DECODER_INV_UNSIGNED_CHAR = "Unable to decode unsigned char. Offset is: %d."
27
+
28
+ DECODER_INV_INTEGER32 = "Unable to decode integer32. Offset is: %d."
29
+ DECODER_INV_INTEGER24 = "Unable to decode integer24. Offset is: %d."
30
+ DECODER_INV_INTEGER16 = "Unable to decode integer16. Offset is: %d."
31
+
32
+ DECODER_INV_UNSIGNED32 = "Unable to decode integer32. Offset is: %d."
33
+ DECODER_INV_UNSIGNED24 = "Unable to decode integer24. Offset is: %d."
34
+ DECODER_INV_UNSIGNED16 = "Unable to decode integer16. Offset is: %d."
35
+
36
+ DECODER_INV_CSTRING = "Unable to decode cstring. Offset is: %d."
37
+ DECODER_OVERFLOW_CSTRING = "Unable to decode cstring due to overflow. Offset is: %d."
38
+ DECODER_INV_BINARY = "Unable to decode cstring. Offset is: %d."
39
+
40
+ DECODER_INV_OFFSET = "Unable to decode package. Offset after decoding is not equal to length. Offset is: %d. Lenght is: %d"
41
+ DECODER_DUE_TO_LIMIT = "Unable to decode package. Offset after decoding is greater than length. Offset is: %d. Lenght is: %d"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ #
2
+ # Copyright 2007 Majoron.com (developers@majoron.com)
3
+ # Original sources are available at www.majoron.com
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ $:.unshift(File.dirname(__FILE__))
19
+
20
+ # Include all files from anthill library
21
+ require "anthill_constants"
22
+ require "byte_buffer"
23
+ require "encoder_base"
24
+ require "decoder_base"
25
+ require "validator_base"
26
+ require "binary_encoder"
27
+ require "binary_decoder"
28
+ require "outputter_base"
29
+ require "txt_outputter"
30
+ require "xml_outputter"
31
+
32
+ module Majoron
33
+ module AntHill
34
+ end
35
+ end
36
+
@@ -0,0 +1,227 @@
1
+ #
2
+ # Copyright 2007 Majoron.com (developers@majoron.com)
3
+ # Original sources are available at www.majoron.com
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ module Majoron
20
+ module AntHill
21
+ class BinaryDecoder < DecoderBase
22
+ # Constructor.
23
+ def initialize()
24
+ super()
25
+ @offset = 0
26
+ @buffer = ""
27
+
28
+ end
29
+
30
+ # Method to skip n bytes
31
+ def skip_n_bytes(bytes, limit = nil)
32
+ return if bytes == 0
33
+ if @offset <= @buffer.size() - bytes
34
+ @offset += bytes
35
+
36
+ # Check overflow
37
+ overflow = !limit.nil? && @offset > limit
38
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
39
+ else
40
+ raise sprintf(CodecErrors::DECODER_INV_CHAR, @offset)
41
+ end
42
+ end
43
+
44
+
45
+ # Methods to align on n bytes
46
+ def padding_n_bytes(bytes, limit = nil)
47
+ raise "not implemented"
48
+ end
49
+
50
+ # Decode methods
51
+ def decode_char(limit = nil)
52
+ sizeof = 1
53
+ if @offset <= @buffer.size() - sizeof
54
+ if RUBY_VERSION >= "1.9.0"
55
+ value = @buffer[@offset].ord
56
+ else
57
+ value = @buffer[@offset]
58
+ end
59
+ # value = @buffer[@offset]
60
+ @offset += sizeof
61
+
62
+ # Check overflow
63
+ overflow = !limit.nil? && @offset > limit
64
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
65
+ value
66
+ else
67
+ raise sprintf(CodecErrors::DECODER_INV_CHAR, @offset)
68
+ end
69
+ end
70
+
71
+ # Decode methods
72
+ def decode_uchar(limit = nil)
73
+ sizeof = 1
74
+ if @offset <= @buffer.size() - sizeof
75
+ if RUBY_VERSION >= "1.9.0"
76
+ value = @buffer[@offset].ord
77
+ else
78
+ value = @buffer[@offset]
79
+ end
80
+ # value = @buffer[@offset]
81
+ @offset += sizeof
82
+
83
+ # Check overflow
84
+ overflow = !limit.nil? && @offset > limit
85
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
86
+ value
87
+ else
88
+ raise sprintf(CodecErrors::DECODER_INV_CHAR, @offset)
89
+ end
90
+ end
91
+
92
+ # Decode methods
93
+ def decode_integer32(value, limit = nil)
94
+ raise "not implemented"
95
+ end
96
+
97
+ # Decode methods
98
+ def decode_integer24(value, limit = nil)
99
+ raise "not implemented"
100
+ end
101
+
102
+ # Decode methods
103
+ def decode_integer16(value, limit = nil)
104
+ raise "not implemented"
105
+ end
106
+
107
+ # Decode methods
108
+ def decode_unsigned32(limit = nil)
109
+ sizeof = 4
110
+ if @offset <= @buffer.size() - sizeof
111
+ value = @buffer[@offset..@offset+(sizeof-1)].unpack('N').first
112
+ @offset += sizeof
113
+
114
+ # Check overflow
115
+ overflow = !limit.nil? && @offset > limit
116
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
117
+ value
118
+ else
119
+ raise sprintf(CodecErrors::DECODER_INV_CHAR, @offset)
120
+ end
121
+ end
122
+
123
+ # Decode methods
124
+ def decode_unsigned16(limit = nil)
125
+ sizeof = 2
126
+ if @offset <= @buffer.size() - sizeof
127
+ value = @buffer[@offset..@offset+(sizeof-1)].unpack('n').first
128
+ @offset += sizeof
129
+
130
+ # Check overflow
131
+ overflow = !limit.nil? && @offset > limit
132
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
133
+ value
134
+ else
135
+ raise sprintf(CodecErrors::DECODER_INV_CHAR, @offset)
136
+ end
137
+ end
138
+
139
+ # Decode methods
140
+ def decode_cstring(value, limit = nil)
141
+ raise "not implemented"
142
+ end
143
+
144
+ # Decode methods
145
+ def decode_cstring_max(maxlen, limit = nil)
146
+ if @offset <= @buffer.size
147
+ value = ""
148
+ if RUBY_VERSION >= "1.9.0"
149
+ while @offset < @buffer.size() && @buffer[@offset].ord != 0
150
+ value << @buffer [ @offset ]
151
+ @offset += 1
152
+
153
+ # Check max limitation
154
+ overmax = value.size() + 1 > maxlen
155
+ raise sprintf(CodecErrors::DECODER_OVERFLOW_CSTRING, @offset) if overmax
156
+ end
157
+ else
158
+ while @offset < @buffer.size() && @buffer[@offset] != 0
159
+ value << @buffer [ @offset ]
160
+ @offset += 1
161
+
162
+ # Check max limitation
163
+ overmax = value.size() + 1 > maxlen
164
+ raise sprintf(CodecErrors::DECODER_OVERFLOW_CSTRING, @offset) if overmax
165
+ end
166
+ end
167
+ @offset += 1
168
+
169
+ # Check overflow
170
+ overflow = !limit.nil? && @offset > limit
171
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
172
+ value
173
+ else
174
+ raise sprintf( CodecErrors::DECODER_INV_CSTRING, @offset)
175
+ end
176
+ end
177
+
178
+ # Decode methods
179
+ def decode_binary(size, limit = nil)
180
+ if @offset <= @buffer.size
181
+ pos = 0
182
+ value = ""
183
+ while @offset < @buffer.size() && pos < size
184
+ value << @buffer [ @offset ]
185
+ @offset += 1
186
+ pos += 1
187
+ end
188
+
189
+ # Check overflow
190
+ overflow = !limit.nil? && @offset > limit
191
+ raise sprintf(CodecErrors::DECODER_DUE_TO_LIMIT, @offset, limit) if overflow
192
+ value
193
+ else
194
+ raise sprintf( CodecErrors::DECODER_INV_BINARY, @offset)
195
+ end
196
+ end
197
+
198
+
199
+ #
200
+ def reset_buffer()
201
+ @offset = 0
202
+ @buffer = ""
203
+ end
204
+
205
+ #
206
+ def get_buffer()
207
+ @buffer
208
+ end
209
+
210
+ #
211
+ def append_buffer(buf)
212
+ @buffer << buf
213
+ end
214
+
215
+ #
216
+ def validate_offset(len)
217
+ return if @offset == len
218
+ raise sprintf(CodecErrors::DECODER_INV_OFFSET, @offset, len)
219
+ end
220
+
221
+ private
222
+ attr_accessor :buffer
223
+ attr_accessor :offset
224
+
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,131 @@
1
+ #
2
+ # Copyright 2007 Majoron.com (developers@majoron.com)
3
+ # Original sources are available at www.majoron.com
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ module Majoron
20
+ module AntHill
21
+ class BinaryEncoder < EncoderBase
22
+ # Constructor.
23
+ def initialize()
24
+ super()
25
+ @offset = 0
26
+ @buffer = ""
27
+
28
+ end
29
+
30
+ # Method to skip n bytes
31
+ def skip_n_bytes(bytes)
32
+ # TODO: implement
33
+ raise "skip_n_bytes is not implemented"
34
+ end
35
+
36
+
37
+ # Padding function to align on n bytes
38
+ def padding_n_bytes(bytes)
39
+ # TODO: implement
40
+ raise "padding_n_bytes is not implemented"
41
+ end
42
+
43
+ # Encode functions
44
+ def encode_char(value)
45
+ sizeof = 1
46
+ @buffer << value
47
+ @offset += sizeof
48
+ end
49
+
50
+ def encode_uchar(value)
51
+ sizeof = 1
52
+ @buffer << value
53
+ @offset += sizeof
54
+ end
55
+
56
+ def encode_integer32(value)
57
+ # TODO: implement
58
+ raise "encode_integer32 is not implemented"
59
+ end
60
+
61
+ def encode_integer24(value)
62
+ # TODO: implement
63
+ raise "encode_integer24 is not implemented"
64
+ end
65
+
66
+ def encode_integer16(value)
67
+ # TODO: implement
68
+ raise "encode_integer16 is not implemented"
69
+ end
70
+
71
+ def encode_unsigned32(value)
72
+ sizeof = 4
73
+ # @buffer << sprintf("%c%c%c%c", value >> 24, value >> 16, value >> 8, value & 0xff)
74
+ @buffer << sprintf("%c%c%c%c", value >> 24, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff)
75
+ @offset += sizeof
76
+ end
77
+
78
+ def encode_unsigned16(value)
79
+ sizeof = 2
80
+ @buffer << sprintf("%c%c", value >> 8, value & 0xff)
81
+ @offset += sizeof
82
+ end
83
+
84
+ def encode_cstring(value, limit = nil)
85
+ # Encoding::CompatibilityError: incompatible character encodings: US-ASCII and ASCII-8BIT
86
+ # ASCII-8BIT is a binary data, I am not able to find how to set encoding in constructor
87
+ if RUBY_VERSION >= "1.9.0"
88
+ @buffer.force_encoding("ASCII-8BIT")
89
+ end
90
+
91
+ sizeof = value.size() + 1 # 1 = NULL terminator
92
+ @buffer << value << 0 # 0 = NULL terminator
93
+ @offset += sizeof
94
+ end
95
+
96
+ def encode_binary(value)
97
+ # Encoding::CompatibilityError: incompatible character encodings: US-ASCII and ASCII-8BIT
98
+ # ASCII-8BIT is a binary data, I am not able to find how to set encoding in constructor
99
+ if RUBY_VERSION >= "1.9.0"
100
+ @buffer.force_encoding("ASCII-8BIT")
101
+ end
102
+
103
+ sizeof = value.size()
104
+ @buffer << value
105
+ @offset += sizeof
106
+ end
107
+
108
+
109
+ #
110
+ def reset_buffer()
111
+ @offset = 0
112
+ @buffer = ""
113
+ end
114
+
115
+ #
116
+ def get_buffer()
117
+ @buffer
118
+ end
119
+
120
+ #
121
+ def append_buffer(buf)
122
+ @buffer << buf
123
+ end
124
+
125
+
126
+ private
127
+ attr_accessor :buffer
128
+ attr_accessor :offset
129
+ end
130
+ end
131
+ end