Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 20 | import org.onosproject.event.ListenerRegistry; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | /** |
| 29 | * A listener registry that automatically prunes listeners that have not |
| 30 | * been sending heartbeat messages to assure us they are really listening. |
| 31 | */ |
| 32 | // package private |
| 33 | class ModelListenerRegistry |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 34 | extends ListenerRegistry<TopoUiEvent, TopoUiListener> { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 35 | |
| 36 | private final Logger log = getLogger(getClass()); |
| 37 | |
| 38 | private final Set<TopoUiListener> zombies = new HashSet<>(); |
| 39 | |
| 40 | @Override |
| 41 | public void process(TopoUiEvent event) { |
| 42 | zombies.clear(); |
| 43 | for (TopoUiListener listener : listeners) { |
| 44 | try { |
| 45 | if (listener.isAwake()) { |
| 46 | listener.event(event); |
| 47 | } else { |
| 48 | zombies.add(listener); |
| 49 | } |
| 50 | } catch (Exception error) { |
| 51 | reportProblem(event, error); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // clean up zombie listeners |
| 56 | for (TopoUiListener z : zombies) { |
| 57 | log.debug("Removing zombie model listener: {}", z); |
| 58 | removeListener(z); |
| 59 | } |
| 60 | } |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | protected boolean checkForNonRegistrant() { |
| 64 | return false; |
| 65 | } |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 66 | } |