Search in sources :

Example 1 with EventDescription

use of alma.acsnc.EventDescription in project ACS by ACS-Community.

the class Consumer method push_structured_event.

/**
	 * As of ACS 3.0, override {@link #processEvent(Object)} instead of this
	 * CORBA method. push_structured_event is what is called each time an event
	 * is received. <b>Do not call it from your code!</b>
	 * 
	 * @param structuredEvent
	 *           The structured event sent by a supplier subclass.
	 * @throws org.omg.CosEventComm.Disconnected
	 */
public void push_structured_event(StructuredEvent structuredEvent) throws org.omg.CosEventComm.Disconnected {
    // time to get the event description
    final EventDescription eDescrip = EventDescriptionHelper.extract(structuredEvent.remainder_of_body);
    Object convertedAny = m_anyAide.complexAnyToObject(structuredEvent.filterable_data[0].value);
    IDLEntity struct = null;
    try {
        struct = (IDLEntity) convertedAny;
        if (isTraceEventsEnabled) {
            m_logger.log(Level.INFO, "Channel:" + m_channelName + ", Publisher:" + eDescrip.name + ", Event Type:" + structuredEvent.header.fixed_header.event_type.type_name);
        }
    } catch (ClassCastException ex) {
        if (isTraceEventsEnabled && convertedAny != null) {
            m_logger.log(Level.INFO, "Channel:" + m_channelName + ", Publisher:" + eDescrip.name + ", Event Type:" + structuredEvent.header.fixed_header.event_type.type_name + ". Failed to convert event data of type '" + convertedAny.getClass().getName() + "' which is not derived from an IDL-defined struct.");
        }
    }
    if (struct != null) {
        // process the extracted data in a separate thread
        final IDLEntity structToProcess = struct;
        // to avoid unnecessary scary logs, we tolerate previous events up to half the queue size
        boolean isReceiverBusyWithPreviousEvent = (eventHandlingExecutor.getQueue().size() > EVENT_QUEUE_CAPACITY / 2);
        //			m_logger.info("Queue size: " + eventHandlingExecutor.getQueue().size());
        boolean thisEventDiscarded = false;
        try {
            eventHandlingExecutor.execute(new Runnable() {

                public void run() {
                    // here we call processEvent from the worker thread
                    processEvent(structToProcess, eDescrip);
                }
            });
        } catch (RejectedExecutionException ex) {
            // receivers have been too slow, queue is full, will drop data.
            thisEventDiscarded = true;
            numEventsDiscarded++;
        }
        if ((thisEventDiscarded || isReceiverBusyWithPreviousEvent) && receiverTooSlowLogRepeatGuard.checkAndIncrement()) {
            LOG_NC_ReceiverTooSlow.log(m_logger, m_clientName, numEventsDiscarded, struct.getClass().getName(), m_channelName, getNotificationFactoryName());
            numEventsDiscarded = 0;
        }
    } else {
        // Should compare this with specs and C++ impl
        if (isTraceEventsEnabled) {
            m_logger.info("Will ignore event of type " + structuredEvent.header.fixed_header.event_type.type_name + " which has no data attached.");
        }
    }
}
Also used : IDLEntity(org.omg.CORBA.portable.IDLEntity) EventDescription(alma.acsnc.EventDescription) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with EventDescription

use of alma.acsnc.EventDescription in project ACS by ACS-Community.

the class NCSubscriber method push_structured_event.

////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Corba callback methods  ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
	 * This method is called by the notification channel (supplier proxy) each time an event is received.
	 * <p>
	 * It is declared <code>final</code> because it is crucial for the functioning of the NC library
	 * and thus cannot be overwritten by a subclass. 
	 * If for special purposes a notification of raw event reception is needed, 
	 * a subclass can implement {@link #push_structured_event_called(StructuredEvent)}, which gets called from this
	 * method as the first thing it does. 
	 * @param structuredEvent
	 *            The structured event sent by a supplier.
	 * @throws Disconnected If this subscriber is disconnected from the NC. 
	 *         See NC spec 3.3.7.1: "if the invocation of push_structured_event upon a StructuredPushConsumer instance 
	 *         by a StructuredProxyPushSupplier instance results in the Disconnected exception being raised, 
	 *         the StructuredProxyPushSupplier will invoke its own disconnect_structured_push_supplier operation, 
	 *         resulting in the destruction of that StructuredProxyPushSupplier instance."
	 *         This serves only as a backup mechanism, since normally we explicitly disconnect the subscriber.
	 * 
	 * @see org.omg.CosNotifyComm.StructuredPushConsumerOperations#push_structured_event(org.omg.CosNotification.StructuredEvent)
	 */
@Override
public final void push_structured_event(StructuredEvent structuredEvent) throws Disconnected {
    boolean shouldProcessEvent = true;
    numEventsReceived++;
    try {
        shouldProcessEvent = push_structured_event_called(structuredEvent);
    } catch (Throwable thr) {
    // ignore any exception, since push_structured_event_called is only meant for 
    // notification, to enable special tests or other exotic purposes.
    // In this case we also keep shouldProcessEvent=true, just in case.
    // TODO: It may be better to treat the exception like shouldProcessEvent==false
    //       since non-struct event data will cause more errors further down.
    }
    // got a subclass 'veto'?
    if (!shouldProcessEvent) {
        if (firstSubclassVeto) {
            logger.info("Event subscriber '" + getClass().getSimpleName() + "' handles one or more raw NC events itself, bypassing base class '" + NCSubscriber.class.getName() + "'. This non-standard behavior will not be logged again by this NCSubscriber.");
            firstSubclassVeto = false;
        }
        return;
    }
    if (isDisconnected()) {
        throw new Disconnected();
    }
    Object convertedAny = anyAide.complexAnyToObject(structuredEvent.filterable_data[0].value);
    if (convertedAny == null) {
        // @TODO: compare with ACS-NC specs and C++ impl, and perhaps call generic receiver with null data,
        //        if the event does not carry any data.
        LOG_NC_EventReceive_FAIL.log(logger, channelName, getNotificationFactoryName(), structuredEvent.header.fixed_header.event_type.type_name, "null");
    } else {
        // then we don't put it into the queue. We could improve this by checking for registered receivers already here...
        if (!eventType.isInstance(convertedAny) && !hasGenericReceiver()) {
            logNoEventReceiver(convertedAny.getClass().getName());
        }
        EventDescription eventDesc = EventDescriptionHelper.extract(structuredEvent.remainder_of_body);
        if (isTraceEventsEnabled()) {
            LOG_NC_EventReceive_OK.log(logger, channelName, getNotificationFactoryName(), structuredEvent.header.fixed_header.event_type.type_name);
        }
        // let the base class deal with queue and dispatching to receiver
        processEventAsync(convertedAny, eventDesc);
    }
}
Also used : EventDescription(alma.acsnc.EventDescription) Disconnected(org.omg.CosEventComm.Disconnected)

Example 3 with EventDescription

use of alma.acsnc.EventDescription in project ACS by ACS-Community.

the class SimpleSupplierConsumerClient method startReceiving.

public void startReceiving() throws Exception {
    statusBlockEvent1 event1 = new statusBlockEvent1();
    event1.counter1 = 0;
    event1.counter2 = 0;
    event1.counter3 = 0;
    event1.flipFlop = true;
    event1.myString = "myValue";
    event1.onOff = OnOffStates.ON;
    event1.period = 0.2f;
    statusBlockEvent2 event2 = new statusBlockEvent2();
    event2.counter1 = 0;
    event2.counter2 = 0;
    event2.counter3 = 0;
    event2.flipFlop = true;
    event2.myString = "myValue";
    event2.onOff = alma.ADMINTEST2.OnOffStates.ON;
    event2.period = 0.2f;
    EventDescription ed = new EventDescription();
    ed.count = 0;
    ed.name = "description";
    ed.timestamp = new Date().getTime();
    for (int i = 0; i != m_times; i++) {
        try {
            // publish events of 2 different types
            for (int j = 0; j != m_nEvents; j++) {
                m_publisher.publishEvent(event1);
                m_logger.info("Published event 1");
                m_publisher.publishEvent(event2);
                m_logger.info("Published event 2");
                m_publisher.publishEvent(ed);
                m_logger.info("Published event ed");
            }
        } catch (Exception e) {
            m_logger.info("It was impossible to publish the event because an exception was thrown");
        }
        // Sleep and get events
        try {
            Thread.sleep(m_interval * 1000);
        } catch (InterruptedException e) {
        }
    /*try {
				m_subscriber.startReceivingEvents();
			} catch(AcsJIllegalStateEventEx e) {
				m_logger.info("AcsJIllegalStateEventEx thrown, perfect :D");
			}*/
    }
}
Also used : ADMINTEST2.statusBlockEvent2(alma.ADMINTEST2.statusBlockEvent2) ADMINTEST1.statusBlockEvent1(alma.ADMINTEST1.statusBlockEvent1) EventDescription(alma.acsnc.EventDescription) Date(java.util.Date) AcsJException(alma.acs.exceptions.AcsJException)

Example 4 with EventDescription

use of alma.acsnc.EventDescription in project ACS by ACS-Community.

the class EventConsumerImpl method initialize.

/**
	 * Sets up the {@link Consumer}.
	 * <p>
	 * {@inheritDoc}
	 */
public void initialize(ContainerServices containerServices) throws ComponentLifecycleException {
    super.initialize(containerServices);
    m_logger.info("initialize() called...");
    try {
        //subscribe to real channel and name
        m_consumer = new Consumer("blar", m_containerServices);
        m_consumer.addSubscription(EventDescription.class, this);
        //			m_consumer.addSubscription(NestedFridgeEventSeqHolder.class, this);
        m_logger.info("Subscribed to 'EventDescription' " + /*and 'NestedFridgeEventSeqHolder'*/
        " types of events on channel 'blar'");
        //try to add a subscription to the same type
        try {
            m_consumer.addSubscription(EventDescription.class, this);
            throw new ComponentLifecycleException("subscription to the same type should fail!");
        } catch (ComponentLifecycleException e) {
            throw e;
        } catch (Exception e) {
            //good, expected this.
            m_logger.info("Good... attempt to subscribe twice to the same event type failed with " + e.getClass().getName());
        }
        //add a subscription to something that exists but we will never actually receive
        try {
            m_consumer.addSubscription(alma.acstime.Epoch.class, this);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Bad...cannot subscribe to multiple events types.");
        // @TODO: throw exception. Perhaps move these checks to separate functional method to be called by test client.
        }
        //add a subscription to something that exists but there is no receive method implemented for.
        try {
            m_consumer.addSubscription(org.omg.CosNotification.StructuredEvent.class, this);
            System.out.println("If you'return reading this message, addSubscription is broken!");
        // @TODO: throw exception. Perhaps move these checks to separate functional method to be called by test client.
        } catch (Exception e) {
            System.out.println("Good...cannot subscribe to events where the receive method has not been implemented");
        }
        //try to add a bad filter
        try {
            if (m_consumer.addFilter(EventDescription.class, "hope to god this filter doesn't work") != -1) {
                System.out.println("If you're reading this message, stupid API allows subscribing to bad filters!");
            }
        } catch (alma.acs.exceptions.AcsJException e) {
            System.out.println("Good...cannot add a bad filter: " + e.getMessage());
        }
        //test the helper
        if (m_consumer.getHelper() == null) {
            System.out.println("Damn helper was null!");
        }
        //test offer_change
        m_consumer.offer_change(new org.omg.CosNotification.EventType[] {}, new org.omg.CosNotification.EventType[] {});
        //test disconnect_structured_push_consumer
        m_consumer.disconnect_structured_push_consumer();
        //start receiving events
        m_consumer.consumerReady();
        //test suspend
        m_consumer.suspend();
        //test resume
        m_consumer.resume();
        m_logger.info("Waiting for events...");
    } catch (ComponentLifecycleException e) {
        throw e;
    } catch (Exception e) {
        throw new ComponentLifecycleException(e);
    }
}
Also used : Consumer(alma.acs.nc.Consumer) ComponentLifecycleException(alma.acs.component.ComponentLifecycleException) EventDescription(alma.acsnc.EventDescription) ComponentLifecycleException(alma.acs.component.ComponentLifecycleException)

Example 5 with EventDescription

use of alma.acsnc.EventDescription in project ACS by ACS-Community.

the class EventSupplierImpl method sendEvents.

/** Sends some events to an event channel.
	 * @param param number of events to send
	 */
public void sendEvents(short param) {
    m_logger.info("Now sending events via NCPublisher...");
    try {
        //first send out some number of events.
        EventDescription t_block = new EventDescription("no name", 32L, 64L);
        for (short i = 0; i < param; i++) {
            m_supplier.publishEvent(t_block);
            Thread.sleep(1);
        }
        //fake a subscription change notification (should be disabled on the proxy consumer in the real system)
        try {
            m_supplier.subscription_change(new org.omg.CosNotification.EventType[] {}, new org.omg.CosNotification.EventType[] {});
            m_logger.warning("Call to 'subscription_change' did not produce the expected NO_IMPLEMENT exception.");
        } catch (NO_IMPLEMENT ex) {
        // expected
        }
    } catch (Exception e) {
        System.err.println(e);
    }
}
Also used : NO_IMPLEMENT(org.omg.CORBA.NO_IMPLEMENT) EventDescription(alma.acsnc.EventDescription) ComponentLifecycleException(alma.acs.component.ComponentLifecycleException)

Aggregations

EventDescription (alma.acsnc.EventDescription)15 ComponentLifecycleException (alma.acs.component.ComponentLifecycleException)5 AcsJException (alma.acs.exceptions.AcsJException)3 NO_IMPLEMENT (org.omg.CORBA.NO_IMPLEMENT)3 Property (org.omg.CosNotification.Property)3 StructuredEvent (org.omg.CosNotification.StructuredEvent)3 Date (java.util.Date)2 IDLEntity (org.omg.CORBA.portable.IDLEntity)2 AcsJCORBAProblemEx (alma.ACSErrTypeCommon.wrappers.AcsJCORBAProblemEx)1 ADMINTEST1.statusBlockEvent1 (alma.ADMINTEST1.statusBlockEvent1)1 ADMINTEST2.statusBlockEvent2 (alma.ADMINTEST2.statusBlockEvent2)1 AcsLogLevel (alma.acs.logging.AcsLogLevel)1 Callback (alma.acs.nc.AcsEventSubscriber.Callback)1 GenericCallback (alma.acs.nc.AcsEventSubscriber.GenericCallback)1 Consumer (alma.acs.nc.Consumer)1 AcsJEventSubscriptionEx (alma.acsErrTypeLifeCycle.wrappers.AcsJEventSubscriptionEx)1 AcsJPublishEventFailureEx (alma.acsncErrType.wrappers.AcsJPublishEventFailureEx)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Level (java.util.logging.Level)1