Module: Roby::Interface::V2::Protocol

Defined in:
lib/roby/interface/v2/protocol.rb

Overview

Protocol definition for communications in the Interface protocol

Nothing else than this and basic types is allowed

See Also:

Defined Under Namespace

Classes: Action, ActionArgument, ActionModel, DelayedArgumentFromState, Error, ExecutionException, Task, VoidClass

Constant Summary collapse

Void =
VoidClass.new.freeze

Class Method Summary collapse

Class Method Details

.add_marshaller(*classes, &block) ⇒ Object



131
132
133
# File 'lib/roby/interface/v2/protocol.rb', line 131

def self.add_marshaller(*classes, &block)
    classes.each { @marshallers[_1] = block }
end

.allow_classes(*classes) ⇒ Object



123
124
125
# File 'lib/roby/interface/v2/protocol.rb', line 123

def self.allow_classes(*classes)
    add_marshaller(*classes) { _2 }
end

.allow_objects(*objects) ⇒ Object



127
128
129
# File 'lib/roby/interface/v2/protocol.rb', line 127

def self.allow_objects(*objects)
    @allowed_objects.merge(objects)
end

.each_marshaller(&block) ⇒ Object



135
136
137
# File 'lib/roby/interface/v2/protocol.rb', line 135

def self.each_marshaller(&block)
    @marshallers.each(&block)
end

.marshal_action(channel, action) ⇒ Action

Convert a Actions::Action

Parameters:

Returns:



195
196
197
198
199
200
# File 'lib/roby/interface/v2/protocol.rb', line 195

def self.marshal_action(channel, action)
    Action.new(
        model: marshal_action_model(channel, action.model),
        arguments: channel.marshal_filter_object(action)
    )
end

.marshal_action_argument_model(channel, action_argument) ⇒ ActionArgument

Parameters:

Returns:



184
185
186
187
188
# File 'lib/roby/interface/v2/protocol.rb', line 184

def self.marshal_action_argument_model(channel, action_argument)
    Protocol::ActionArgument.new(
        **channel.marshal_filter_object(action_argument.to_h)
    )
end

.marshal_action_model(channel, action, planner_model: nil) ⇒ ActionModel

Parameters:

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/roby/interface/v2/protocol.rb', line 207

def self.marshal_action_model(channel, action, planner_model: nil)
    arguments = action.arguments.map do
        marshal_action_argument_model(channel, _1)
    end
    ActionModel.new(
        planner_name: planner_model&.name,
        name: action.name,
        doc: action.doc,
        arguments: arguments,
        advanced: action.advanced?
    )
end

.marshal_delayed_argument_from_state(_channel, delayed_arg) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/roby/interface/v2/protocol.rb', line 278

def self.marshal_delayed_argument_from_state(_channel, delayed_arg)
    object =
        case delayed_arg.__object__
        when Conf
            :Conf
        when State
            :State
        else
            delayed_arg.__object__.to_s
        end

    DelayedArgumentFromState.new(
        object: object, path: delayed_arg.__methods__
    )
end

.marshal_exception(_channel, exception) ⇒ Error

Convert a Exception

Parameters:

Returns:



251
252
253
254
255
256
257
# File 'lib/roby/interface/v2/protocol.rb', line 251

def self.marshal_exception(_channel, exception)
    message = PP.pp(exception, +"").chomp
    Error.new(
        class_name: exception.class.name, message: message,
        backtrace: exception.backtrace || []
    )
end

.marshal_execution_exception(channel, execution_exception) ⇒ Object

Convert a ExecutionException



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/roby/interface/v2/protocol.rb', line 260

def self.marshal_execution_exception(channel, execution_exception)
    exception = execution_exception.exception
    if exception.respond_to?(:failed_task) &&
       (failed_task = exception.failed_task)
        marshalled_failed_task =
            channel.marshal_filter_object(failed_task)
    end

    ExecutionException.new(
        exception: marshal_exception(channel, exception),
        failed_task: marshalled_failed_task,
        involved_tasks: channel.marshal_filter_object(
            execution_exception.each_involved_task.to_a
        )
    )
end

.marshal_task(channel, task) ⇒ ActionModel

Convert a Task

Parameters:

Returns:



225
226
227
228
229
230
231
232
233
# File 'lib/roby/interface/v2/protocol.rb', line 225

def self.marshal_task(channel, task)
    Task.new(
        id: task.droby_id.id,
        model: task.model.name,
        state: task.current_state,
        started_since: task.start_event.last&.time,
        arguments: marshal_task_arguments(channel, task.arguments)
    )
end

.marshal_task_arguments(channel, arguments) ⇒ Hash

Convert a TaskArguments

Parameters:

Returns:

  • (Hash)


240
241
242
243
244
# File 'lib/roby/interface/v2/protocol.rb', line 240

def self.marshal_task_arguments(channel, arguments)
    arguments.assigned_arguments.transform_values do
        channel.marshal_filter_object(_1)
    end
end

.register_marshallers(protocol) ⇒ Object



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
# File 'lib/roby/interface/v2/protocol.rb', line 139

def self.register_marshallers(protocol)
    protocol.allow_classes(
        Action,
        ActionArgument,
        Error,
        VoidClass,
        CommandLibrary::InterfaceCommands,
        Command,
        ExecutionException,
        Time
    )

    protocol.add_marshaller(
        Actions::Models::Action, &method(:marshal_action_model)
    )
    protocol.add_marshaller(Actions::Action, &method(:marshal_action))
    protocol.add_marshaller(Roby::Task, &method(:marshal_task))
    protocol.add_marshaller(Roby::VoidClass) { Void }
    protocol.add_marshaller(::Exception, &method(:marshal_exception))
    protocol.add_marshaller(
        Roby::ExecutionException, &method(:marshal_execution_exception)
    )
    protocol.add_marshaller(
        Roby::DelayedArgumentFromState,
        &method(:marshal_delayed_argument_from_state)
    )
end

.setup_channel(channel) ⇒ Object

Configure channel marshalling to convert Roby classes into their protocol equivalent

Parameters:



171
172
173
174
175
176
177
# File 'lib/roby/interface/v2/protocol.rb', line 171

def self.setup_channel(channel)
    each_marshaller do |klass, block|
        channel.add_marshaller(klass, &block)
    end

    channel.allow_objects(*@allowed_objects)
end

.void?(value) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/roby/interface/v2/protocol.rb', line 116

def self.void?(value)
    value.kind_of?(VoidClass)
end