OpenSplice Java FACE API  v6.x
OpenSplice Future Airborne Capability Environment (FACE) Java API
TransportServices.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 package org.vortex.FACE;
21 
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.logging.Level;
27 
28 import org.omg.dds.core.AlreadyClosedException;
29 import org.omg.dds.core.IllegalOperationException;
30 import org.omg.dds.core.NotEnabledException;
31 import org.omg.dds.core.PreconditionNotMetException;
32 import org.omg.dds.core.ServiceEnvironment;
33 
34 import FACE.CONNECTION_DIRECTION_TYPEHolder;
35 import FACE.MESSAGING_PATTERN_TYPE;
36 import FACE.RETURN_CODE_TYPE;
37 import FACE.RETURN_CODE_TYPEHolder;
38 import FACE.TRANSPORT_CONNECTION_STATUS_TYPEHolder;
39 
40 public class TransportServices {
41  private ServiceEnvironment env;
42  private Map<String, ConnectionDescription> connectionDescriptions;
43  private Map<Long, Connection<?>> connections;
44 
46  }
47 
48  public void Initialize(String configuration,
49  RETURN_CODE_TYPEHolder return_code) {
50  System.setProperty(
51  ServiceEnvironment.IMPLEMENTATION_CLASS_NAME_PROPERTY,
52  "org.opensplice.dds.core.OsplServiceEnvironment");
53  this.env = ServiceEnvironment.createInstance(TransportServices.class.getClassLoader());
54  this.connectionDescriptions = Collections
55  .synchronizedMap(new HashMap<String, ConnectionDescription>());
56  this.connections = Collections
57  .synchronizedMap(new HashMap<Long, Connection<?>>());
58  if (return_code == null) {
59  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
60  return;
61  }
62  if (configuration == null) {
63  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
64  Logger.getInstance().log("Invalid argument configuration", Level.SEVERE);
65  return;
66  }
67  File f = new File(configuration);
68  if(f.exists() && !f.isDirectory()) {
69  try {
70  ConnectionDescription cd = new ConnectionDescription(env,configuration);
71  for (ConnectionDescription connection : cd.getConfigurations()) {
72  connectionDescriptions.put(connection.getName(), connection);
73  }
74  return_code.value = FACE.RETURN_CODE_TYPE.NO_ERROR;
75  } catch (Exception e) {
76  Logger.getInstance().log(e.getMessage(), Level.SEVERE);
77  Logger.getInstance().log(e.toString(), Level.FINEST);
78  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_CONFIG;
79  }
80  } else {
81  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
82  Logger.getInstance().log("Invalid argument configuration, configuration file (" + f.getAbsolutePath() + ") does not exists", Level.SEVERE);
83  return;
84  }
85  }
86 
87 
88  public void Create_Connection(
89  /* in */String connection_name,
90  /* in */MESSAGING_PATTERN_TYPE pattern,
91  /* out */us.opengroup.FACE.LongHolder connection_id,
92  /* out */CONNECTION_DIRECTION_TYPEHolder connection_direction,
93  /* out */us.opengroup.FACE.IntHolder max_message_size,
94  /* in */long timeout,
95  /* out */RETURN_CODE_TYPEHolder return_code) {
96  if (return_code == null) {
97  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
98  return;
99  }
100  if(connection_name == null ) {
101  Logger.getInstance().log("Invalid argument connection_name", Level.SEVERE);
102  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
103  return;
104  }
105  if(connection_id == null ) {
106  Logger.getInstance().log("Invalid argument connection_id", Level.SEVERE);
107  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
108  return;
109  }
110  if (connection_direction == null) {
111  Logger.getInstance().log("Invalid argument connection_direction", Level.SEVERE);
112  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
113  return;
114  }
115  if (pattern != MESSAGING_PATTERN_TYPE.PUB_SUB) {
116  Logger.getInstance().log("Only PUB_SUB message type supported ", Level.SEVERE);
117  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_PARAM;
118  return;
119  }
120  synchronized (this.connections) {
121  ConnectionDescription description = connectionDescriptions.get(connection_name.toLowerCase());
122  if (description == null) {
123  Logger.getInstance().log("No connection found with name " + connection_name, Level.SEVERE);
124  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_CONFIG;
125  return;
126  }
127  Connection<?> connection = this.connections.get(description.getId());
128  if (connection != null) {
129  return_code.value = FACE.RETURN_CODE_TYPE.NO_ACTION;
130  connection_id.value = description.getId();
131  connection_direction.value = description.getDirection();
132  } else {
133  try {
134  connection = Connection.getConnection(description,
135  Class.forName(description.getTypeName()));
136 
137  connection_id.value = description.getId();
138  connection_direction.value = description.getDirection();
139 
140  this.connections.put(connection_id.value, connection);
141 
142  return_code.value = FACE.RETURN_CODE_TYPE.NO_ERROR;
143  } catch (ClassNotFoundException e) {
145  .log("Class '"
146  + description.getTypeName()
147  + "' for configured type_name could not be found. Please use fully qualified class name",
148  Level.SEVERE);
149  return_code.value = FACE.RETURN_CODE_TYPE.INVALID_CONFIG;
150  } catch (Exception e) {
151  Logger.getInstance().log(e.getMessage(), Level.SEVERE);
152  Logger.getInstance().log(e.toString(), Level.FINEST);
153  return_code.value = this.getFACEReturnCodeFromException(e);
154  }
155  }
156  }
157  return;
158  }
159 
189  public void Destroy_Connection(long connection_id,
190  /* out */RETURN_CODE_TYPEHolder return_code) {
191  if (return_code == null) {
192  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
193  return;
194  }
195  synchronized (this.connections) {
196  Connection<?> connection = this.connections.remove(connection_id);
197  if (connection == null) {
198  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id, Level.SEVERE);
199  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
200  return;
201  }
202  try {
203  connection.close();
204  return_code.value = RETURN_CODE_TYPE.NO_ERROR;
205  } catch (Exception e) {
206  Logger.getInstance().log(e.getMessage(), Level.SEVERE);
207  return_code.value = RETURN_CODE_TYPE.INVALID_MODE;
208  }
209  }
210  return;
211  }
212 
255  public <TYPE> void Receive_Message(long connection_id, long timeout,
256  us.opengroup.FACE.LongHolder transaction_id, Holder<TYPE> message,
257  int message_size, RETURN_CODE_TYPEHolder return_code) {
258  if (return_code == null) {
259  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
260  return;
261  }
262  if (transaction_id == null) {
263  Logger.getInstance().log("Invalid argument transaction_id", Level.SEVERE);
264  return;
265  }
266  if (message == null) {
267  Logger.getInstance().log("Invalid argument message", Level.SEVERE);
268  return;
269  }
270  synchronized (this.connections) {
271  Connection<?> connection = connections.get(connection_id);
272  if (connection == null) {
273  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id, Level.SEVERE);
274  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
275  return;
276  }
277 
278  if (connection.getDirection() == FACE.CONNECTION_DIRECTION_TYPE.SOURCE) {
279  return_code.value = RETURN_CODE_TYPE.INVALID_MODE;
280  Logger.getInstance().log("Invalid connection direction for Receive_Message function", Level.SEVERE);
281  return;
282  }
283 
284  Connection<TYPE> typedConnection = connection.cast();
285 
286  if (typedConnection == null) {
287  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
288  return;
289  }
290  DestinationConnection<TYPE> destConnection = typedConnection.asDestination();
291 
292  if (destConnection == null) {
293  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
294  return;
295  }
296  destConnection.receiveMessage(timeout, transaction_id, message,
297  message_size, return_code);
298  }
299  }
300 
351  public <TYPE> void Send_Message(long connection_id, long timeout,
352  us.opengroup.FACE.LongHolder transaction_id, Holder<TYPE> message, us.opengroup.FACE.IntHolder message_size,
353  RETURN_CODE_TYPEHolder return_code) {
354  if (return_code == null) {
355  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
356  return;
357  }
358  if (transaction_id == null) {
359  Logger.getInstance().log("Invalid argument transaction_id", Level.SEVERE);
360  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
361  return;
362  }
363  if (message == null) {
364  Logger.getInstance().log("Invalid argument message", Level.SEVERE);
365  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
366  return;
367  }
368  synchronized (this.connections) {
369  Connection<?> connection = connections.get(connection_id);
370 
371  if (connection == null) {
372  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id, Level.SEVERE);
373  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
374  return;
375  }
376 
377  if (connection.getDirection() == FACE.CONNECTION_DIRECTION_TYPE.DESTINATION) {
378  return_code.value = RETURN_CODE_TYPE.INVALID_MODE;
379  Logger.getInstance().log("Invalid connection direction for Send_Message function", Level.SEVERE);
380  return;
381  }
382 
383  Connection<TYPE> typedConnection = connection.cast();
384 
385  if (typedConnection == null) {
386  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
387  return;
388  }
389  SourceConnection<TYPE> srcConnection = typedConnection.asSource();
390 
391  if (srcConnection == null) {
392  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
393  return;
394  }
395  return_code.value = srcConnection.sendMessage(message.value, timeout,transaction_id);
396  }
397 
398  }
399 
438  public <TYPE> void Register_Callback(
439  /* in */long connection_id,
440  /* in */boolean[] waitset,
441  /* inout */Read_CallbackHolder<TYPE> data_callback,
442  /* in */int max_message_size,
443  /* out */RETURN_CODE_TYPEHolder return_code) {
444  if (return_code == null) {
445  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
446  return;
447  }
448  if (data_callback == null) {
449  Logger.getInstance().log("Invalid argument data_callback", Level.SEVERE);
450  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
451  return;
452  }
453  Connection<?> connection = connections.get(connection_id);
454 
455  if (connection == null) {
456  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id, Level.SEVERE);
457  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
458  return;
459  }
460  if (connection.getDirection() == FACE.CONNECTION_DIRECTION_TYPE.SOURCE) {
461  return_code.value = RETURN_CODE_TYPE.INVALID_MODE;
462  Logger.getInstance().log("Invalid connection direction for Register_Callback function", Level.SEVERE);
463  return;
464  }
465 
466  Connection<TYPE> typedConnection = connection.cast();
467 
468  if (typedConnection == null) {
469  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
470  return;
471  }
472  DestinationConnection<TYPE> destConnection = typedConnection
473  .asDestination();
474 
475  if (destConnection == null) {
476  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
477  return;
478  }
479  if (destConnection.getCallback() != null) {
480  return_code.value = RETURN_CODE_TYPE.NO_ACTION;
481  return;
482  }
483  return_code.value = destConnection.registerCallback(data_callback,
484  max_message_size);
485  return;
486  }
487 
488  public <TYPE> void Unregister_Callback(long connection_id,
489  RETURN_CODE_TYPEHolder return_code) {
490  if (return_code == null) {
491  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
492  return;
493  }
494  Connection<?> connection = connections.get(connection_id);
495 
496  if (connection == null) {
497  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id, Level.SEVERE);
498  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
499  return;
500  }
501 
502  if (connection.getDirection() == FACE.CONNECTION_DIRECTION_TYPE.SOURCE) {
503  return_code.value = RETURN_CODE_TYPE.INVALID_MODE;
504  Logger.getInstance().log("Invalid connection direction for Unregister_Callback function", Level.SEVERE);
505  return;
506  }
507 
508  Connection<TYPE> typedConnection = connection.cast();
509 
510  if (typedConnection == null) {
511  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
512  return;
513  }
514  DestinationConnection<TYPE> destConnection = typedConnection
515  .asDestination();
516 
517  if (destConnection == null) {
518  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
519  return;
520  }
521  return_code.value = destConnection.unregisterCallback();
522 
523  return;
524 
525  }
526 
535  /* inout */us.opengroup.FACE.StringHolder connection_name,
536  /* inout */us.opengroup.FACE.LongHolder connection_id,
537  /* out */TRANSPORT_CONNECTION_STATUS_TYPEHolder connection_status,
538  /* out */RETURN_CODE_TYPEHolder return_code) {
539  if (return_code == null) {
540  Logger.getInstance().log("Invalid argument return_code", Level.SEVERE);
541  return;
542  }
543  if (connection_name == null) {
544  Logger.getInstance().log("Invalid argument connection_name", Level.SEVERE);
545  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
546  return;
547  }
548  if (connection_id == null) {
549  Logger.getInstance().log("Invalid argument connection_id", Level.SEVERE);
550  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
551  return;
552  }
553  if (connection_status == null) {
554  Logger.getInstance().log("Invalid argument connection_status", Level.SEVERE);
555  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
556  return;
557  }
558  synchronized (this.connections) {
559  Connection<?> connection = connections.get(connection_id.value);
560 
561  if (connection == null) {
562  Logger.getInstance().log("Invalid argument connection_id could not find a connection for connection_id:" + connection_id.value, Level.SEVERE);
563  return_code.value = RETURN_CODE_TYPE.INVALID_PARAM;
564  return;
565  }
566  connection_name.value = connection.getDescription().getName();
567  connection_status.value = connection.getStatus();
568  return_code.value = RETURN_CODE_TYPE.NO_ERROR;
569  }
570  return;
571  }
572 
573  private RETURN_CODE_TYPE getFACEReturnCodeFromException(Exception e) {
574  if (e instanceof AlreadyClosedException
575  || e instanceof java.util.concurrent.TimeoutException) {
576  return RETURN_CODE_TYPE.NO_ACTION;
577  } else if (e instanceof UnsupportedOperationException) {
578  return RETURN_CODE_TYPE.NOT_AVAILABLE;
579  } else if (e instanceof IllegalArgumentException) {
580  return RETURN_CODE_TYPE.INVALID_PARAM;
581  } else if (e instanceof NotEnabledException
582  || e instanceof PreconditionNotMetException
583  || e instanceof IllegalOperationException) {
584  return RETURN_CODE_TYPE.INVALID_MODE;
585  }
586  return RETURN_CODE_TYPE.INVALID_PARAM;
587 
588  /*
589  * case DDS.RETCODE_ALREADY_DELETED.value: case
590  * DDS.RETCODE_NO_DATA.value: return RETURN_CODE_TYPE.NO_ACTION; case
591  * DDS.RETCODE_OK.value: return RETURN_CODE_TYPE.NO_ERROR; case
592  * DDS.RETCODE_UNSUPPORTED.value: return RETURN_CODE_TYPE.NOT_AVAILABLE;
593  * case DDS.RETCODE_BAD_PARAMETER.value: return
594  * RETURN_CODE_TYPE.INVALID_PARAM; case DDS.RETCODE_TIMEOUT.value:
595  * return RETURN_CODE_TYPE.TIMED_OUT; case
596  * DDS.RETCODE_NOT_ENABLED.value: case
597  * DDS.RETCODE_PRECONDITION_NOT_MET.value: case
598  * DDS.RETCODE_ILLEGAL_OPERATION.value: return
599  * RETURN_CODE_TYPE.INVALID_MODE; case DDS.RETCODE_ERROR.value: case
600  * DDS.RETCODE_OUT_OF_RESOURCES.value: case
601  * DDS.RETCODE_IMMUTABLE_POLICY.value: case
602  * DDS.RETCODE_INCONSISTENT_POLICY.value: default: return
603  * RETURN_CODE_TYPE.INVALID_CONFIG; }
604  */
605  }
606 }
void log(String message, Level level)
Definition: Logger.java:47
DestinationConnection< TYPE > asDestination()
void Create_Connection(String connection_name, MESSAGING_PATTERN_TYPE pattern, us.opengroup.FACE.LongHolder connection_id, CONNECTION_DIRECTION_TYPEHolder connection_direction, us.opengroup.FACE.IntHolder max_message_size, long timeout, RETURN_CODE_TYPEHolder return_code)
abstract CONNECTION_DIRECTION_TYPE getDirection()
RETURN_CODE_TYPE registerCallback(Read_CallbackHolder< TYPE > callback, int maxMessageSize)
static Logger getInstance()
Definition: Logger.java:43
TRANSPORT_CONNECTION_STATUS_TYPE getStatus()
Definition: Connection.java:59
static< TYPE > Connection< TYPE > getConnection(ConnectionDescription description, Class< TYPE > dataType)
SourceConnection< TYPE > asSource()
void receiveMessage(long timeout, us.opengroup.FACE.LongHolder transaction_id, Holder< TYPE > message, int message_size, RETURN_CODE_TYPEHolder return_code)
void Initialize(String configuration, RETURN_CODE_TYPEHolder return_code)
RETURN_CODE_TYPE sendMessage(TYPE instanceData, long timeout, us.opengroup.FACE.LongHolder transaction_id)
This is a typed class which will be generated by idlpp.
Definition: TS.java:25
void Destroy_Connection(long connection_id, RETURN_CODE_TYPEHolder return_code)
For DDS, TS shall keep track of the DomainParticipant and Topic variables for deletion.
void Get_Connection_Parameters(us.opengroup.FACE.StringHolder connection_name, us.opengroup.FACE.LongHolder connection_id, TRANSPORT_CONNECTION_STATUS_TYPEHolder connection_status, RETURN_CODE_TYPEHolder return_code)
HashSet< ConnectionDescription > getConfigurations()
ConnectionDescription getDescription()
Definition: Connection.java:75