/workspaces/astro/sol3-sdk/cpp/sol3/core/demo_component.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/core/demo_component.h Source File
Space-ng SDK
demo_component.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 #pragma once
3 
7 #include "sol3/core/msg/buffer.h"
8 #include "sol3/core/msg/text.h"
10 
11 #include <algorithm>
12 #include <cstdint>
13 #include <cstring>
14 #include <string>
15 
16 namespace sol3::core {
17 
19  bool text_written = false;
20  bool buffer_written = false;
21  uint64_t text_seq = 0;
22  uint64_t buffer_seq = 0;
23 };
24 
25 // Simple demo component that echoes text and increments buffer metadata.
27  public:
33 
35  IBufferExchange& exchange,
36  uint32_t component_id,
37  size_t slot_count = 2,
38  size_t slot_size = 1024,
39  uint32_t width = 8,
40  uint32_t height = 8)
41  : exchange_(exchange),
42  text_input_(exchange, component_id),
43  buffer_input_(exchange, component_id),
44  text_output_(exchange, component_id, slot_count, slot_size),
45  buffer_output_(exchange, component_id, slot_count, slot_size),
46  width_(width),
47  height_(height) {
48  for (size_t i = 0; i < slot_count; ++i) {
49  msg::BufferInfo info;
50  info.mutate_id(buffer_output_.port().encoded());
51  info.mutate_idx(static_cast<uint32_t>(i + 1));
52  info.mutate_format(msg::BufferFormat_CPU_GENERIC);
53  info.mutate_width(width_);
54  info.mutate_height(height_);
55  info.mutate_stride_w(width_);
56  info.mutate_stride_h(height_);
57  info.mutate_size(static_cast<uint64_t>(width_) * height_);
58  info.mutate_buffer_use(msg::BufferUse_NONE);
59  buffer_pool_.push_back(makeShmemBuffer(info));
60  exchange.registerBuffer(*buffer_pool_.back());
61  }
62  last_buffer_info_ = buffer_pool_.front()->info();
63  }
64 
65  MessagePort textInputPort() const { return text_input_.port(); }
66  MessagePort bufferInputPort() const { return buffer_input_.port(); }
67  MessagePort textOutputPort() const { return text_output_.port(); }
68  MessagePort bufferOutputPort() const { return buffer_output_.port(); }
69 
71  DemoStepResult result;
72  ++step_count_;
73 
74  auto text_view = text_input_.readHead();
75  if (text_view.hasValue()) {
76  auto const& payload = text_view.payload();
77  if (payload.data()) {
78  last_text_ = payload.data()->str();
79  }
80  }
81 
82  auto buffer_view = buffer_input_.readHead();
83  if (buffer_view.hasValue()) {
84  auto const& payload = buffer_view.payload();
85  if (payload.info()) {
86  last_buffer_info_ = *payload.info();
87  }
88  }
89 
90  // Always write outputs; use the most recent inputs when available.
91  text_output_.payload().data =
92  "echo: " + last_text_ + " step=" + std::to_string(step_count_);
93  result.text_seq = text_output_.write();
94  result.text_written = result.text_seq > 0;
95 
96  auto& buffer = buffer_pool_[step_count_ % buffer_pool_.size()];
97  auto* data = static_cast<uint8_t*>(buffer->mutableData());
98  if (last_buffer_info_.id() != 0 && last_buffer_info_.size() != 0) {
99  auto incoming =
100  exchange_.get(last_buffer_info_.id(), last_buffer_info_.idx());
101  if (incoming && data != nullptr) {
102  auto copy_size = std::min(buffer->size(), incoming->size());
103  std::memcpy(data, incoming->data(), copy_size);
104  }
105  } else if (data != nullptr) {
106  std::memset(data, static_cast<int>(step_count_ & 0xFF), buffer->size());
107  }
108  buffer_output_.payload().info = buffer->info();
109  result.buffer_seq = buffer_output_.write();
110  result.buffer_written = result.buffer_seq > 0;
111 
112  return result;
113  }
114 
115  private:
116  IBufferExchange& exchange_;
117  MessageInput<TextInputSpec> text_input_;
118  MessageInput<BufferInputSpec> buffer_input_;
119  MessageOutput<TextOutputSpec> text_output_;
120  MessageOutput<BufferOutputSpec> buffer_output_;
121  uint32_t width_ = 0;
122  uint32_t height_ = 0;
123  uint64_t step_count_ = 0;
124  std::string last_text_ = "";
125  msg::BufferInfo last_buffer_info_{};
126  std::vector<std::unique_ptr<IBufferMutable>> buffer_pool_;
127 };
128 
129 } // namespace sol3::core
Definition: demo_component.h:26
MessagePort textOutputPort() const
Definition: demo_component.h:67
DemoStepResult step()
Definition: demo_component.h:70
MessagePort bufferInputPort() const
Definition: demo_component.h:66
MessagePort textInputPort() const
Definition: demo_component.h:65
MessagePort bufferOutputPort() const
Definition: demo_component.h:68
DemoComponent(IBufferExchange &exchange, uint32_t component_id, size_t slot_count=2, size_t slot_size=1024, uint32_t width=8, uint32_t height=8)
Definition: demo_component.h:34
Interface for registering, discovering, and disposing shared buffers.
Definition: shmem_buffer.h:112
virtual void registerBuffer(IBufferMutable const &buffer)=0
virtual std::shared_ptr< IBufferConst > get(uint32_t id, uint32_t idx) const =0
Definition: message_input.h:23
Definition: message_output.h:20
Definition: message_port.h:19
Definition: any_message_input.h:15
std::unique_ptr< IBufferMutable > makeShmemBuffer(BufferInfo const &info)
Creates a mutable buffer, matching the BufferInfo.
Definition: demo_component.h:18
bool buffer_written
Definition: demo_component.h:20
uint64_t text_seq
Definition: demo_component.h:21
bool text_written
Definition: demo_component.h:19
uint64_t buffer_seq
Definition: demo_component.h:22
Compile time message type and id specification.
Definition: message_spec.h:15