24
24
TYPE_VALUES = 0x0006
25
25
TYPE_INTERVAL = 0x0007
26
26
27
+ header = struct .Struct ("!2H" )
28
+ number = struct .Struct ("!Q" )
29
+ short = struct .Struct ("!H" )
30
+ double = struct .Struct ("<d" )
31
+ char = struct .Struct ("B" )
32
+ long_ = struct .Struct ("!q" )
33
+
27
34
_value_formats = {
28
- VALUE_COUNTER : "!Q" ,
29
- VALUE_GAUGE : "<d" ,
30
- VALUE_DERIVE : "!q" ,
31
- VALUE_ABSOLUTE : "!Q"
35
+ VALUE_COUNTER : number ,
36
+ VALUE_GAUGE : double ,
37
+ VALUE_DERIVE : long_ ,
38
+ VALUE_ABSOLUTE : number
32
39
}
33
40
34
41
@@ -73,18 +80,19 @@ def __init__(self, name, *db_template):
73
80
self ._value_types = [value_type for dsname , value_type in db_template ]
74
81
self ._value_formats = [
75
82
_value_formats [value_type ] for value_type in self ._value_types ]
76
- self ._message_template = struct .pack (
77
- "!HHH" , TYPE_VALUES , 6 + (9 * len (db_template )),
78
- len (db_template ))
83
+
84
+ self ._message_template = header .pack (
85
+ TYPE_VALUES , 6 + (9 * len (db_template ))
86
+ ) + short .pack (len (db_template ))
79
87
for value_type in self ._value_types :
80
- self ._message_template += struct .pack ("B" , value_type )
88
+ self ._message_template += char .pack (value_type )
81
89
82
90
def encode_values (self , * values ):
83
91
"""Encode a series of values according to the type template."""
84
92
85
93
msg = self ._message_template
86
94
for format_ , dsvalue in zip (self ._value_formats , values ):
87
- msg += struct .pack (format_ , dsvalue )
95
+ msg += format_ .pack (dsvalue )
88
96
89
97
return msg
90
98
@@ -118,22 +126,23 @@ def __init__(
118
126
)
119
127
120
128
def _pack_string (self , typecode , value ):
121
- return struct .pack (
122
- "!HH" , typecode , 5 + len (value )) + value .encode ('ascii' ) + b"\0 "
129
+ return header .pack (
130
+ typecode , 5 + len (value )) + value .encode ('ascii' ) + b"\0 "
123
131
124
132
def send (self , connection , timestamp , * values ):
125
133
"""Send a message on a connection."""
126
134
127
- header = self ._host_message_part + \
128
- struct .pack ("!HHq" , TYPE_TIME , 12 , int (timestamp )) + \
135
+ header_ = self ._host_message_part + \
136
+ header .pack (TYPE_TIME , 12 ) + \
137
+ long_ .pack (int (timestamp )) + \
129
138
self ._remainder_message_parts
130
139
131
140
payload = self .type .encode_values (* values )
132
141
133
- connection .send (header + payload )
142
+ connection .send (header_ + payload )
134
143
135
144
136
- class Connection (object ):
145
+ class ClientConnection (object ):
137
146
connections = {}
138
147
create_mutex = threading .Lock ()
139
148
@@ -155,7 +164,8 @@ def for_host_port(cls, host, port):
155
164
try :
156
165
key = (host , port )
157
166
if key not in cls .connections :
158
- cls .connections [key ] = connection = Connection (host , port )
167
+ cls .connections [key ] = connection = \
168
+ ClientConnection (host , port )
159
169
return connection
160
170
else :
161
171
return cls .connections [key ]
0 commit comments