1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import org.vertx.java.core.parsetools.RecordParser
16 from core.buffer import Buffer
17
18 __author__ = "Scott Horn"
19 __email__ = "scott@hornmicro.com"
20 __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
23 """A helper class which allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed
24 size records.
25
26 Instances of this class take as input Buffer instances containing raw bytes, and output records.
27 For example, if I had a simple ASCII text protocol delimited by '\\n' and the input was the following:
28
29 buffer1:HELLO\\nHOW ARE Y
30 buffer2:OU?\\nI AM
31 buffer3: DOING OK
32 buffer4:\\n
33
34 Then the output would be:
35
36 buffer1:HELLO
37 buffer2:HOW ARE YOU?
38 buffer3:I AM DOING OK
39
40 Instances of this class can be changed between delimited mode and fixed size record mode on the fly as
41 individual records are read, this allows you to parse protocols where, for example, the first 5 records might
42 all be fixed size (of potentially different sizes), followed by some delimited records, followed by more fixed
43 size records.
44
45 Instances of this class can't currently be used for protocols where the text is encoded with something other than
46 a 1-1 byte-char mapping. TODO extend this class to cope with arbitrary character encodings.
47 """
48
50 self.java_parser = java_parser
51
54
60
61 @staticmethod
63 """Create a new RecordParser instance, initially in delimited mode, and where the delimiter can be represented
64 by a delimiter string endcoded in latin-1 . Don't use this if your String contains other than latin-1 characters.
65
66 Keyword arguments:
67 @param delim: The delimiter string.
68 @param handler: the output handler
69
70 @return: a new RecordParser
71 """
72 return RecordParser(org.vertx.java.core.parsetools.RecordParser.newDelimited(delim, RecordParserHandler(handler)))
73
74 @staticmethod
85
87 """Flip the parser into delimited mode. This method can be called multiple times with different values
88 of delim while data is being parsed.
89
90 Keyword arguments:
91 @param delim: the delimiter string.
92 """
93 self.java_parser.delimitedMode(delim)
94
96 """Flip the parser into fixed size mode. This method can be called multiple times with different values
97 of size while data is being parsed.
98
99 Keyword arguments:
100 @param size: the record size.
101 """
102 self.java_parser.fixedSizeMode(size)
103
105 """ Record Parser handler """
107 self.handler = handler
108
110 """ Call the handler after buffer parsed"""
111 self.handler(Buffer(buffer))
112