Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36081202dffe52128edcd75298363a64a7f0609d (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
package org.eclipse.osee.ote.client.msg.core.internal;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.ote.OTEException;
import org.eclipse.osee.ote.core.ServiceUtility;
import org.eclipse.osee.ote.message.commands.RecordCommand;
import org.eclipse.osee.ote.message.commands.SetElementValue;
import org.eclipse.osee.ote.message.commands.SubscribeToMessage;
import org.eclipse.osee.ote.message.commands.UnSubscribeToMessage;
import org.eclipse.osee.ote.message.commands.ZeroizeElement;
import org.eclipse.osee.ote.message.enums.DataType;
import org.eclipse.osee.ote.message.event.OteEventMessageUtil;
import org.eclipse.osee.ote.message.event.send.OteSendEventMessage;
import org.eclipse.osee.ote.message.tool.SubscriptionDetails;
import org.eclipse.osee.ote.remote.messages.AVAILABLE_PHYSICAL_TYPES_REQ;
import org.eclipse.osee.ote.remote.messages.GET_INET_ADDRESS_REQ;
import org.eclipse.osee.ote.remote.messages.GET_INET_ADDRESS_RESP;
import org.eclipse.osee.ote.remote.messages.SOCKET_ID;
import org.eclipse.osee.ote.remote.messages.STOP_RECORDING_CMD;
import org.eclipse.osee.ote.remote.messages.SerializedAvailablePhysicalTypesMessage;
import org.eclipse.osee.ote.remote.messages.SerializedRecordCommandMessage;
import org.eclipse.osee.ote.remote.messages.SerializedSetElementMessage;
import org.eclipse.osee.ote.remote.messages.SerializedSubscribeToMessage;
import org.eclipse.osee.ote.remote.messages.SerializedSubscriptionDetailsMessage;
import org.eclipse.osee.ote.remote.messages.SerializedUnSubscribeMessage;
import org.eclipse.osee.ote.remote.messages.SerializedZeroizeElementMessage;
import org.osgi.service.event.EventAdmin;

public class MessageServiceSupport {

   private static EventAdmin admin;
   private static OteSendEventMessage send;
   private static Executor worker = Executors.newSingleThreadExecutor();
   
   private static EventAdmin getEventAdmin(){
      if(admin == null){
         admin = ServiceUtility.getService(EventAdmin.class);
      }
      return admin;
   }
   
   private static OteSendEventMessage get(){
      if(send == null){
         send = new OteSendEventMessage(getEventAdmin());
      }
      return send;
   }
   
   @Deprecated
   public static SubscriptionDetails subscribeToMessage(SubscribeToMessage subscribeToMessage) {
      if(Thread.currentThread().getName().contains("main")){
         System.out.println("bad");
      }
      SerializedSubscriptionDetailsMessage resp = new SerializedSubscriptionDetailsMessage();
      try{
         SerializedSubscribeToMessage cmd = new SerializedSubscribeToMessage(subscribeToMessage);
         resp = get().synchSendAndResponse(resp, cmd, 10000);
         if(resp == null){
            throw new OTEException("Timed out waiting for message response");
         } 
         SubscriptionDetails details = resp.getObject();
         return details;
      } catch (IOException ex){
         throw new OTEException("Serialization Error", ex);
      } catch (ClassNotFoundException e) {
         throw new OTEException("Serialization Error", e);
      }
   }
   
   public static void subscribeToMessage(SubscribeToMessage subscribeToMessage, SubscriptionHandler handler) {
      worker.execute(new Runnable(){
         @Override
         public void run() {
            SerializedSubscriptionDetailsMessage resp = new SerializedSubscriptionDetailsMessage();
            try{
               SerializedSubscribeToMessage cmd = new SerializedSubscribeToMessage(subscribeToMessage);
               resp = get().synchSendAndResponse(resp, cmd, 10000);
               if(resp == null){
                  throw new OTEException("Timed out waiting for message response");
               } 
               SubscriptionDetails details = resp.getObject();
               handler.onSubscriptionComplete(details);
            } catch (IOException ex){
               throw new OTEException("Serialization Error", ex);
            } catch (ClassNotFoundException e) {
               throw new OTEException("Serialization Error", e);
            }
         }
         
      });
     
   }

   public static void unsubscribeToMessage(UnSubscribeToMessage unSubscribeToMessage) {
      SerializedUnSubscribeMessage cmd;
      try {
         cmd = new SerializedUnSubscribeMessage(unSubscribeToMessage);
         OteEventMessageUtil.postEvent(cmd);
      } catch (IOException e) {
         throw new OTEException("Serialization Error", e);
      }
   }

   public static Set<? extends DataType> getAvailablePhysicalTypes() throws OTEException {
      AVAILABLE_PHYSICAL_TYPES_REQ req = new AVAILABLE_PHYSICAL_TYPES_REQ();
      try{
         SerializedAvailablePhysicalTypesMessage types = new SerializedAvailablePhysicalTypesMessage();
         types = get().synchSendAndResponse(types, req, 10000);
         if(types == null){
            throw new OTEException("Timed out waiting for message response");
         } 
         return types.getObject();
      } catch (IOException ex){
         throw new OTEException("Serialization Error", ex);
      } catch (ClassNotFoundException e) {
         throw new OTEException("Serialization Error", e);
      }
   }

   public static InetSocketAddress getMsgUpdateSocketAddress() throws OTEException {
      return getCommonAddress(SOCKET_ID.MSG_UPDATES);
   }

   public static InetSocketAddress getRecorderSocketAddress() throws OTEException {
      return getCommonAddress(SOCKET_ID.RECORDER);
   }

   private static InetSocketAddress getCommonAddress(SOCKET_ID id){
      GET_INET_ADDRESS_REQ req = new GET_INET_ADDRESS_REQ();
      req.SOCKET_ID.setValue(id);
      GET_INET_ADDRESS_RESP resp = get().synchSendAndResponse(GET_INET_ADDRESS_RESP.class, GET_INET_ADDRESS_RESP.TOPIC, req, 10000);//todo retry?
      InetSocketAddress address;
      if(resp == null){
         throw new OTEException("Timed out waiting for message response");
      } else {
         try {
            address = new InetSocketAddress(resp.ADDRESS.getAddress(), resp.ADDRESS.getPort());
         } catch (UnknownHostException e) {
            throw new OTEException(e);
         } 
      }
      return address;
   }
   
   public static void startRecording(RecordCommand cmd) {
      try {
         SerializedRecordCommandMessage msg = new SerializedRecordCommandMessage(cmd);
         OteEventMessageUtil.postEvent(msg);
      } catch (IOException e) {
         throw new OTEException("Error starting recording", e);
      }
   }

   public static void stopRecording() {
      STOP_RECORDING_CMD cmd = new STOP_RECORDING_CMD();
      OteEventMessageUtil.postEvent(cmd);
   }

   public static void setElementValue(SetElementValue cmd) {
      try {
         SerializedSetElementMessage msg = new SerializedSetElementMessage(cmd);
         OteEventMessageUtil.postEvent(msg);
      } catch (IOException e) {
         throw new OTEException(e);
      }
   }

   public static void zeroizeElement(ZeroizeElement cmd) {
      try {
         SerializedZeroizeElementMessage msg = new SerializedZeroizeElementMessage(cmd);
         OteEventMessageUtil.postEvent(msg);
      } catch (IOException e) {
         throw new OTEException(e);
      }
   }

}

Back to the top