blob: ae29da404ad0b45e6721751658daba8f8ceabe23 [file] [log] [blame]
Simon Hunt732bb2e2015-05-13 18:32:16 -07001/*
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
18package org.onosproject.ui.impl.topo;
19
Simon Huntff663742015-05-14 13:33:05 -070020import org.onosproject.event.ListenerRegistry;
Simon Hunt732bb2e2015-05-13 18:32:16 -070021import org.slf4j.Logger;
22
23import java.util.HashSet;
24import java.util.Set;
25
26import 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
33class ModelListenerRegistry
Simon Huntff663742015-05-14 13:33:05 -070034 extends ListenerRegistry<TopoUiEvent, TopoUiListener> {
Simon Hunt732bb2e2015-05-13 18:32:16 -070035
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 Huntff663742015-05-14 13:33:05 -070061
Simon Hunt732bb2e2015-05-13 18:32:16 -070062}