update domo
This commit is contained in:
@@ -1,123 +0,0 @@
|
||||
/*!
|
||||
* domi-iot.cpp
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 08/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <adapters/libevent.h>
|
||||
|
||||
#include <nats.h>
|
||||
|
||||
static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
||||
{
|
||||
printf("Received msg: %s - %.*s\n",
|
||||
natsMsg_GetSubject(msg),
|
||||
natsMsg_GetDataLength(msg),
|
||||
natsMsg_GetData(msg));
|
||||
|
||||
// Need to destroy the message!
|
||||
natsMsg_Destroy(msg);
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function of domo-iot daemon.
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// natsConnection *conn = NULL;
|
||||
// natsOptions *opts = NULL;
|
||||
// natsSubscription *sub = NULL;
|
||||
// natsStatus s;
|
||||
// struct event_base *evLoop = NULL;
|
||||
// nats_Open(-1);
|
||||
|
||||
printf("Listening on subject 'foo'\n");
|
||||
|
||||
// One time initialization of things that we need.
|
||||
//natsLibevent_Init();
|
||||
|
||||
// Create a loop.
|
||||
// evLoop = event_base_new();
|
||||
// if (evLoop == NULL)
|
||||
// s = NATS_ERR;
|
||||
|
||||
// if (natsOptions_Create(&opts) != NATS_OK)
|
||||
// s = NATS_NO_MEMORY;
|
||||
|
||||
s = natsOptions_UseGlobalMessageDelivery(opts, true);
|
||||
|
||||
// Indicate which loop and callbacks to use once connected.
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetEventLoop(opts, (void *)evLoop,
|
||||
natsLibevent_Attach,
|
||||
natsLibevent_Read,
|
||||
natsLibevent_Write,
|
||||
natsLibevent_Detach);
|
||||
|
||||
s = natsOptions_SetURL(opts, "nats.nadal-fr.com");
|
||||
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Subscribe(&sub, conn, "foo", onMsg, NULL);
|
||||
|
||||
// For maximum performance, set no limit on the number of pending messages.
|
||||
if (s == NATS_OK)
|
||||
s = natsSubscription_SetPendingLimits(sub, -1, -1);
|
||||
|
||||
// Run the event loop.
|
||||
// This call will return when the connection is closed (either after
|
||||
// receiving all messages, or disconnected and unable to reconnect).
|
||||
if (s == NATS_OK)
|
||||
{
|
||||
event_base_dispatch(evLoop);
|
||||
}
|
||||
|
||||
// If there was an error, print a stack trace and exit
|
||||
if (s != NATS_OK)
|
||||
{
|
||||
nats_PrintLastErrorStack(stderr);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// Anything that is created need to be destroyed
|
||||
natsSubscription_Destroy(sub);
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
|
||||
if (evLoop != NULL)
|
||||
event_base_free(evLoop);
|
||||
|
||||
// To silence reports of memory still in used with valgrind
|
||||
nats_Close();
|
||||
libevent_global_shutdown();
|
||||
|
||||
printf("done\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user