2020-05-20 22:58:53 +02:00
|
|
|
/* -*- compile-command: "make MEMDEBUG=TRUE -j3"; -*- */
|
|
|
|
/*
|
2022-12-20 04:53:57 +01:00
|
|
|
* Copyright 2020 - 2022 by Eric House (xwords@eehouse.org). All rights
|
|
|
|
* reserved.
|
2020-05-20 22:58:53 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mosquitto.h>
|
|
|
|
|
|
|
|
#include "mqttcon.h"
|
|
|
|
#include "gsrcwrap.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "strutils.h"
|
|
|
|
|
|
|
|
typedef struct _MQTTConStorage {
|
|
|
|
LaunchParams* params;
|
|
|
|
struct mosquitto* mosq;
|
|
|
|
MQTTDevID clientID;
|
|
|
|
gchar clientIDStr[32];
|
|
|
|
int msgPipe[2];
|
2023-01-11 20:10:45 +01:00
|
|
|
XP_Bool connected;
|
2023-03-25 04:28:45 +01:00
|
|
|
GSList* queue;
|
2020-05-20 22:58:53 +02:00
|
|
|
} MQTTConStorage;
|
|
|
|
|
|
|
|
#define DEFAULT_QOS 2
|
|
|
|
|
2023-03-25 04:28:45 +01:00
|
|
|
typedef struct _QElem {
|
|
|
|
gchar* topic;
|
|
|
|
uint8_t* buf;
|
|
|
|
uint16_t len;
|
|
|
|
int mid;
|
|
|
|
} QElem;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sendQueueHead( MQTTConStorage* storage )
|
|
|
|
{
|
2024-01-05 17:46:19 +01:00
|
|
|
XP_LOGFF( "queue len: %d", g_slist_length(storage->queue) );
|
2023-03-25 04:28:45 +01:00
|
|
|
if ( storage->connected ) {
|
|
|
|
for ( GSList* iter = storage->queue; !!iter; iter = iter->next ) {
|
|
|
|
QElem* elem = (QElem*)iter->data;
|
|
|
|
if ( 0 == elem->mid ) {
|
2023-04-07 05:02:30 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
int err =
|
|
|
|
#endif
|
|
|
|
mosquitto_publish( storage->mosq, &elem->mid, elem->topic,
|
|
|
|
elem->len, elem->buf, DEFAULT_QOS, true );
|
2024-01-05 17:46:19 +01:00
|
|
|
XP_LOGFF( "mosquitto_publish(topic=%s, msgLen=%d) => %s; mid=%d", elem->topic,
|
2023-03-25 04:28:45 +01:00
|
|
|
elem->len, mosquitto_strerror(err), elem->mid );
|
2024-01-05 17:46:19 +01:00
|
|
|
/* Remove this so all are resent together? */
|
2023-03-25 04:28:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_RETURN_VOID();
|
|
|
|
} /* sendQueueHead */
|
|
|
|
|
|
|
|
typedef struct _FindState {
|
|
|
|
QElem elem;
|
|
|
|
XP_Bool found;
|
|
|
|
} FindState;
|
|
|
|
|
|
|
|
static bool
|
|
|
|
elemsEqual( QElem* qe1, QElem* qe2 )
|
|
|
|
{
|
|
|
|
return qe1->len == qe2->len
|
|
|
|
&& 0 == strcmp( qe1->topic, qe2->topic )
|
|
|
|
&& 0 == memcmp( qe1->buf, qe2->buf, qe1->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
findMsg( gpointer data, gpointer user_data )
|
|
|
|
{
|
|
|
|
QElem* qe = (QElem*)data;
|
|
|
|
FindState* fsp = (FindState*)user_data;
|
|
|
|
if ( !fsp->found && elemsEqual( qe, &fsp->elem ) ) {
|
|
|
|
fsp->found = XP_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
queueIdle( gpointer data )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)data;
|
|
|
|
sendQueueHead( storage );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tickleQueue( MQTTConStorage* storage )
|
|
|
|
{
|
|
|
|
ADD_ONETIME_IDLE( queueIdle, storage );
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:29:57 +02:00
|
|
|
// Ubuntu 22.4 is at 2.72.4 right now. Debian bookworm at 2.74.6. Ubuntu 20.4
|
|
|
|
// is at 2.64.6
|
|
|
|
#if GLIB_CHECK_VERSION(2,68,0)
|
|
|
|
# define G_MEMDUP(a,b) g_memdup2((a), (b))
|
|
|
|
#else
|
|
|
|
# define G_MEMDUP(a,b) g_memdup((a), (b))
|
|
|
|
#endif
|
|
|
|
|
2023-03-25 04:28:45 +01:00
|
|
|
/* Add to queue if not already there */
|
|
|
|
static void
|
|
|
|
enqueue( MQTTConStorage* storage, const char* topic,
|
|
|
|
const XP_U8* buf, XP_U16 len )
|
|
|
|
{
|
|
|
|
FindState fs = {
|
|
|
|
.elem.buf = (uint8_t*)buf,
|
|
|
|
.elem.len = len,
|
|
|
|
.elem.topic = (gchar*)topic,
|
|
|
|
};
|
|
|
|
g_slist_foreach( storage->queue, findMsg, &fs );
|
|
|
|
|
|
|
|
if ( fs.found ) {
|
|
|
|
XP_LOGFF( "dropping duplicate message" );
|
|
|
|
} else {
|
|
|
|
QElem* elem = g_malloc0( sizeof(*elem) );
|
|
|
|
elem->topic = g_strdup( topic );
|
2023-10-28 20:29:57 +02:00
|
|
|
elem->buf = G_MEMDUP( buf, len );
|
2023-03-25 04:28:45 +01:00
|
|
|
elem->len = len;
|
|
|
|
storage->queue = g_slist_append( storage->queue, elem );
|
|
|
|
XP_LOGFF( "added elem; len now %d", g_slist_length(storage->queue) );
|
|
|
|
|
|
|
|
tickleQueue( storage );
|
|
|
|
}
|
2024-01-05 17:46:19 +01:00
|
|
|
} /* enqueue */
|
2023-03-25 04:28:45 +01:00
|
|
|
|
|
|
|
typedef struct _RemoveState {
|
|
|
|
MQTTConStorage* storage;
|
|
|
|
int mid;
|
|
|
|
XP_Bool found;
|
|
|
|
} RemoveState;
|
|
|
|
|
|
|
|
static void
|
|
|
|
removeWithMid( gpointer data, gpointer user_data )
|
|
|
|
{
|
|
|
|
QElem* qe = (QElem*)data;
|
|
|
|
RemoveState* rsp = (RemoveState*)user_data;
|
|
|
|
if ( qe->mid == rsp->mid ) {
|
|
|
|
XP_ASSERT( !rsp->found );
|
|
|
|
rsp->found = XP_TRUE;
|
|
|
|
MQTTConStorage* storage = rsp->storage;
|
|
|
|
storage->queue = g_slist_remove( storage->queue, qe );
|
|
|
|
XP_LOGFF( "removed elem with mid %d; len now %d", rsp->mid,
|
|
|
|
g_slist_length(storage->queue) );
|
|
|
|
|
|
|
|
g_free( qe->topic );
|
|
|
|
g_free( qe->buf );
|
|
|
|
g_free( qe );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 06:51:36 +02:00
|
|
|
static gint
|
|
|
|
dequeueIdle( gpointer data )
|
2023-03-25 04:28:45 +01:00
|
|
|
{
|
2023-04-07 06:51:36 +02:00
|
|
|
LOG_FUNC();
|
|
|
|
RemoveState* rsp = (RemoveState*)data;
|
|
|
|
XP_ASSERT( !rsp->found );
|
|
|
|
|
|
|
|
g_slist_foreach( rsp->storage->queue, removeWithMid, rsp );
|
|
|
|
if ( !rsp->found ) {
|
|
|
|
XP_LOGFF( "failed to find mid %d", rsp->mid );
|
2023-03-25 04:28:45 +01:00
|
|
|
}
|
2023-04-07 06:51:36 +02:00
|
|
|
g_free( rsp );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dequeue_on_idle( MQTTConStorage* storage, int mid )
|
|
|
|
{
|
|
|
|
RemoveState* rsp = g_malloc0( sizeof(*rsp) );
|
|
|
|
rsp->storage = storage;
|
|
|
|
rsp->mid = mid;
|
|
|
|
// ADD_ONETIME_IDLE() has trouble with multiple instance with same idle
|
|
|
|
// proc, so:
|
|
|
|
/*guint res = */g_idle_add( dequeueIdle, rsp );
|
2023-03-25 04:28:45 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
static MQTTConStorage*
|
|
|
|
getStorage( LaunchParams* params )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)params->mqttConStorage;
|
|
|
|
if ( NULL == storage ) {
|
|
|
|
storage = XP_CALLOC( params->mpool, sizeof(*storage) );
|
|
|
|
params->mqttConStorage = storage;
|
|
|
|
}
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
loadClientID( LaunchParams* params, MQTTConStorage* storage )
|
|
|
|
{
|
|
|
|
dvc_getMQTTDevID( params->dutil, NULL_XWE, &storage->clientID );
|
|
|
|
formatMQTTDevID( &storage->clientID, storage->clientIDStr,
|
|
|
|
VSIZE(storage->clientIDStr) );
|
2020-06-01 08:42:44 +02:00
|
|
|
XP_ASSERT( 16 == strlen(storage->clientIDStr) );
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
onMessageReceived( struct mosquitto* XP_UNUSED_DBG(mosq), void *userdata,
|
2022-12-16 22:33:36 +01:00
|
|
|
const struct mosquitto_message* message )
|
2020-05-20 22:58:53 +02:00
|
|
|
{
|
|
|
|
XP_LOGFF( "(len=%d)", message->payloadlen );
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)userdata;
|
|
|
|
XP_ASSERT( storage->mosq == mosq );
|
|
|
|
|
|
|
|
XP_ASSERT( message->payloadlen < 0x7FFF );
|
2022-12-16 22:33:36 +01:00
|
|
|
|
|
|
|
const int msgPipe = storage->msgPipe[1];
|
|
|
|
/* write topic, then message */
|
|
|
|
const char* topic = message->topic;
|
|
|
|
short msgLen = htons(1 + strlen(topic));
|
|
|
|
write( msgPipe, &msgLen, sizeof(msgLen) );
|
|
|
|
write( msgPipe, topic, 1 + strlen(topic) );
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
short len = (short)message->payloadlen;
|
|
|
|
len = htons( len );
|
2022-12-16 22:33:36 +01:00
|
|
|
write( msgPipe, &len, sizeof(len) );
|
|
|
|
write( msgPipe, message->payload, message->payloadlen );
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-11 20:10:45 +01:00
|
|
|
connect_callback( struct mosquitto* mosq, void* userdata,
|
|
|
|
int XP_UNUSED_DBG(connErr) )
|
2020-05-20 22:58:53 +02:00
|
|
|
{
|
2023-01-11 20:10:45 +01:00
|
|
|
XP_LOGFF( "(err=%s)", mosquitto_strerror(connErr) );
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)userdata;
|
|
|
|
storage->connected = XP_TRUE;
|
|
|
|
|
|
|
|
XP_UCHAR topicStorage[256];
|
|
|
|
XP_UCHAR* topics[4];
|
|
|
|
XP_U16 nTopics = VSIZE(topics);
|
|
|
|
dvc_getMQTTSubTopics( storage->params->dutil, NULL_XWE,
|
|
|
|
topicStorage, VSIZE(topicStorage),
|
|
|
|
&nTopics, topics );
|
|
|
|
int mid;
|
|
|
|
int err = mosquitto_subscribe_multiple( mosq, &mid, nTopics, topics,
|
|
|
|
DEFAULT_QOS, 0, NULL );
|
|
|
|
XP_LOGFF( "mosquitto_subscribe(topics[0]=%s, etc) => %s, mid=%d", topics[0],
|
|
|
|
mosquitto_strerror(err), mid );
|
2023-02-02 04:08:04 +01:00
|
|
|
XP_USE(err);
|
2023-03-25 04:28:45 +01:00
|
|
|
|
|
|
|
tickleQueue( storage );
|
2024-01-05 17:46:19 +01:00
|
|
|
} /* connect_callback */
|
2020-05-20 22:58:53 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
subscribe_callback( struct mosquitto *mosq, void *userdata, int mid,
|
|
|
|
int qos_count, const int *granted_qos)
|
|
|
|
{
|
|
|
|
XP_USE(mosq);
|
|
|
|
XP_USE(userdata);
|
|
|
|
XP_USE(mid);
|
|
|
|
XP_USE(qos_count);
|
|
|
|
XP_USE(granted_qos);
|
|
|
|
XP_LOGFF ("Subscribed (mid: %d): %d", mid, granted_qos[0]);
|
2020-06-11 22:11:56 +02:00
|
|
|
for ( int ii = 1; ii < qos_count; ii++ ) {
|
2020-05-20 22:58:53 +02:00
|
|
|
XP_LOGFF(", %d", granted_qos[ii]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 04:28:45 +01:00
|
|
|
static void
|
|
|
|
publish_callback( struct mosquitto* XP_UNUSED(mosq), void* userdata, int mid )
|
|
|
|
{
|
|
|
|
XP_LOGFF( "publish of mid %d successful", mid );
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)userdata;
|
2023-04-07 06:51:36 +02:00
|
|
|
dequeue_on_idle( storage, mid );
|
2023-03-25 04:28:45 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
static void
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
log_callback( struct mosquitto *mosq, void *userdata, int level,
|
2021-03-19 21:03:39 +01:00
|
|
|
const char* str )
|
2020-05-20 22:58:53 +02:00
|
|
|
{
|
|
|
|
XP_USE(mosq);
|
|
|
|
XP_USE(userdata);
|
|
|
|
XP_USE(level);
|
2021-03-15 15:52:54 +01:00
|
|
|
XP_USE(str);
|
|
|
|
/* XP_LOGFF( "msg: %s", str ); */
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
handle_gotmsg( GIOChannel* source, GIOCondition XP_UNUSED(condition), gpointer data )
|
|
|
|
{
|
|
|
|
// XP_LOGFF( "(len=%d)", message->payloadlen );
|
|
|
|
LOG_FUNC();
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)data;
|
|
|
|
|
|
|
|
int pipe = g_io_channel_unix_get_fd( source );
|
|
|
|
XP_ASSERT( pipe == storage->msgPipe[0] );
|
2022-12-16 22:33:36 +01:00
|
|
|
|
|
|
|
short topicLen;
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
ssize_t nRead =
|
|
|
|
#endif
|
2022-12-16 22:33:36 +01:00
|
|
|
read( pipe, &topicLen, sizeof(topicLen) );
|
|
|
|
XP_ASSERT( nRead == sizeof(topicLen) );
|
|
|
|
topicLen = ntohs(topicLen);
|
|
|
|
XP_U8 topicBuf[topicLen];
|
|
|
|
#ifdef DEBUG
|
|
|
|
nRead =
|
|
|
|
#endif
|
|
|
|
read( pipe, topicBuf, topicLen );
|
|
|
|
XP_ASSERT( nRead == topicLen);
|
|
|
|
XP_ASSERT( '\0' == topicBuf[topicLen-1] );
|
|
|
|
|
|
|
|
short msgLen;
|
|
|
|
#ifdef DEBUG
|
|
|
|
nRead =
|
|
|
|
#endif
|
|
|
|
read( pipe, &msgLen, sizeof(msgLen) );
|
|
|
|
XP_ASSERT( nRead == sizeof(msgLen) );
|
|
|
|
msgLen = ntohs(msgLen);
|
|
|
|
XP_U8 msgBuf[msgLen];
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
nRead =
|
|
|
|
#endif
|
2022-12-16 22:33:36 +01:00
|
|
|
read( pipe, msgBuf, msgLen );
|
|
|
|
XP_ASSERT( nRead == msgLen );
|
2020-05-20 22:58:53 +02:00
|
|
|
|
2022-12-16 22:33:36 +01:00
|
|
|
dvc_parseMQTTPacket( storage->params->dutil, NULL_XWE,
|
|
|
|
(XP_UCHAR*)topicBuf, msgBuf, msgLen );
|
2023-02-02 04:08:04 +01:00
|
|
|
LOG_RETURN_VOID();
|
2020-05-20 22:58:53 +02:00
|
|
|
return TRUE;
|
|
|
|
} /* handle_gotmsg */
|
|
|
|
|
|
|
|
void
|
|
|
|
mqttc_init( LaunchParams* params )
|
|
|
|
{
|
2022-10-08 19:41:29 +02:00
|
|
|
if ( types_hasType( params->conTypes, COMMS_CONN_MQTT ) ) {
|
|
|
|
XP_ASSERT( !params->mqttConStorage );
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
|
|
|
storage->params = params;
|
2020-05-20 22:58:53 +02:00
|
|
|
|
2022-10-08 19:41:29 +02:00
|
|
|
loadClientID( params, storage );
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#ifdef DEBUG
|
2022-10-08 19:41:29 +02:00
|
|
|
int res =
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#endif
|
2022-10-08 19:41:29 +02:00
|
|
|
pipe( storage->msgPipe );
|
|
|
|
XP_ASSERT( !res );
|
|
|
|
ADD_SOCKET( storage, storage->msgPipe[0], handle_gotmsg );
|
2020-05-20 22:58:53 +02:00
|
|
|
|
2022-10-08 19:41:29 +02:00
|
|
|
int err = mosquitto_lib_init();
|
|
|
|
XP_LOGFF( "mosquitto_lib_init() => %d", err );
|
|
|
|
XP_ASSERT( 0 == err );
|
|
|
|
|
|
|
|
bool cleanSession = false;
|
|
|
|
struct mosquitto* mosq = storage->mosq =
|
|
|
|
mosquitto_new( storage->clientIDStr, cleanSession, storage );
|
|
|
|
|
|
|
|
err = mosquitto_username_pw_set( mosq, "xwuser", "xw4r0cks" );
|
|
|
|
XP_LOGFF( "mosquitto_username_pw_set() => %s", mosquitto_strerror(err) );
|
|
|
|
|
|
|
|
mosquitto_log_callback_set( mosq, log_callback );
|
|
|
|
mosquitto_connect_callback_set( mosq, connect_callback );
|
|
|
|
mosquitto_message_callback_set( mosq, onMessageReceived );
|
|
|
|
mosquitto_subscribe_callback_set( mosq, subscribe_callback );
|
2023-03-25 04:28:45 +01:00
|
|
|
mosquitto_publish_callback_set( mosq, publish_callback );
|
2022-10-08 19:41:29 +02:00
|
|
|
|
|
|
|
int keepalive = 60;
|
|
|
|
err = mosquitto_connect( mosq, params->connInfo.mqtt.hostName,
|
|
|
|
params->connInfo.mqtt.port, keepalive );
|
2023-11-21 03:15:32 +01:00
|
|
|
XP_LOGFF( "mosquitto_connect(host=%s, port=%d) => %s",
|
|
|
|
params->connInfo.mqtt.hostName,
|
|
|
|
params->connInfo.mqtt.port,
|
2022-10-08 19:41:29 +02:00
|
|
|
mosquitto_strerror(err) );
|
|
|
|
if ( MOSQ_ERR_SUCCESS == err ) {
|
|
|
|
err = mosquitto_loop_start( mosq );
|
|
|
|
XP_ASSERT( !err );
|
|
|
|
} else {
|
|
|
|
XP_LOGFF( "failed to connect so not proceeding" );
|
|
|
|
}
|
2020-05-20 22:58:53 +02:00
|
|
|
} else {
|
2022-10-08 19:41:29 +02:00
|
|
|
XP_LOGFF( "MQTT disabled; doing nothing" );
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mqttc_cleanup( LaunchParams* params )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
2024-01-05 17:46:19 +01:00
|
|
|
(void)mosquitto_loop_stop( storage->mosq, true ); /* blocks until thread dies */
|
2020-05-20 22:58:53 +02:00
|
|
|
mosquitto_destroy( storage->mosq );
|
|
|
|
storage->mosq = NULL;
|
|
|
|
mosquitto_lib_cleanup();
|
|
|
|
|
2024-01-05 17:46:19 +01:00
|
|
|
XP_LOGFF( "quitting with %d undelievered messages", g_slist_length(storage->queue) );
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
XP_ASSERT( params->mqttConStorage == storage ); /* cheat */
|
|
|
|
XP_FREEP( params->mpool, &storage );
|
|
|
|
params->mqttConStorage = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MQTTDevID*
|
|
|
|
mqttc_getDevID( LaunchParams* params )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
|
|
|
return &storage->clientID;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar*
|
|
|
|
mqttc_getDevIDStr( LaunchParams* params )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
|
|
|
return storage->clientIDStr;
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:20:02 +01:00
|
|
|
|
|
|
|
static void
|
2022-12-20 20:55:30 +01:00
|
|
|
msgAndTopicProc( void* closure, const XP_UCHAR* topic, const XP_U8* buf, XP_U16 len )
|
2022-12-20 18:20:02 +01:00
|
|
|
{
|
|
|
|
MQTTConStorage* storage = (MQTTConStorage*)closure;
|
2023-03-25 04:28:45 +01:00
|
|
|
(void)enqueue( storage, topic, buf, len );
|
2022-12-20 18:20:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
void
|
2022-12-31 01:14:22 +01:00
|
|
|
mqttc_invite( LaunchParams* params, const NetLaunchInfo* nli,
|
2022-09-06 21:16:29 +02:00
|
|
|
const MQTTDevID* invitee )
|
2020-05-20 22:58:53 +02:00
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#ifdef DEBUG
|
2020-05-20 22:58:53 +02:00
|
|
|
gchar buf[32];
|
|
|
|
XP_LOGFF( "need to send to %s", formatMQTTDevID(invitee, buf, sizeof(buf) ) );
|
2020-06-01 08:42:44 +02:00
|
|
|
XP_ASSERT( 16 == strlen(buf) );
|
add filtering to wordlist browser
Add a basic regular expression engine to the dictiter, and to the UI add
the ability to filter for "starts with", "contains" and "ends with",
which translate into ANDed RE_*, _*RE_* and _*RE, respectively (with
_ standing for blank/wildcard). The engine's tightly integrated with the
next/prevWord() functions for greatest possible speed, but unless
there's no pattern does slow things down a bit (especially when "ENDS
WITH" is used.) The full engine is not exposed (users can't provide raw
REs), and while the parser will accept nesting (e.g. ([AB]_*[CD]){2,5}
to mean words from 2-5 tiles long starting with A or B and ending with C
or D) the engine can't handle it. Which is why filtering for word length
is handled separately from REs (but also tightly integrated.)
Users can enter strings that don't map to tiles. They now get an
error. It made sense for the error alert to have a "Show tiles"
button, so there's now a dialog listing all the tiles in a wordlist,
something the browser has needed all along.
2020-08-05 18:25:33 +02:00
|
|
|
#endif
|
2020-05-20 22:58:53 +02:00
|
|
|
|
2022-12-20 18:20:02 +01:00
|
|
|
dvc_makeMQTTInvites( params->dutil, NULL_XWE, msgAndTopicProc, storage,
|
2022-12-31 01:14:22 +01:00
|
|
|
invitee, nli );
|
2022-12-20 04:53:57 +01:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
XP_S16
|
2022-12-31 01:14:22 +01:00
|
|
|
mqttc_send( LaunchParams* params, XP_U32 gameID,
|
2023-03-18 23:31:20 +01:00
|
|
|
const SendMsgsPacket* const msgs,
|
2023-03-18 03:21:57 +01:00
|
|
|
XP_U16 streamVersion, const MQTTDevID* addressee )
|
2020-05-20 22:58:53 +02:00
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
2023-03-18 03:21:57 +01:00
|
|
|
XP_S16 nSent = dvc_makeMQTTMessages( params->dutil, NULL_XWE,
|
|
|
|
msgAndTopicProc, storage,
|
2023-03-18 23:31:20 +01:00
|
|
|
msgs, addressee,
|
2023-03-18 03:21:57 +01:00
|
|
|
gameID, streamVersion );
|
|
|
|
return nSent;
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 02:23:30 +01:00
|
|
|
void
|
|
|
|
mqttc_onInviteHandled( LaunchParams* params, const NetLaunchInfo* nli )
|
|
|
|
{
|
|
|
|
LOG_FUNC();
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
|
|
|
dvc_makeMQTTNukeInvite( params->dutil, NULL_XWE,
|
|
|
|
msgAndTopicProc, storage, nli );
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:58:53 +02:00
|
|
|
void
|
|
|
|
mqttc_notifyGameGone( LaunchParams* params, const MQTTDevID* addressee, XP_U32 gameID )
|
|
|
|
{
|
|
|
|
MQTTConStorage* storage = getStorage( params );
|
2022-12-20 20:55:30 +01:00
|
|
|
dvc_makeMQTTNoSuchGames( params->dutil, NULL_XWE,
|
|
|
|
msgAndTopicProc, storage,
|
2022-12-31 01:14:22 +01:00
|
|
|
addressee, gameID );
|
2020-05-20 22:58:53 +02:00
|
|
|
}
|