bool
Axis2QpidReceiver::start(
    void)
{
    if(!conf_ctx)
        return false;

    Connection connection;
    axis2_bool_t serverSide = AXIS2_TRUE;

    serverSide = axis2_amqp_util_conf_ctx_get_server_side(conf_ctx, env);

    while(true)
    {
        try
        {
            std::list<string> queueNameList;
            string qpidBrokerIP = axis2_amqp_util_conf_ctx_get_qpid_broker_ip(conf_ctx, env);
            int qpidBrokerPort = axis2_amqp_util_conf_ctx_get_qpid_broker_port(conf_ctx, env);

            /* Check if Client Side and Resolve Dynamic Queue Name */
            if(serverSide == AXIS2_TRUE) /* Server side */
            {
                std::cout << "Connecting to Qpid Broker on " << qpidBrokerIP << ":"
                    << qpidBrokerPort << " ... ";
            }

            /* Create Connection to Qpid Broker */
            connection.open(qpidBrokerIP, qpidBrokerPort);

            if(serverSide == AXIS2_TRUE) /* Server side */
            {
                /* Create queue for each service. Queue name is equal to service name */
                axis2_conf_t* conf = axis2_conf_ctx_get_conf(conf_ctx, env);
                if(!conf)
                    return false;

                axutil_hash_t* serviceMap = axis2_conf_get_all_svcs(conf, env);
                if(!serviceMap)
                    return false;

                axutil_hash_index_t* serviceHI = NULL;
                void* serviceValue = NULL;

                for(serviceHI = axutil_hash_first(serviceMap, env); serviceHI; serviceHI
                    = axutil_hash_next(env, serviceHI))
                {
                    axutil_hash_this(serviceHI, NULL, NULL, &serviceValue);

                    axis2_svc_t* service = (axis2_svc_t*)serviceValue;
                    if(!service)
                        return false;

                    axis2_char_t* serviceName = axutil_qname_get_localpart(axis2_svc_get_qname(
                        service, env), env);
                    if(!serviceName)
                        return false;

                    queueNameList.push_back(serviceName);
                }

                std::cout << "CONNECTED" << std::endl;
            }
            else /* Client side separate listener in dual-channel case */
            {
                string queueName = axis2_amqp_util_conf_ctx_get_dual_channel_queue_name(conf_ctx,
                    env);

                queueNameList.push_back(queueName);
            }

            /* Create new session */
            Session session = connection.newSession();

            /* Create Subscription manager */
            SubscriptionManager subscriptionManager(session);

            Axis2QpidReceiverListener qpidReceiverListener(env, conf_ctx);

            /* Subscribe to queues */
            while(!queueNameList.empty())
            {
                string queueName = queueNameList.front();

                session.queueDeclare(arg::queue = queueName, arg::autoDelete = true);
                session.exchangeBind(arg::exchange = AXIS2_AMQP_EXCHANGE_DIRECT, arg::queue
                    = queueName, arg::bindingKey = queueName);

                subscriptionManager.subscribe(qpidReceiverListener, queueName);

                queueNameList.pop_front();
            }

            /* Listen and Wait */
            if(serverSide == AXIS2_TRUE) /* Server side */
            {
                std::cout << "Started Axis2 AMQP Server ..." << std::endl;
            }

            subscriptionManager.run();

            return true;
        }
        catch(const std::exception& e)
        {
            connection.close();

            if(serverSide == AXIS2_TRUE) /* Server side */
            {
                std::cout << "FAILED" << std::endl;
            }

            sleep(5);
        }
    }

    connection.close();

    return false;
}
Exemple #2
0
/* This is the function where threads start running */
void *AXIS2_THREAD_FUNC
axis2_udp_receiver_thread_worker_func(
    axutil_thread_t * thd,
    void *data)
{
	const axutil_env_t *env = NULL;
	axis2_status_t status = AXIS2_FAILURE;
	axis2_conf_t *conf = NULL;
	axis2_svc_t *svc = NULL;
	axis2_udp_recv_thd_args_t *args = NULL;
	axis2_udp_response_t response;
	axutil_hash_index_t *hi = NULL;
	axutil_hash_t *ori_all_svcs = NULL, *all_svcs = NULL;
	void *val = NULL;
	
	args = (axis2_udp_recv_thd_args_t *) data;
	env = (axutil_env_t *) args->env;	

	conf = axis2_conf_ctx_get_conf(args->conf_ctx, env);
	/* Get all the service discriptions */
	ori_all_svcs = axis2_conf_get_all_svcs(conf, env);
	if (!ori_all_svcs)
	{
		return NULL;
	}
	all_svcs = axutil_hash_copy(ori_all_svcs, env);	
	if (args->is_multicast)
	{
	/* If multicast we go through every service and try to figure out weather they are 
	   accepting multicast messages. If a service accepts a multicast message we send the 
	   request to that service bypassing the normal dispatchers. Dispatchers cannot be 
	   used since no dispatching information is found in the multicast messages 
	*/
    for (hi = axutil_hash_first(all_svcs, env); hi;
         hi = axutil_hash_next(env, hi))
    {		
        axutil_hash_this(hi, NULL, NULL, &val);
        svc = (axis2_svc_t *) val;
        if (svc)
        {
			axutil_param_t *param = NULL;
			axis2_char_t *param_val = NULL;
			/* Get the Multicast accept parameter from the services.xml */
            param = axis2_svc_get_param(svc, env, AXIS2_UDP_MULTICAST_ACCEPT);
			if (!param) 
			{
				continue;
			}
			/* check weather this service accepts multicast requests */
			param_val = axutil_param_get_value(param, env);
			if (!param_val || !axutil_strcmp(param_val, "false") || axutil_strcmp(param_val, "true"))
			{
				continue;
			}
			
			response.buf_size = 0;
			response.buff = NULL;

			/* set the service to the request. This will bypass the dispatches */
			args->request.svc = svc;
			/* Process the request */
			status = axis2_udp_receiver_process_request(args->env, args->conf_ctx, 
															&args->request, &response);
			if (status == AXIS2_FAILURE)
			{
				AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error processing the request.");
				return NULL;
			}

			/* If we have a response send it */
			if (response.buff)
			{
				status = axutil_network_handler_send_dgram(env, args->send_socket, 
													response.buff, &response.buf_size, 
													args->req_addr, args->req_port, NULL);
				if (status == AXIS2_FAILURE)
				{
					AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error sending the response.");
					return NULL;
				}
			}
		}
	}
	}
	else
	{
		axis2_ctx_t *ctx = NULL;
		axutil_property_t *prop = NULL;
		axis2_udp_backchannel_info_t *binfo = NULL;

		ctx = axis2_conf_ctx_get_base(args->conf_ctx, env);
		prop = axis2_ctx_get_property(ctx, env, AXIS2_UDP_BACKCHANNEL_INFO);
		if (prop)
		{
			binfo = axutil_property_get_value(prop, env);
		}
		/* Unicast case. In this case message contains dispatching information. 
		 * So we send the request in the normal way 
		 */
		response.buf_size = 0;
		response.buff = NULL;
		if (binfo)
		{
			args->request.svc = binfo->svc;
			args->request.op = binfo->op;
		}
		
		/* Process the request */
		status = axis2_udp_receiver_process_request(args->env, args->conf_ctx, 
														&args->request, &response);
		if (status == AXIS2_FAILURE)
		{
			AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error processing the request.");
			return NULL;
		}

		/* If we have a response send it */
		if (response.buff)
		{
			status = axutil_network_handler_send_dgram(env, args->send_socket, 
												response.buff, &response.buf_size, 
												args->req_addr, args->req_port, NULL);
			if (status == AXIS2_FAILURE)
			{
				AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error sending the response.");
				return NULL;
			}
		}
	}
	return NULL;
}