def app_on_receive_event(rcv, msg, app_state): 'Application receiver event callback.' if msg.type == lib.LBM_MSG_DATA: data = ffi.string(ffi.cast('char *', msg.data)) topic_name = ffi.string(ffi.cast('char *', msg.topic_name)) src = ffi.string(ffi.cast('char *', msg.source)) print('Msg:' + data.decode('utf-8') + '[' + topic_name.decode('utf-8') + ':' + src.decode('utf-8') + ':' + str(msg.sequence_number) + '], state=' + str(app_state))
def pylbm_src_cb_proc(src, event, event_data, clientd): 'UM source event application callback.' if event == lib.LBM_SRC_EVENT_CONNECT: clientname = ffi.string(ffi.cast('const char *', event_data)) print('Receiver connect ['+clientname.decode('utf-8')+']') elif event == lib.LBM_SRC_EVENT_DISCONNECT: clientname = ffi.string(ffi.cast('const char *', event_data)) print('Receiver disconnect ['+clientname.decode('utf-8')+']') return 0
def main(): 'Put "main" program in a function to allow callable.' # Setup logging callback. lbmerr(lib.lbm_log(lib.pylbm_log_cb_proc, ffi.NULL)) # Read the config file. err = lib.lbm_config(b'um.cfg') if err == lib.LBM_FAILURE: print("Warning, lbm_config 'um.cfg' error: " + str(ffi.string(lib.lbm_errmsg()))) # Create the context attribute. p_cattr = ffi.new('lbm_context_attr_t **') lbmerr(lib.lbm_context_attr_create(p_cattr)) # Get the attribute pointer. cattr = p_cattr[0] # This test program uses the "new source notification" callback to # demonstrate setting up a callback via a configuration option. # See python function pylbm_src_notify_function_cb() above. lbm_src_notify_func = ffi.new('lbm_src_notify_func_t *') lbm_src_notify_func.notifyfunc = lib.pylbm_src_notify_function_cb lbmerr( lib.lbm_context_attr_setopt(cattr, b'resolver_source_notification_function', lbm_src_notify_func, ffi.sizeof('lbm_src_notify_func_t'))) # Create the context. p_ctx = ffi.new('lbm_context_t **') lbmerr(lib.lbm_context_create(p_ctx, cattr, ffi.NULL, ffi.NULL)) #get the context pointer ctx = p_ctx[0] # Delete the context attribute. lib.lbm_context_attr_delete(cattr) # Create the receiver topic attribute. p_rcv_tattr = ffi.new('lbm_rcv_topic_attr_t **') lbmerr(lib.lbm_rcv_topic_attr_create(p_rcv_tattr)) # Get the attribute pointer. rcv_tattr = p_rcv_tattr[0] # Lookup for the receiver topic. p_topic = ffi.new('lbm_topic_t **') lbmerr(lib.lbm_rcv_topic_lookup(p_topic, ctx, b'lbmtst.py', rcv_tattr)) # Get the topic object. topic = p_topic[0] # Set some application state. app_state = {'abc':123} main.app_state_handle = ffi.new_handle(app_state) # Create the callback object. lbm_rcv_callback = LbmRcvCallback(app_on_receive_event, app_state) callback_handle = ffi.new_handle(lbm_rcv_callback) # Create the receiver. p_rcv = ffi.new('lbm_rcv_t **') lbmerr(lib.lbm_rcv_create(p_rcv, ctx, topic, lib.pylbm_rcv_cb_proc, callback_handle, ffi.NULL)) # Get the receiver object. rcv = p_rcv[0] print('Receiver created for lbmtst.py topic') # Delete the topic attribute. lib.lbm_rcv_topic_attr_delete(rcv_tattr) # Create the source topic attribute. p_src_tattr = ffi.new('lbm_src_topic_attr_t **') lbmerr(lib.lbm_src_topic_attr_create(p_src_tattr)) # Get the attribute pointer. src_tattr = p_src_tattr[0] print('source topic created') # Allocate the desired source topic. p_topic = ffi.new('lbm_topic_t **') lbmerr(lib.lbm_src_topic_alloc(p_topic, ctx, b'lbmtst.py', src_tattr)) # Get the topic object. topic = p_topic[0] # Create the source. p_src = ffi.new('lbm_src_t **') lbmerr(lib.lbm_src_create(p_src, ctx, topic, lib.pylbm_src_cb_proc, ffi.NULL, ffi.NULL)) # Get the source object. src = p_src[0] print('source created') # Delete the topic attribute. lib.lbm_src_topic_attr_delete(src_tattr) # Let topic res complete. time.sleep(1) print('TR complete') # Publish 50 messages. for i in range(50): message = ('message%d' % i).encode('utf-8') lbmerr(lib.lbm_src_send(src, message, len(message), lib.LBM_SRC_BLOCK)) # Let messages be delivered. time.sleep(1) # Cleanup. print('Deleting receiver') lbmerr(lib.lbm_rcv_delete(rcv)) print('Deleting source') lbmerr(lib.lbm_src_delete(src)) print('Deleting context') lbmerr(lib.lbm_context_delete(ctx))
def lbmerr(err): 'Very simple, programmer-friendly (user-hostile) error checker.' assert err != lib.LBM_FAILURE, ffi.string(lib.lbm_errmsg())
def pylbm_log_cb_proc(level, message, clientd): 'UM logger application callback.' print(ffi.string(message).decode('utf-8')) return 0
def pylbm_src_notify_function_cb(topic_str, src_str, clientd): 'New source notification callback.' topic = ffi.string(topic_str).decode('utf-8') source = ffi.string(src_str).decode('utf-8') print('new source: topic=' + topic + ', source_string=' + source) return 0
def pylbm_log_cb_proc(_unused_level, message, _unused_clientd): """UM logger application callback.""" print(ffi.string(message).decode('utf-8')) return 0
def lbmerr(err): """Very simple, programmer-friendly (user-hostile) error checker.""" assert err != lib.LBM_FAILURE, ffi.string(lib.lbm_errmsg().decode('utf-8'))