Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. public class waitset_statuscondSubscriber {
  2. // -----------------------------------------------------------------------
  3. // Public Methods
  4. // -----------------------------------------------------------------------
  5.  
  6. public static void main(String[] args) {
  7. // --- Get domain ID --- //
  8. int domainId = 0;
  9. if (args.length >= 1) {
  10. domainId = Integer.valueOf(args[0]).intValue();
  11. }
  12.  
  13. // -- Get max loop count; 0 means infinite loop --- //
  14. int sampleCount = 0;
  15. if (args.length >= 2) {
  16. sampleCount = Integer.valueOf(args[1]).intValue();
  17. }
  18.  
  19.  
  20. /* Uncomment this to turn on additional logging
  21. Logger.get_instance().set_verbosity_by_category(
  22. LogCategory.NDDS_CONFIG_LOG_CATEGORY_API,
  23. LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL);
  24. */
  25.  
  26. // --- Run --- //
  27. subscriberMain(domainId, sampleCount);
  28. }
  29.  
  30.  
  31.  
  32. // -----------------------------------------------------------------------
  33. // Private Methods
  34. // -----------------------------------------------------------------------
  35.  
  36. // --- Constructors: -----------------------------------------------------
  37.  
  38. private waitset_statuscondSubscriber() {
  39. super();
  40. }
  41.  
  42.  
  43. // -----------------------------------------------------------------------
  44.  
  45. private static void subscriberMain(int domainId, int sampleCount) {
  46.  
  47. DomainParticipant participant = null;
  48. Subscriber subscriber = null;
  49. Topic topic = null;
  50. waitset_statuscondDataReader reader = null;
  51.  
  52. try {
  53.  
  54. // --- Create participant --- //
  55.  
  56. /* To customize participant QoS, use
  57. the configuration file
  58. USER_QOS_PROFILES.xml */
  59.  
  60. participant = DomainParticipantFactory.TheParticipantFactory.
  61. create_participant(
  62. domainId, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
  63. null /* listener */, StatusKind.STATUS_MASK_NONE);
  64. if (participant == null) {
  65. System.err.println("create_participant error\n");
  66. return;
  67. }
  68.  
  69. // --- Create subscriber --- //
  70.  
  71. /* To customize subscriber QoS, use
  72. the configuration file USER_QOS_PROFILES.xml */
  73.  
  74. subscriber = participant.create_subscriber(
  75. DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */,
  76. StatusKind.STATUS_MASK_NONE);
  77. if (subscriber == null) {
  78. System.err.println("create_subscriber error\n");
  79. return;
  80. }
  81.  
  82. // --- Create topic --- //
  83.  
  84. /* Register type before creating topic */
  85. String typeName = waitset_statuscondTypeSupport.get_type_name();
  86. waitset_statuscondTypeSupport.register_type(participant, typeName);
  87.  
  88. /* To customize topic QoS, use
  89. the configuration file USER_QOS_PROFILES.xml */
  90.  
  91. topic = participant.create_topic(
  92. "Example waitset_statuscond",
  93. typeName, DomainParticipant.TOPIC_QOS_DEFAULT,
  94. null /* listener */, StatusKind.STATUS_MASK_NONE);
  95. if (topic == null) {
  96. System.err.println("create_topic error\n");
  97. return;
  98. }
  99.  
  100. // --- Create reader --- //
  101.  
  102. /* To customize data reader QoS, use
  103. the configuration file USER_QOS_PROFILES.xml */
  104.  
  105. reader = (waitset_statuscondDataReader)
  106. subscriber.create_datareader(
  107. topic, Subscriber.DATAREADER_QOS_DEFAULT, null,
  108. StatusKind.STATUS_MASK_NONE);
  109. if (reader == null) {
  110. System.err.println("create_datareader error\n");
  111. return;
  112. }
  113.  
  114. /* Get status conditions
  115. * ---------------------
  116. * Each entity may have an attached Status Condition. To modify the
  117. * statuses we need to get the reader's Status Conditions first.
  118. */
  119. StatusCondition status_condition = reader.get_statuscondition();
  120. if (status_condition == null) {
  121. System.err.println("get_statuscondition error\n");
  122. return;
  123. }
  124.  
  125. /* Set enabled statuses
  126. * --------------------
  127. * Now that we have the Status Condition, we are going to enable the
  128. * status we are interested in: knowing that data is available
  129. */
  130. status_condition.set_enabled_statuses(
  131. StatusKind.DATA_AVAILABLE_STATUS);
  132.  
  133. /* Create and attach conditions to the WaitSet
  134. * -------------------------------------------
  135. * Finally, we create the WaitSet and attach both the Read
  136. * Conditions and the Status Condition to it.
  137. */
  138. WaitSet waitset = new WaitSet();
  139.  
  140.  
  141. /* Attach Status Conditions */
  142. waitset.attach_condition(status_condition);
  143.  
  144. // --- Wait for data --- //
  145.  
  146. final long receivePeriodSec = 1;
  147.  
  148. for (int count = 0; (sampleCount == 0) || (count < sampleCount);
  149. ++count) {
  150. ConditionSeq active_conditions_seq =
  151. new ConditionSeq();
  152. Duration_t wait_timeout = new Duration_t();
  153. wait_timeout.sec = 1;
  154. wait_timeout.nanosec = 500000000;
  155.  
  156. try {
  157. /* wait() blocks execution of the thread until one or more
  158. * attached Conditions become true, or until a user-
  159. * specified timeout expires.
  160. */
  161. waitset.wait(active_conditions_seq, wait_timeout);
  162. /* We get to timeout if no conditions were triggered */
  163. } catch (RETCODE_TIMEOUT e) {
  164. System.out.println(
  165. "Wait timed out!! No conditions were triggered.\n");
  166. continue;
  167. }
  168.  
  169. /* Get the number of active conditions */
  170. System.out.print("Got " + active_conditions_seq.size() +
  171. " active conditions\n");
  172.  
  173. /* In this case, we have only a single condition, but
  174. if we had multiple, we would need to iterate over
  175. them and check which one is true. Leaving the logic
  176. for the more complex case. */
  177. for (int i = 0; i < active_conditions_seq.size(); ++i) {
  178.  
  179.  
  180. /* Compare with Status Condition (not required as we
  181. * only have one condition, but leaving the logic for the
  182. * more complex case.) */
  183. if (active_conditions_seq.get(i) == status_condition) {
  184. /* Get the status changes so we can check which status
  185. * condition triggered. */
  186. int triggeredmask =
  187. reader.get_status_changes();
  188.  
  189. /* Data available */
  190. if ((triggeredmask & StatusKind.DATA_AVAILABLE_STATUS)
  191. != 0) {
  192. /* Current conditions match our conditions to read
  193. * data, so we can read data just like we would do
  194. * in any other example. */
  195. waitset_statuscondSeq data_seq =
  196. new waitset_statuscondSeq();
  197. SampleInfoSeq info_seq = new SampleInfoSeq();
  198.  
  199. /* Access data using read(), take(), etc. If you
  200. * fail to do this the condition will remain true,
  201. * and the WaitSet will wake up immediately -
  202. * causing high CPU usage when it does not sleep in
  203. * the loop */
  204. boolean follow = true;
  205. while (follow) {
  206. try {
  207. reader.take(
  208. data_seq, info_seq,
  209. ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
  210. SampleStateKind.ANY_SAMPLE_STATE,
  211. ViewStateKind.ANY_VIEW_STATE,
  212. InstanceStateKind.ANY_INSTANCE_STATE);
  213.  
  214. /* Print data */
  215. for (int j = 0; j < data_seq.size(); ++j) {
  216. if (!((SampleInfo)
  217. info_seq.get(j)).valid_data) {
  218. System.out.println("Got metadata");
  219. continue;
  220. }
  221. System.out.println(
  222. ((waitset_statuscond)
  223. data_seq.get(j))
  224. .toString());
  225. }
  226. } catch (RETCODE_NO_DATA noData) {
  227. /* When there isn't data, the subscriber
  228. * stop to take samples
  229. */
  230. follow = false;
  231. } finally {
  232. /* Return the loaned data */
  233. reader.return_loan(data_seq, info_seq);
  234. }
  235. }
  236. }
  237. }
  238. }
  239. try {
  240. Thread.sleep(receivePeriodSec * 1000); // in millisec
  241. } catch (InterruptedException ix) {
  242. System.err.println("INTERRUPTED");
  243. break;
  244. }
  245. }
  246. } finally {
  247.  
  248. // --- Shutdown --- //
  249.  
  250. if(participant != null) {
  251. participant.delete_contained_entities();
  252.  
  253. DomainParticipantFactory.TheParticipantFactory.
  254. delete_participant(participant);
  255. }
  256. /* RTI Connext provides the finalize_instance()
  257. method for users who want to release memory used by the
  258. participant factory singleton. Uncomment the following block of
  259. code for clean destruction of the participant factory
  260. singleton. */
  261. //DomainParticipantFactory.finalize_instance();
  262. }
  263. }
  264.  
  265. // -----------------------------------------------------------------------
  266. // Private Types
  267. // -----------------------------------------------------------------------
  268.  
  269. // =======================================================================
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement