forked from arienchen/pytibrv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtibrvsend.py
More file actions
138 lines (102 loc) · 3.1 KB
/
Copy pathtibrvsend.py
File metadata and controls
138 lines (102 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
##
# tibrvsend.py
# rewrite TIBRV example: tibrvsend.c
# using ython Object Model
#
# LAST MODIFIED: V1.0 2016-12-22 ARIEN arien.chen@gmail.com
#
import sys
import getopt
import time
from pytibrv.Tibrv import *
def usage():
print('TIBRV Sender: tibrvsend.py')
print('')
print('tibrvsend.py [--service service] [--network network]')
print(' [--daemon daemon] <subject> <message>')
print()
sys.exit(1)
def get_params(argv):
try:
opts, args = getopt.getopt(argv, '', ['service=', 'network=', 'daemon='])
except getopt.GetoptError:
usage()
service = None
network = None
daemon = None
for opt, arg in opts:
if opt == '--service':
service = arg
elif opt == '--network':
network = arg
elif opt == '--daemon':
daemon = arg
else:
usage()
if len(args) != 2:
usage()
return service, network, daemon, args[0], args[1]
def callback(event, msg: TibrvMsg, closure):
print("RECV [{}] < {}".format(msg.sendSubject, str(msg)))
pass
def watiForReply(tx, replySubject, timeout_sec):
try:
queue = TibrvQueue()
queue.create()
listener = TibrvListener();
listener.create(queue, TibrvMsgCallback(callback), tx, replySubject, None)
start_time = time.monotonic()
elapsed_sec = 0
while elapsed_sec < timeout_sec:
if queue.count > 0:
return True
time.sleep(0.5)
x = time.monotonic() - start_time
elapsed_sec += 0.5
print("timeout!!!!")
return False
finally:
queue.destroy()
listener.destroy()
# MAIN PROGRAM
def main(argv):
progname = argv[0]
# service, network, daemon, subj, msg_data = get_params(argv[1:])
service = "8200"
network = "192.168.100.115"
daemon = "tcp:7500"
subj = "TEST.TOPIC"
msg_data = "TEST_MESSAGE"
err = Tibrv.open()
if err != TIBRV_OK:
print('{}: Failed to open TIB/RV: {}'.format('', progname, TibrvStatus.text(err)))
sys.exit(1);
tx = TibrvTx()
err = tx.create(service, network, daemon)
if err != TIBRV_OK:
print('{}: Failed to initialize transport: {}'.format('', progname, TibrvStatus.text(err)))
sys.exit(1)
tx.description = progname
try:
msg = TibrvMsg.create(1024)
except TibrvError as e:
print('{}: Failed to create message: {}'.format('', progname, e.text(err)))
sys.exit(1)
err = msg.setStr('DATA', msg_data)
if err == TIBRV_OK:
msg.sendSubject = subj
msg.replySubject = "REPLY.SUBJECT"
if msg.error is None:
err = tx.send(msg)
watiForReply(tx, "REPLY.SUBJECT", 10)
else:
err = msg.error.code()
if err != TIBRV_OK:
print('{}: {} in sending "{}" to "{}"'. format(progname, tibrvStatus_GetText(err), msg_data, subj))
del msg
del tx
Tibrv.close()
sys.exit(0)
return
if __name__ == "__main__":
main(sys.argv)