1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2015 - Philippe Proulx <pproulx@efficios.com>
4 # Copyright (C) 2014 - David Goulet <dgoulet@efficios.com>
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 from __future__
import unicode_literals
20 import lttngust
.debug
as dbg
24 # server command header
25 _server_cmd_header_struct
= struct
.Struct('>QII')
28 # server command header size
29 _SERVER_CMD_HEADER_SIZE
= _server_cmd_header_struct
.size
32 class _ServerCmdHeader(object):
33 def __init__(self
, data_size
, cmd_id
, cmd_version
):
34 self
.data_size
= data_size
36 self
.cmd_version
= cmd_version
39 def _server_cmd_header_from_data(data
):
41 data_size
, cmd_id
, cmd_version
= _server_cmd_header_struct
.unpack(data
)
42 except (Exception) as e
:
43 dbg
._pdebug
('cannot decode command header: {}'.format(e
))
46 return _ServerCmdHeader(data_size
, cmd_id
, cmd_version
)
49 class _ServerCmd(object):
50 def __init__(self
, header
):
54 def from_data(cls
, header
, data
):
55 raise NotImplementedError()
58 class _ServerCmdList(_ServerCmd
):
60 def from_data(cls
, header
, data
):
64 class _ServerCmdEnable(_ServerCmd
):
66 _loglevel_struct
= struct
.Struct('>II')
68 def __init__(self
, header
, loglevel
, loglevel_type
, name
):
69 super(self
.__class
__, self
).__init
__(header
)
70 self
.loglevel
= loglevel
71 self
.loglevel_type
= loglevel_type
75 def from_data(cls
, header
, data
):
77 loglevel
, loglevel_type
= cls
._loglevel
_struct
.unpack_from(data
)
78 data_name
= data
[cls
._loglevel
_struct
.size
:]
79 name
= data_name
.rstrip(b
'\0').decode()
81 return cls(header
, loglevel
, loglevel_type
, name
)
82 except (Exception) as e
:
83 dbg
._pdebug
('cannot decode enable command: {}'.format(e
))
87 class _ServerCmdDisable(_ServerCmd
):
88 def __init__(self
, header
, name
):
89 super(self
.__class
__, self
).__init
__(header
)
93 def from_data(cls
, header
, data
):
95 name
= data
.rstrip(b
'\0').decode()
97 return cls(header
, name
)
98 except (Exception) as e
:
99 dbg
._pdebug
('cannot decode disable command: {}'.format(e
))
103 class _ServerCmdRegistrationDone(_ServerCmd
):
105 def from_data(cls
, header
, data
):
109 _SERVER_CMD_ID_TO_SERVER_CMD
= {
112 3: _ServerCmdDisable
,
113 4: _ServerCmdRegistrationDone
,
117 def _server_cmd_from_data(header
, data
):
118 if header
.cmd_id
not in _SERVER_CMD_ID_TO_SERVER_CMD
:
121 return _SERVER_CMD_ID_TO_SERVER_CMD
[header
.cmd_id
].from_data(header
, data
)
124 _CLIENT_CMD_REPLY_STATUS_SUCCESS
= 1
125 _CLIENT_CMD_REPLY_STATUS_INVALID_CMD
= 2
128 class _ClientCmdReplyHeader(object):
129 _payload_struct
= struct
.Struct('>I')
131 def __init__(self
, status_code
=_CLIENT_CMD_REPLY_STATUS_SUCCESS
):
132 self
.status_code
= status_code
135 return self
._payload
_struct
.pack(self
.status_code
)
138 class _ClientCmdReplyEnable(_ClientCmdReplyHeader
):
142 class _ClientCmdReplyDisable(_ClientCmdReplyHeader
):
146 class _ClientCmdReplyList(_ClientCmdReplyHeader
):
147 _nb_events_struct
= struct
.Struct('>I')
148 _data_size_struct
= struct
.Struct('>I')
150 def __init__(self
, names
, status_code
=_CLIENT_CMD_REPLY_STATUS_SUCCESS
):
151 super(self
.__class
__, self
).__init
__(status_code
)
155 upper_data
= super(self
.__class
__, self
).get_data()
156 nb_events_data
= self
._nb
_events
_struct
.pack(len(self
.names
))
159 for name
in self
.names
:
160 names_data
+= name
.encode() + b
'\0'
162 data_size_data
= self
._data
_size
_struct
.pack(len(names_data
))
164 return upper_data
+ data_size_data
+ nb_events_data
+ names_data
167 class _ClientRegisterCmd(object):
168 _payload_struct
= struct
.Struct('>IIII')
170 def __init__(self
, domain
, pid
, major
, minor
):
177 return self
._payload
_struct
.pack(self
.domain
, self
.pid
, self
.major
,