From 0984c0053a3b970b9eb8e47a1295a2e08ae6da64 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Fri, 31 Mar 2023 11:15:58 +0200 Subject: [PATCH] Add readme and sample code --- README.md | 5 +++ test/shapes/ShapeType.idl | 20 ++++++++++ test/shapes/client.rb | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 README.md create mode 100644 test/shapes/ShapeType.idl create mode 100644 test/shapes/client.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..810cc0d --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# R2DDS + +This repository contains a sample ruby script to read/write ShapeType samples to DDS. + +Using the DDS Dynamic API a Ruby language binding for DDS could be developed. A small proof of concept was created using the type specific data reader/writer but that would require C++ code generation for each new IDL defined type. diff --git a/test/shapes/ShapeType.idl b/test/shapes/ShapeType.idl new file mode 100644 index 0000000..d45d88c --- /dev/null +++ b/test/shapes/ShapeType.idl @@ -0,0 +1,20 @@ +/** + * @file test.idl + * @author + * + * @brief DDS Shapes example + * + * @copyright Copyright (c) Remedy IT Expertise BV + * Chamber of commerce Rotterdam nr.276339, The Netherlands + */ + +#pragma DCPS_DATA_TYPE "ShapeType" +#pragma DCPS_DATA_KEY "ShapeType color" + +struct ShapeType { + string color; + long x; + long y; + long shapesize; +}; + diff --git a/test/shapes/client.rb b/test/shapes/client.rb new file mode 100644 index 0000000..b1ee8be --- /dev/null +++ b/test/shapes/client.rb @@ -0,0 +1,83 @@ +#-------------------------------------------------------------------- +# +# Author: Johnny Willemsen +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the R2DDS LICENSE which is +# included with this program. +# +# Copyright (c) Remedy IT Expertise BV +#-------------------------------------------------------------------- + +require 'optparse' +require 'lib/assert.rb' +include TestUtil::Assertions + +OPTIONS = { + :dcps_debuglevel => 0, +} + +ARGV.options do |opts| + script_name = File.basename($0) + opts.banner = "Usage: ruby #{script_name} [options]" + + opts.separator "" + + opts.on("--d LVL", + "Set DCPSDebugLevel value.", + "Default: 0") { |v| OPTIONS[:dcps_debuglevel]=v } + + opts.separator "" + + opts.on("-h", "--help", + "Show this help message.") { puts opts; exit } + + opts.parse! +end + +require 'dds' + +class ShapeListener < DDS::DataReaderListener + def initialize() + end + + def on_data_available(reader) + puts "Ruby on_data_available!" + shape = ShapeType.new("ORANGE", 10, 10, 10) + reader.read (shape); + puts "Read sample #{shape.color()} #{shape.x()} #{shape.y()} #{shape.shapesize()}"; + end +end + +begin + +dfp = DDS.DomainParticipantFactory_init() +dp = dfp.create_participant() +topic = dp.create_topic() +pub = dp.create_publisher() +sub = dp.create_subscriber() +dw = pub.create_datawriter(topic) + +listener = ShapeListener.new() + +dr = sub.create_datareader(topic, listener) + +$i = 0 +$num = 10 +shape = ShapeType.new("ORANGE", 10, 10, 10) + +while $i < $num do + dw.write(shape) + shape.shapesize = $i * 10 + shape.x = $i * 10 + 10 + shape.y = $i * 10 + 10 + shape.color = "RED" + $i=$i+1 + sleep(1) +end + +pub = dp.delete_contained_entities() + +ensure + +end