Beispiel #1
0
/** Thread that handles incoming messages from mio
 * @param context
 * @param mio_sock
 */
static void* _mio_handler_write(void* vcontext)
{
    multiplexer_context_t *context = (multiplexer_context_t*) vcontext;
    char* message = NULL;
    size_t message_len = 0;

    mio_response_t *response;
    while(1)
    {
        response = mio_response_new();
        //printf("waiting for xmpp message\n");
        mio_pubsub_data_receive(context -> mio_info -> mio, response);
        if (response -> stanza == NULL)
        {
            mio_response_free(response);
            printf("IPC ERROR: data receive stanza null\n");

            continue;
        }
        //mio_stanza_to_text(response -> stanza, &message, &message_len);
        xmpp_stanza_to_text(response -> stanza -> xmpp_stanza, &message, &message_len);
       
        // receive message
        printf("Distributing\n%s\n",message);
        _distribute_message(context,message,0);
        mio_response_free(response);
        if (message_len != 0)
            free(message);
    }
    return NULL;
}
Beispiel #2
0
/* Check if the received stanza is <handshake/> and set auth to true
 * and fire connection handler.
 */
int _handle_component_hs_response(xmpp_conn_t * const conn,
            xmpp_stanza_t * const stanza,
            void * const userdata)
{
    char *name;

    xmpp_timed_handler_delete(conn, _handle_missing_handshake);

    name = xmpp_stanza_get_name(stanza);
    if (strcmp(name, "handshake") != 0) {
        char *msg;
        size_t msg_size;
        xmpp_stanza_to_text(stanza, &msg, &msg_size);
        if (msg) {
            xmpp_debug(conn->ctx, "auth", "Handshake failed: %s", msg);
            xmpp_free(conn->ctx, msg);
        }
        xmpp_disconnect(conn);
        return XMPP_EINT;
    } else {
        conn->authenticated = 1;
        conn->conn_handler(conn, XMPP_CONN_CONNECT, 0, NULL, conn->userdata);
    }

    /* We don't need this handler anymore, return 0 so it can be deleted
     * from the list of handlers.
     */
    return 0;
}
Beispiel #3
0
/** Send an XML stanza to the XMPP server.
 *  This is the main way to send data to the XMPP server.  The function will
 *  terminate without action if the connection state is not CONNECTED.
 *
 *  @param conn a Strophe connection object
 *  @param stanza a Strophe stanza object
 *
 *  @ingroup Connections
 */
void xmpp_send(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza)
{
	char *buf;
	size_t len;

	if (conn->state == XMPP_STATE_CONNECTED &&
	    !xmpp_stanza_to_text(stanza, &buf, &len)) {
			xmpp_send_raw(conn, buf, len);
			xmpp_debug(conn->ctx, "conn", "SENT: %s", buf);
			xmpp_free(conn->ctx, buf);
	}
}
Beispiel #4
0
static void _handle_stream_stanza(xmpp_stanza_t *stanza,
                                  void * const userdata)
{
    xmpp_conn_t *conn = (xmpp_conn_t *)userdata;
    char *buf;
    size_t len;

    if (xmpp_stanza_to_text(stanza, &buf, &len) == 0) {
        xmpp_debug(conn->ctx, "xmpp", "RECV: %s", buf);
        xmpp_free(conn->ctx, buf);
    }

    handler_fire_stanza(conn, stanza);
}
Beispiel #5
0
static void
_send_presence_stanza(xmpp_stanza_t *const stanza)
{
    char *text;
    size_t text_size;
    xmpp_stanza_to_text(stanza, &text, &text_size);

    xmpp_conn_t *conn = connection_get_conn();
    char *plugin_text = plugins_on_presence_stanza_send(text);
    if (plugin_text) {
        xmpp_send_raw_string(conn, "%s", plugin_text);
        free(plugin_text);
    } else {
        xmpp_send_raw_string(conn, "%s", text);
    }
    xmpp_free(connection_get_ctx(), text);
}
Beispiel #6
0
static int
_presence_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
{
    log_debug("Presence stanza handler fired");

    char *text = NULL;
    size_t text_size;
    xmpp_stanza_to_text(stanza, &text, &text_size);

    gboolean cont = plugins_on_presence_stanza_receive(text);
    xmpp_free(connection_get_ctx(), text);
    if (!cont) {
        return 1;
    }

    const char *type = xmpp_stanza_get_type(stanza);

    if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
        _presence_error_handler(stanza);
    }

    if (g_strcmp0(type, STANZA_TYPE_UNAVAILABLE) == 0) {
        _unavailable_handler(stanza);
    }

    if (g_strcmp0(type, STANZA_TYPE_SUBSCRIBE) == 0) {
        _subscribe_handler(stanza);
    }

    if (g_strcmp0(type, STANZA_TYPE_SUBSCRIBED) == 0) {
        _subscribed_handler(stanza);
    }

    if (g_strcmp0(type, STANZA_TYPE_UNSUBSCRIBED) == 0) {
        _unsubscribed_handler(stanza);
    }

    xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
    if (mucuser) {
        _muc_user_handler(stanza);
    }

    _available_handler(stanza);

    return 1;
}
/**
 * Creates the right parameter structure from the stanza based on the method.
 */
void* _CreateParamStructureFromParameters (sdvp_method_t method,
        xmpp_stanza_t * const stanza) {
    void* rv = NULL;
    xmpp_stanza_t * params;
    params = _GetRpcParamsElement(stanza);
    char* s = NULL;
    size_t buflen;
    xmpp_stanza_to_text(params, &s, &buflen);
    syslog(LOG_DEBUG, "Param stanza: %s \n", s);
	free(s);

    switch (method) {
    case IEC_READ:
        rv = _CreateReadParam(params);
        if (rv) {
            (((sdvp_ReadParam_t*) rv))->sender = _CreateFrom(
                    xmpp_stanza_get_attribute(stanza, "from"), "TODO", "TODO");
        }
        break;
    case IEC_WRITE:
        rv = _CreateWriteParam(params);
        if (rv) {
            (((sdvp_WriteParam_t*) rv))->sender = _CreateFrom(
                    xmpp_stanza_get_attribute(stanza, "from"), "TODO", "TODO");
        }
        break;
    case IEC_GET_VARIABLE_ACCESS_ATTRIBUTE:
        rv = _CreateGetVariableAccessAttributeParam(params);
        if (rv) {
            (((sdvp_GetVariableAccessAttributeParam_t*) rv))->sender =
                    _CreateFrom(
                            xmpp_stanza_get_attribute(stanza, "from"), "TODO",
                            "TODO");
        }
        break;
    case IEC_GET_NAME_LIST:
        rv = _CreateGetNameListParam(params);
        if (rv) {
            (((sdvp_GetNameListParam_t*) rv))->sender = _CreateFrom(
                    xmpp_stanza_get_attribute(stanza, "from"), "TODO", "TODO");
        }
        break;
    case SDVP_METHOD_61850_READ:
        case SDVP_METHOD_61850_WRITE:
        case SDVP_METHOD_61850_LIST:
        case SDVP_METHOD_DIRECT_READ:
        case SDVP_METHOD_DIRECT_WRITE:
        case SDVP_METHOD_DIRECT_LIST:
        case SDVP_METHOD_LIAB_LIST:
        rv = _CreateSdvpParameters(params);
        if (rv) {
            (((sdvp_params_t*) rv))->from = _CreateFrom(
                    xmpp_stanza_get_attribute(stanza, "from"), "TODO", "TODO");
        }

        break;
    case SDVP_METHOD_UNDEFINED:
        break;
    }

    return rv;
}
Beispiel #8
0
int pubsub_handler(sox_conn_t *conn, sox_stanza_t *stanza,
		sox_userdata_t *userdata) {

	XML_Parser p;
	char *buf;
	size_t buflen;
	int set_state = 0;
	sox_stanza_t *actuate_stanza;
	char time_str[30];
	char *value_on = "1";
	char *value_off = "0";
  static int last_state=5;
	static int last_state_cnt=0;

	if (stanza == NULL )
		return -1;


xmpp_stanza_to_text(stanza->xmpp_stanza, &buf, &buflen);

//Creates an instance of the XML Parser to parse the event packet
	p = XML_ParserCreate(NULL );
	if (!p) {
		fprintf(stderr, "Couldn't allocate memory for XML Parser\n");
		return -1;
	}

//Sets the handlers to call when parsing the start and end of an XML element
	XML_SetElementHandler(p, startElement, endElement);
	XML_SetUserData(p, &set_state);

	if (XML_Parse(p, buf, strlen(buf), 1) == XML_STATUS_ERROR) {
		fprintf(stderr, "Parse Error at line %u:\n%s\n",
				(unsigned int) XML_GetCurrentLineNumber(p),
				XML_ErrorString(XML_GetErrorCode(p)));
		return -1;
	}

	XML_ParserFree(p);

if(last_state_cnt>0) last_state_cnt--;
if( last_state==set_state && last_state_cnt<=0)  return 1;
if(set_state==0 && last_state_cnt>0 ) set_state=last_state;

	// Check return value from XMPP parser and actuate
	if (set_state == 2) {
		sox_timestamp_create(time_str);
		actuate_stanza = sox_pubsub_item_data_new(conn);
		sox_item_transducer_value_actuate_add(actuate_stanza, NULL,
				"socket_state", a_id, value_on, value_on, time_str);
		if ((userdata->return_value = sox_item_publish_data(conn,
				actuate_stanza, actuate_event, userdata)) < 0)
			fprintf(stderr, "ERROR: Could not publish item, error code: %i\n",
					userdata->return_value);
		if(last_state!=set_state) last_state_cnt=3;
	  last_state=set_state;
		set_state = 0;
	  printf( "publishing actuate on\n");
	} else if (set_state == 1) {
		sox_timestamp_create(time_str);
		actuate_stanza = sox_pubsub_item_data_new(conn);
		sox_item_transducer_value_actuate_add(actuate_stanza, NULL,
				"socket_state", a_id, value_off, value_off, time_str);
		if ((userdata->return_value = sox_item_publish_data(conn,
				actuate_stanza, actuate_event, userdata)) < 0)
			fprintf(stderr, "ERROR: Could not publish item, error code: %i\n",
					userdata->return_value);
		if(last_state!=set_state) last_state_cnt=3;
	  last_state=set_state;
		set_state = 0;
	  printf( "publishing actuate off\n");
	}

	return 1;
}