Python Client  0.0.0.0
Python wrapper for Corelink DLL
corelink_send_stream.py
Go to the documentation of this file.
1 """"
2  @file corelink_send_stream.py
3  Handler class for the sender streams.
4  A container for the stream id and necessary data to call associated functions
5 """
6 import ctypes
7 import json
8 import corelink_const
9 import corelink_stream_data
10 
11 
12 class SendStream:
13  """
14  Send Stream
15  """
16  def __init__(self,*arg):
17  if len(arg)==2:
18  self.update(arg[0],arg[1],arg[2])
19  if len(arg)==1:
20  if isinstance(arg[0],corelink_stream_data.StreamData):
21  stream_data = arg[0]
22  if(stream_data.state & corelink_const.STATE_CODES['STREAM_STATE_SEND']) >0:
23  self.update(stream_data.stream_id,stream_data.state,stream_data.stream_ref)
24  else:
25  self.update(-1,corelink_const.STATE_CODES['STREAM_STATE_NONE'],-1)
26  if isinstance(arg[0],SendStream):
27  rhs = arg[0]
28  self.update(rhs.stream_id,rhs.state,rhs.stream_ref)
29 
30  def send(self,*arg):
31  """
32  send
33  """
34  if len(arg)==1:
35  msg = arg[0]
36  federation_id = 0
37  self.send_msg(self.state,self.stream_ref,self.stream_id,federation_id,msg,len(msg))
38  if len(arg)==2:
39  msg = arg[0]
40  federation_id = arg[1]
41  self.send_msg(self.state,self.stream_ref,self.stream_id,federation_id,msg,len(msg))
42  if len(arg) == 3:
43  msg = arg[0]
44  msg_len = arg[1]
45  federation_id = arg[2]
46  self.send_msg(self.state,self.stream_ref,self.stream_id,federation_id,msg,msg_len)
47 
48  def send_json(self,msg,json_obj,server_check=False,federation_id=0):
49  """
50  send_json
51  """
52  corelink_const.LIB.sendMsgJson.argtypes = [ctypes.c_int,
53  ctypes.c_int,
54  ctypes.POINTER(ctypes.c_int),
55  ctypes.c_int,
56  ctypes.c_char_p,
57  ctypes.c_int,
58  ctypes.c_char_p,
59  ctypes.c_int,
60  ctypes.c_bool]
61  state_c = ctypes.c_int(self.state)
62  stream_ref_c = ctypes.c_int(self.stream_ref)
63  stream_id_c = ctypes.c_int(self.stream_id)
64  federation_id_c = ctypes.c_int(federation_id)
65  msg_c = ctypes.create_string_buffer(msg.encode('utf-8'))
66  msg_len_c = ctypes.c_int(len(msg))
67  buffer = json.dumps(json_obj)
68  buffer_c = ctypes.create_string_buffer(buffer.encode('utf-8'))
69  buffer_len_c = ctypes.c_int(len(buffer))
70  server_check_c = ctypes.c_bool(server_check)
71  corelink_const.LIB.sendMsgJson(state_c,
72  stream_ref_c,
73  stream_id_c,
74  federation_id_c,
75  msg_c,
76  msg_len_c,
77  buffer_c,
78  buffer_len_c,
79  server_check_c)
80 
81  # pylint: disable=too-many-arguments
82  # Seven is reasonable in this case.
83  @staticmethod
84  def send_msg(state,stream_ref,stream_id,federation_id,msg,msg_len):
85  """"
86  send message
87  """
88  corelink_const.LIB.sendMsg.argtypes = [ctypes.c_int,
89  ctypes.c_int,
90  ctypes.POINTER(ctypes.c_int),
91  ctypes.c_int,
92  ctypes.c_char_p,
93  ctypes.c_int]
94  state_c = ctypes.c_int(state)
95  stream_ref_c = ctypes.c_int(stream_ref)
96  stream_id_c = ctypes.c_int(stream_id)
97  federation_id_c = ctypes.c_int(federation_id)
98  msg_c = ctypes.create_string_buffer(msg.encode('utf-8'))
99  msg_len_c = ctypes.c_int(msg_len)
100  corelink_const.LIB.sendMsg(state_c,stream_ref_c,stream_id_c,federation_id_c,msg_c,msg_len_c)
101 
102 
103  def update(self,stream_id,state,stream_ref):
104  """"
105  update the parameters
106  """
107  self.stream_id = stream_id
108  self.state = state
109  self.stream_ref = stream_ref