Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event.impl; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 17 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.event.AbstractEvent; |
| 23 | import org.onosproject.event.DefaultEventSinkRegistry; |
| 24 | import org.onosproject.event.Event; |
| 25 | import org.onosproject.event.EventDeliveryService; |
| 26 | import org.onosproject.event.EventSink; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 28 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 29 | import java.util.concurrent.BlockingQueue; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 30 | import java.util.concurrent.ExecutorService; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 31 | import java.util.concurrent.LinkedBlockingQueue; |
| 32 | |
| 33 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 34 | import static org.onlab.util.Tools.groupedThreads; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Simple implementation of an event dispatching service. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 42 | public class CoreEventDispatcher extends DefaultEventSinkRegistry |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 43 | implements EventDeliveryService { |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 44 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | private final ExecutorService executor = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 48 | newSingleThreadExecutor(groupedThreads("onos/event", "dispatch-%d")); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 49 | |
| 50 | @SuppressWarnings("unchecked") |
| 51 | private static final Event KILL_PILL = new AbstractEvent(null, 0) { |
| 52 | }; |
| 53 | |
| 54 | private final BlockingQueue<Event> events = new LinkedBlockingQueue<>(); |
| 55 | |
| 56 | private volatile boolean stopped = false; |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public void post(Event event) { |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 60 | events.add(event); |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 61 | } |
| 62 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 63 | @Activate |
| 64 | public void activate() { |
| 65 | stopped = false; |
| 66 | executor.execute(new DispatchLoop()); |
| 67 | log.info("Started"); |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 68 | } |
| 69 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 70 | @Deactivate |
| 71 | public void deactivate() { |
| 72 | stopped = true; |
| 73 | post(KILL_PILL); |
| 74 | log.info("Stopped"); |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 77 | // Auxiliary event dispatching loop that feeds off the events queue. |
| 78 | private class DispatchLoop implements Runnable { |
| 79 | @Override |
| 80 | @SuppressWarnings("unchecked") |
| 81 | public void run() { |
| 82 | log.info("Dispatch loop initiated"); |
| 83 | while (!stopped) { |
| 84 | try { |
| 85 | // Fetch the next event and if it is the kill-pill, bail |
| 86 | Event event = events.take(); |
| 87 | if (event == KILL_PILL) { |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | // Locate the sink for the event class and use it to |
| 92 | // process the event |
| 93 | EventSink sink = getSink(event.getClass()); |
| 94 | if (sink != null) { |
| 95 | sink.process(event); |
| 96 | } else { |
| 97 | log.warn("No sink registered for event class {}", |
| 98 | event.getClass()); |
| 99 | } |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 100 | } catch (Exception e) { |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 101 | log.warn("Error encountered while dispatching event:", e); |
| 102 | } |
| 103 | } |
| 104 | log.info("Dispatch loop terminated"); |
| 105 | } |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 106 | } |
| 107 | |
tom | 94bb4a4 | 2014-08-27 22:12:02 -0700 | [diff] [blame] | 108 | } |