Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4865592d00fe75e652dfcdc874a90fe7e182210c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.messaging.event.res.internal;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.ConnectionListener;
import org.eclipse.osee.framework.messaging.ConnectionNode;
import org.eclipse.osee.framework.messaging.OseeMessagingListener;
import org.eclipse.osee.framework.messaging.OseeMessagingStatusCallback;
import org.eclipse.osee.framework.messaging.event.res.IFrameworkEventListener;
import org.eclipse.osee.framework.messaging.event.res.IOseeCoreModelEventService;
import org.eclipse.osee.framework.messaging.event.res.RemoteEvent;

/**
 * @author Donald G. Dunne
 */
public class OseeCoreModelEventServiceImpl implements OseeMessagingStatusCallback, IOseeCoreModelEventService {

   private final Map<IFrameworkEventListener, HashCollection<ResMessages, OseeMessagingListener>> subscriptions =
      new HashMap<IFrameworkEventListener, HashCollection<ResMessages, OseeMessagingListener>>();
   private final Map<ResMessages, Boolean> messages;
   private final ConnectionNode connectionNode;

   public OseeCoreModelEventServiceImpl(ConnectionNode connectionNode, Map<ResMessages, Boolean> messages) {
      this.connectionNode = connectionNode;
      this.messages = messages;
   }

   @Override
   public void success() {
      // do nothing
   }

   @Override
   public void fail(Throwable th) {
      OseeLog.log(Activator.class, Level.SEVERE, th);

   }

   @Override
   public void sendRemoteEvent(RemoteEvent remoteEvent) throws OseeCoreException {
      ResMessages resMessage = getResMessageType(remoteEvent);
      if (resMessage == null) {
         OseeLog.log(Activator.class, Level.INFO, null, "ResEventManager: Unhandled remote event [%s]", resMessage);
      } else if (connectionNode == null) {
         OseeLog.log(Activator.class, Level.INFO, null,
            "ResEventManager: Connection node was null - unable to send remote event [%s]", resMessage);
      } else {
         connectionNode.send(resMessage, remoteEvent, this);
      }
   }

   private ResMessages getResMessageType(RemoteEvent remoteEvent) {
      ResMessages resMessage = null;
      if (remoteEvent != null) {
         for (ResMessages allowedMessage : messages.keySet()) {
            Class<?> messageClass = allowedMessage.getSerializationClass();
            if (messageClass.isAssignableFrom(remoteEvent.getClass())) {
               resMessage = allowedMessage;
               break;
            }
         }
      }
      return resMessage;
   }

   @Override
   public void addConnectionListener(ConnectionListener connectionListener) {
      connectionNode.addConnectionListener(connectionListener);
   }

   @Override
   public void removeConnectionListener(ConnectionListener connectionListener) {
      connectionNode.removeConnectionListener(connectionListener);
   }

   @Override
   public void addFrameworkListener(IFrameworkEventListener frameworkEventListener) {
      OseeLog.log(Activator.class, Level.INFO, "Registering Client for Remote Events");

      for (Entry<ResMessages, Boolean> messageEntries : messages.entrySet()) {
         ResMessages resMessageID = messageEntries.getKey();
         boolean isVerbose = messageEntries.getValue();
         subscribe(resMessageID, resMessageID.getSerializationClass(), isVerbose, frameworkEventListener);
      }
   }

   @Override
   public void removeFrameworkListener(IFrameworkEventListener frameworkEventListener) {
      OseeLog.log(Activator.class, Level.INFO, "De-Registering Client for Remote Events");

      HashCollection<ResMessages, OseeMessagingListener> listeners = subscriptions.get(frameworkEventListener);
      if (listeners != null) {
         for (ResMessages messageID : listeners.keySet()) {
            Collection<OseeMessagingListener> listernerList = listeners.getValues(messageID);
            if (listernerList != null) {
               for (OseeMessagingListener listener : listernerList) {
                  connectionNode.unsubscribe(messageID, listener, this);
               }
            }
         }
         subscriptions.remove(frameworkEventListener);
      }
   }

   private <T extends RemoteEvent> void subscribe(ResMessages messageId, Class<T> clazz, boolean isVerbose, IFrameworkEventListener frameworkEventListener) {
      OseeMessagingListener listener = new FrameworkRelayMessagingListener<T>(clazz, frameworkEventListener, isVerbose);
      connectionNode.subscribe(messageId, listener, this);
      HashCollection<ResMessages, OseeMessagingListener> listeners = subscriptions.get(frameworkEventListener);
      if (listeners == null) {
         listeners = new HashCollection<ResMessages, OseeMessagingListener>(true, HashSet.class);
         subscriptions.put(frameworkEventListener, listeners);
      }
      listeners.put(messageId, listener);
   }
}

Back to the top