Skip to main content
summaryrefslogtreecommitdiffstats
blob: 70de39a7a696301894a2401bcaa2a27948d882e5 (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
/*******************************************************************************
 * 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.ote.client.msg.core.db;

import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.osee.ote.client.msg.core.internal.MessageServiceSupport;
import org.eclipse.osee.ote.message.Message;
import org.eclipse.osee.ote.message.commands.SubscribeToMessage;
import org.eclipse.osee.ote.message.commands.UnSubscribeToMessage;
import org.eclipse.osee.ote.message.enums.DataType;
import org.eclipse.osee.ote.message.interfaces.IMsgToolServiceClient;
import org.eclipse.osee.ote.message.tool.MessageMode;
import org.eclipse.osee.ote.message.tool.SubscriptionDetails;
import org.eclipse.osee.ote.message.tool.SubscriptionKey;

/**
 * @author Ken J. Aguilar
 */
public class MessageInstance {

   private final DataType type;
   private final MessageMode mode;
   private final Message msg;
   private SubscriptionKey serverSubscriptionKey = null;
   private int refcount = 0;
   private boolean supported = true;
   private volatile boolean connected = false;
   
   public MessageInstance(Message msg, MessageMode mode, DataType type) {
      this.msg = msg;
      this.mode = mode;
      this.type = type;
   }

   public Message getMessage() {
      return msg;
   }

   public SubscriptionKey getServerSubscriptionKey() {
      return serverSubscriptionKey;
   }

   public void setServerSubscriptionKey(SubscriptionKey serverSubscriptionKey) {
      this.serverSubscriptionKey = serverSubscriptionKey;
   }

   public boolean isAttached() {
      return serverSubscriptionKey != null;
   }

   public Integer attachToService(IMsgToolServiceClient client) throws Exception {
      InetSocketAddress address = client.getAddressByType(msg.getClass().getName(), type);
      SubscriptionDetails details;
      if(address == null){
         details = null;
      } else {
         details = MessageServiceSupport.subscribeToMessage(new SubscribeToMessage(msg.getClass().getName(), type, mode,
               client.getAddressByType(msg.getClass().getName(), type), client.getTestSessionKey()));
      }
      if (details == null) {
         supported = false;
         return null;
      }
      supported = true;
      msg.setData(details.getCurrentData());
      connected = true;
      serverSubscriptionKey = details.getKey();
      return serverSubscriptionKey.getId();
   }

   public void detachService(IMsgToolServiceClient client) throws Exception {
      if (supported) {
         MessageServiceSupport.unsubscribeToMessage(new UnSubscribeToMessage(msg.getClass().getName(), mode, type,
            client.getAddressByType(msg.getClass().getName(), type)));
      }
      connected = false;
      serverSubscriptionKey = null;
   }

   public Integer getId() {
      return serverSubscriptionKey != null ? serverSubscriptionKey.getId() : null;
   }

   public void incrementReferenceCount() {
      refcount++;
   }

   public void decrementReferenceCount() {
      refcount--;
   }

   public boolean hasReferences() {
      return refcount > 0;
   }

   public DataType getType() {
      return type;
   }

   public MessageMode getMode() {
      return mode;
   }

   public Set<DataType> getAvailableTypes() {
	  HashSet<DataType> set = new HashSet<DataType>();
	  if(connected){
	     Set<? extends DataType> envSet = MessageServiceSupport.getAvailablePhysicalTypes();
	     Set<DataType> available = msg.getAssociatedMessages().keySet();
	     for(DataType type : available.toArray(new DataType[available.size()])){
	        if(envSet.contains(type)){
	           set.add(type);
	        }
	     }
	  }
     return set;
   }

   public boolean isSupported() {
      return supported;
   }

   @Override
   public String toString() {
      return String.format("Message Instance(type=%s, mode=%s, ref=%d, supported=%b)", type.name(), mode.name(),
         refcount, supported);
   }
}

Back to the top