Skip to main content
summaryrefslogtreecommitdiffstats
blob: dd6663676861c21fef332d8dbd50938c2e5af062 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.skynet.core.event;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationRequiredException;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.event.res.IFrameworkEventListener;
import org.eclipse.osee.framework.messaging.event.res.RemoteEvent;
import org.eclipse.osee.framework.messaging.event.res.msgs.RemoteBasicGuidArtifact1;
import org.eclipse.osee.framework.messaging.event.res.msgs.RemotePurgedArtifactsEvent1;
import org.eclipse.osee.framework.messaging.event.res.msgs.RemoteTransactionEvent1;
import org.eclipse.osee.framework.skynet.core.event.msgs.TransactionEvent;
import org.eclipse.osee.framework.skynet.core.event2.FrameworkEventUtil;
import org.eclipse.osee.framework.skynet.core.event2.artifact.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event2.artifact.EventModType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;

/**
 * Manages remote events from the SkynetEventService.
 * 
 * @author Donald G Dunne
 */
public class RemoteEventManager2 {
   private static final RemoteEventManager2 instance = new RemoteEventManager2();
   private final IFrameworkEventListener clientEventListener;

   private RemoteEventManager2() {
      super();
      clientEventListener = new ClientEventListener();
   }

   private static class ClientEventListener implements IFrameworkEventListener {

      private static final long serialVersionUID = 1L;

      @Override
      public void onEvent(final RemoteEvent[] events) throws RemoteException {
         Job job = new Job("Receive Event2") {

            @Override
            protected IStatus run(IProgressMonitor monitor) {

               for (RemoteEvent event : events) {

                  Sender sender = null;
                  try {
                     sender = new Sender(event.getNetworkSender());
                     // If the sender's sessionId is the same as this client, then this event was
                     // created in this client and returned by remote event manager; ignore and continue
                     if (sender.isLocal()) {
                        continue;
                     }
                  } catch (OseeAuthenticationRequiredException ex1) {
                     OseeLog.log(Activator.class, Level.SEVERE, ex1);
                     continue;
                  }

                  if (event instanceof RemoteTransactionEvent1) {
                     try {
                        RemoteTransactionEvent1 event1 = (RemoteTransactionEvent1) event;
                        TransactionEvent transEvent = FrameworkEventUtil.getTransactionEvent(event1);
                        InternalEventManager2.kickTransactionEvent(sender, transEvent);
                        // TODO process transaction event by updating artifact/relation caches
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof RemotePurgedArtifactsEvent1) {
                     try {
                        RemotePurgedArtifactsEvent1 event1 = (RemotePurgedArtifactsEvent1) event;
                        Set<EventBasicGuidArtifact> artifactChanges = new HashSet<EventBasicGuidArtifact>();
                        for (RemoteBasicGuidArtifact1 guidArt : event1.getArtifacts()) {
                           artifactChanges.add(new EventBasicGuidArtifact(EventModType.Purged,
                                 FrameworkEventUtil.getBasicGuidArtifact(guidArt)));
                        }
                        // TODO process transaction event by updating artifact/relation caches
                        InternalEventManager2.kickArtifactsPurgedEvent(sender, artifactChanges);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  }
                  //                  } else if (event instanceof NetworkArtifactChangeTypeEvent) {
                  //                     try {
                  //                        // TODO do work here to reload change type artifact if loaded
                  //                        InternalEventManager2.kickArtifactsChangeTypeEvent(sender, EventBasicGuidArtifact.get(
                  //                              EventModType.ChangeType,
                  //                              ((NetworkArtifactChangeTypeEvent) event).getDefaultBasicGuidArtifacts()),
                  //                              ((NetworkArtifactChangeTypeEvent) event).getToArtTypeGuid());
                  //                     } catch (Exception ex) {
                  //                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                  //                     }
                  //                  } else if (event instanceof NetworkArtifactPurgeEvent) {
                  //                     try {
                  //                        for (DefaultBasicGuidArtifact guidArt : ((NetworkArtifactPurgeEvent) event).getDefaultBasicGuidArtifacts()) {
                  //                           Artifact artifact = ArtifactCache.getActive(guidArt);
                  //                           if (artifact != null) {
                  //                              //This is because applications may still have a reference to the artifact
                  //                              for (RelationLink link : RelationManager.getRelationsAll(artifact.getArtId(),
                  //                                    artifact.getBranch().getId(), false)) {
                  //                                 link.internalRemoteEventDelete();
                  //                              }
                  //                              ArtifactCache.deCache(artifact);
                  //                              artifact.internalSetDeleted();
                  //                           }
                  //                        }
                  //                        InternalEventManager2.kickArtifactsPurgedEvent(sender, EventBasicGuidArtifact.get(
                  //                              EventModType.Purged, ((NetworkArtifactPurgeEvent) event).getDefaultBasicGuidArtifacts()));
                  //                     } catch (Exception ex) {
                  //                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                  //                     }
                  //                  }
               }
               return Status.OK_STATUS;
            }
         };
         job.setSystem(true);
         job.setUser(false);
         job.schedule();
      }
   };

   public static void deregisterFromRemoteEventManager() {
   }

   public static void kick(RemoteEvent remoteEvent) {
      kick(Collections.singleton(remoteEvent));
   }

   public static void kick(Collection<RemoteEvent> events) {
      kick(events.toArray(new RemoteEvent[events.size()]));
   }

   public static boolean isConnected() {
      // TODO return if connected to event service
      return true;
   }

   /**
    * InternalEventManager.enableRemoteEventLoopback will enable a testing loopback that will take the kicked remote
    * events and loop them back as if they came from an external client. It will allow for the testing of the OEM -> REM
    * -> OEM processing. In addition, this onEvent is put in a non-display thread which will test that all handling by
    * applications is properly handled by doing all processing and then kicking off display-thread when need to update
    * ui. SessionId needs to be modified so this client doesn't think the events came from itself.
    */
   public static void kick(final RemoteEvent... events) {
      if (isConnected()) {
         Job job = new Job("Send Event2") {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
               System.err.println("Do Work here");
               return Status.OK_STATUS;
            }
         };

         job.schedule();
      }

      if (InternalEventManager2.isEnableRemoteEventLoopback()) {
         OseeLog.log(Activator.class, Level.INFO, "REM2: Loopback enabled - Returning events as Remote event.");
         Thread thread = new Thread() {
            @Override
            public void run() {
               try {
                  String newSessionId = GUID.create();
                  for (RemoteEvent event : events) {
                     event.getNetworkSender().setSessionId(newSessionId);
                  }
                  instance.clientEventListener.onEvent(events);
               } catch (RemoteException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);

               }
            }
         };
         thread.start();
      }
   }

}

Back to the top