server
– ZMQ based server¶
ZMQ based server to stream content and TCS-like event generators.
-
class
tcs_lib.server.
ZMQServer
(url, context=None)[source]¶ Bases:
object
Create a ZeroMQ server that publishes content on the give
ulr
.Sending events is performed by the
send_event()
method usingzmq.Socket.send_multipart()
.send_event()
andstart()
allow to send generic events.send_tcs_event()
is designed to send events that reflect TCS expectations. If thetcs_event
passed to the method does not contain the__wire_time
key, set it totime.time()
.Parameters: - url : string
url and port to bind the socket to
- context :
zmq.Context
, optional context to use when creating the sockets. If not given, uses the global instance returned by
zmq.Context.instance()
-
send_event
(event)[source]¶ Send the event via the socket.
Parameters: - event : list of string
event to send using
zmq.Socket.send_multipart()
; the events are converted to byte string usingstring_to_bytes()
.
-
send_tcs_event
(tcs_topic, tcs_event)[source]¶ Version of
send_event()
specialized to send TCS-like events.Set
__wire_time
in thetcs_event
totime.time()
.If event is a string, decode it into a dictionary using
json.loads()
.Parameters: - tcs_topic : string
topic of the event
- tcs_event : dict or string
event to send
-
start
(events)[source]¶ Start serving events via the socket
Parameters: - events : generator yielding lists of strings or bytes
each event retrieved in a loop and sent using
send_event()
. If one event isNone
, it is not sent.
-
class
tcs_lib.server.
TCSDBReplay
(db_name, sort_by='none', speedup=1.0, topics=None, convert_number=False, convert_bool=False)[source]¶ Bases:
object
Open a TCS sqlite3 database, query it and return one entry at a time when looping or using the
next()
builtin function.Parameters: - db_name : string
file containing the database
- sort_by : string, optional
whether to sort or not the event_ids. Accepted values:
'none'
,'__data_time'
,'__wire_time'
- speedup : float, optional
speedup to use to replay the database. A value larger than 1 fast-forwards the replay, a smaller value slows the replay down
- topics : list, optional
list of topics to return when iterating. If
None
or[]
, all topics are returned.- convert_number, convert_bool : bool, optional
try to convert database entries to number (int or float) and to boolean, according to the value of the
data_type
column. The conversion is fail-safe
Attributes: - event_ids : iterator
results of the query for the
event_id
. The attribute is filled by the_reset_iter()
- start_wire_time, start_iter_time : float
wire time and current time when calling
__next__()
the first time. Call_reset_iter()
to reset them before starting a new iteration
-
_reset_iter
()[source]¶ Reset the status and allow restarting the iterations.
- Create and execute the query to retrieve the
event_id
and save it inevent_ids
. The query is done on theevent
table, if no ordering'none'
or the'__data_time'
ordering is required, or on theattribute
table, if the'__wire_time'
ordering is required. - Unset the
start_wire_time
andstart_iter_time
This method is called when initializing the class. It can be called to reinitialize the iterator.
Raises: - tcs_lib.DBOrderingError
if the ordering is not known
- Create and execute the query to retrieve the
-
__next__
()[source]¶ Get the next element of
event_id
, build the TCS object and return it.Returns: - list of strings or ``None``
if the topic is accepted returns
[topic, json(event)]
, otherwise returnsNone
-
_event_dict
(event_id)[source]¶ Create dictionary representing the event with id
event_id
Parameters: - event_id : string
id of the event
Returns: - result : dictionary
event information
-
_convert_to_type
(value, type_)[source]¶ Try to convert value to the given type, if the conversion is required
Parameters: - value : string
value to convert
- type_ : string
type of value. Known types: “number”, “boolean”, “string”. Any unknown type is treated as a string
Returns: - value : int, float, bool or string
converted value
-
_convert_to_number
(value)[source]¶ Try to convert the input
value
from string toint
orfloat
, in that order. If it fails, returnsvalue
unchanged