summaryrefslogtreecommitdiffstats
blob: df320dc3af69de6ac2fc3d426ea0619b4d7f8632 (plain)
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
/*******************************************************************************
 * 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.messaging.event.skynet;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Date;
import net.jini.core.lookup.ServiceItem;
import org.eclipse.osee.framework.jini.discovery.IServiceLookupListener;
import org.eclipse.osee.framework.jini.discovery.ServiceDataStore;
import org.eclipse.osee.framework.jini.util.OseeJini;

public class Sample implements IServiceLookupListener, Serializable {
   private static final long serialVersionUID = 8195127334711471268L;

   private ISkynetEventService ses;
   private final ASkynetEventListener listener;

   public Sample() {
      super();

      listener = new EventEchoer();

      // TODO this may take a very long time ... like FOREVER ... perform timeout ~5s if it is an absolutely necessary service
      //      ServiceDataStore.getEclipseInstance(EclipseJiniClassloader.getInstance()).addListener(this, ISkynetEventService.class);
      ServiceDataStore.getNonEclipseInstance().addListener(this, ISkynetEventService.class);

      for (;;) {
         try {
            Thread.sleep(5000);
            System.out.print(new Date() + ":");
            if (ses == null) {
               System.out.println("Service not connected");
            } else {
               System.out.println("kick");
               try {
                  //                  ses.kick(new ISkynetEvent[]{new RemoteArtifactModifiedEvent(1, 2, 3, 4, "bob")}, listener);
               } catch (Exception e) {
                  e.printStackTrace();
                  ses = null;
               }
            }
         } catch (InterruptedException e) {
            break;
         }
      }
   }

   @Override
   public void serviceAdded(ServiceItem serviceItem) {
      ses = (ISkynetEventService) serviceItem.service;
      try {
         ses.register((ISkynetEventListener) OseeJini.getRemoteReference(listener));
      } catch (RemoteException e) {
         e.printStackTrace();
      }
   }

   @Override
   public void serviceChanged(ServiceItem serviceItem) {
      // like when the Entry[] changes
      System.out.println("***Service changed");
      serviceAdded(serviceItem);
   }

   @Override
   public void serviceRemoved(ServiceItem serviceItem) {
      // Ensure the lookup server didn't just lose contact
      //      try {
      //         ses.someStupidCallLikeGetId();
      //      } catch(RemoteException ex) {
      ses = null;
      //      }

   }

   public static void main(String[] args) {
      new Sample();
   }

   private class EventEchoer extends ASkynetEventListener {
      private static final long serialVersionUID = 2251382843127874925L;

      @Override
      public void onEvent(ISkynetEvent[] events) throws RemoteException {
         System.out.println("Events received at " + new Date());

         for (ISkynetEvent event : events) {
            System.out.println("\t" + event);
         }
      }
   }
}