MCPcopy
hub / github.com/tinode/chat / run

Function run

tn-cli/client.py:198–283  ·  view source on GitHub ↗
(args, schema, secret)

Source from the content-addressed store, hash-verified

196
197# The main processing loop: send messages to server, receive responses.
198def run(args, schema, secret):
199 failed = False
200 try:
201 from prompt_toolkit import PromptSession
202
203 if tn_globals.IsInteractive:
204 tn_globals.Prompt = PromptSession()
205 # Create channel with default credentials.
206 tn_globals.Connection = None
207 if args.ssl:
208 opts = (('grpc.ssl_target_name_override', args.ssl_host),) if args.ssl_host else None
209 tn_globals.Connection = grpc.secure_channel(args.host, grpc.ssl_channel_credentials(), opts)
210 else:
211 tn_globals.Connection = grpc.insecure_channel(args.host)
212
213 # Call the server
214 stream = pbx.NodeStub(tn_globals.Connection).MessageLoop(gen_message(schema, secret, args))
215
216 # Read server responses
217 for msg in stream:
218 if tn_globals.Verbose:
219 stdoutln("\r<= " + to_json(msg))
220
221 if msg.HasField("ctrl"):
222 handle_ctrl(msg.ctrl)
223
224 elif msg.HasField("meta"):
225 what = []
226 if len(msg.meta.sub) > 0:
227 what.append("sub")
228 if msg.meta.HasField("desc"):
229 what.append("desc")
230 if msg.meta.HasField("del"):
231 what.append("del")
232 if len(msg.meta.tags) > 0:
233 what.append("tags")
234 stdoutln("\r<= meta " + ",".join(what) + " " + msg.meta.topic)
235
236 if tn_globals.WaitingFor and tn_globals.WaitingFor.await_id == msg.meta.id:
237 if 'varname' in tn_globals.WaitingFor:
238 tn_globals.Variables[tn_globals.WaitingFor.varname] = msg.meta
239 tn_globals.WaitingFor = None
240
241 elif msg.HasField("data"):
242 stdoutln("\n\rFrom: " + msg.data.from_user_id)
243 stdoutln("Topic: " + msg.data.topic)
244 stdoutln("Seq: " + str(msg.data.seq_id))
245 if msg.data.head:
246 stdoutln("Headers:")
247 for key in msg.data.head:
248 stdoutln("\t" + key + ": "+str(msg.data.head[key]))
249 stdoutln(json.loads(msg.data.content))
250
251 elif msg.HasField("pres"):
252 # 'ON', 'OFF', 'UA', 'UPD', 'GONE', 'ACS', 'TERM', 'MSG', 'READ', 'RECV', 'DEL', 'TAGS', 'AUX'
253 what = pb.ServerPres.What.Name(msg.pres.what)
254 stdoutln("\r<= pres " + what + " " + msg.pres.topic)
255

Callers 1

tn-cli.pyFile · 0.90

Calls 9

stdoutlnFunction · 0.90
to_jsonFunction · 0.90
printerrFunction · 0.90
printoutFunction · 0.90
gen_messageFunction · 0.85
handle_ctrlFunction · 0.85
appendMethod · 0.80
getMethod · 0.80
MessageLoopMethod · 0.65

Tested by

no test coverage detected