예제 #1
0
static void ev_handler(struct ns_connection *nc, int ev, void *p) {
  struct ns_mqtt_message *msg = (struct ns_mqtt_message *)p;
  (void) nc;

#if 0
  if (ev != NS_POLL)
    printf("USER HANDLER GOT %d\n", ev);
#endif

  switch (ev) {
    case NS_CONNECT:
      ns_set_protocol_mqtt(nc);
      ns_send_mqtt_handshake(nc, "dummy");
      break;
    case NS_MQTT_CONNACK:
      if (msg->connack_ret_code != NS_MQTT_CONNACK_ACCEPTED) {
        printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
        exit(1);
      }
      printf("Subscribing to '/stuff'\n");
      ns_mqtt_subscribe(nc, topic_expressions, sizeof(topic_expressions)/sizeof(*topic_expressions), 42);
      break;
    case NS_MQTT_PUBACK:
      printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
      break;
    case NS_MQTT_SUBACK:
      printf("Subscription acknowledged, forwarding to '/test'\n");
      break;
    case NS_MQTT_PUBLISH:
      {
#if 0
        char hex[1024] = {0};
        ns_hexdump(nc->recv_iobuf.buf, msg->payload.len, hex, sizeof(hex));
        printf("Got incoming message %s:\n%s", msg->topic, hex);
#else
        printf("Got incoming message %s: %.*s\n", msg->topic, (int)msg->payload.len, msg->payload.p);
#endif

        printf("Forwarding to /test\n");
        ns_mqtt_publish(nc, "/test", 65, NS_MQTT_QOS(0), msg->payload.p, msg->payload.len);
      }
      break;
    case NS_CLOSE:
      printf("Connection closed\n");
      exit(1);
  }
}
예제 #2
0
static void ev_handler( struct ns_connection *nc, int ev, void *p )
{
    struct ns_mqtt_message *msg = ( struct ns_mqtt_message * )p;
    Cmqttobj *pmqttobj = ( Cmqttobj *)nc->mgr->user_data;

    switch ( ev ) {

        case NS_CONNECT:
            ns_set_protocol_mqtt( nc );
            ns_send_mqtt_handshake( nc, "vscpd" );
            break;

        case NS_MQTT_CONNACK:
            if ( msg->connack_ret_code != NS_MQTT_CONNACK_ACCEPTED ) {
                printf( "Got mqtt connection error: %d\n", msg->connack_ret_code );
                pmqttobj->m_bQuit = true;
                return;
            }
            
            pmqttobj->m_bConnected = true;

            if ( pmqttobj->m_bSubscribe ) {
                ns_mqtt_subscribe( nc, pmqttobj->m_topic_list, 1, 42 );
            }
            else {
                ;
            }
            break;

        case NS_MQTT_PUBACK:
            printf( "Message publishing acknowledged (msg_id: %d)\n", msg->message_id );
            break;

        case NS_MQTT_SUBACK:
            printf( "Subscription acknowledged, forwarding to '/test'\n" );
            break;

        case NS_MQTT_PUBLISH:
        {
            printf( "Got incoming message %s: %.*s\n", msg->topic, ( int )msg->payload.len, msg->payload.p );
            vscpEventEx eventEx;

            if ( !strcmp( msg->topic, pmqttobj->m_topic.ToAscii() ) ) {

                wxString str = wxString::FromAscii( ( const char * )msg->payload.p );
                if ( vscp_setVscpEventExFromString( &eventEx, str ) ) {

                    vscpEvent *pEvent = new vscpEvent;
                    if ( NULL != pEvent ) {

                        pEvent->sizeData = 0;
                        pEvent->pdata = NULL;

                        if ( vscp_doLevel2FilterEx( &eventEx, &pmqttobj->m_vscpfilter ) ) {

                            if ( vscp_convertVSCPfromEx( pEvent, &eventEx ) ) {
                                pmqttobj->m_mutexReceiveQueue.Lock();
                                pmqttobj->m_receiveList.push_back( pEvent );
                                pmqttobj->m_semReceiveQueue.Post();
                                pmqttobj->m_mutexReceiveQueue.Unlock();
                            }
                            else {

                            }
                        }
                        else {
                            vscp_deleteVSCPevent( pEvent );
                        }
                    }
                }
            }
        }
        break;

        case NS_CLOSE:
            printf( "Connection closed\n" );
            pmqttobj->m_bConnected = false;
            pmqttobj->m_bQuit = true;
    }

}