Skip to content

Commit

Permalink
Merge pull request #58 from rgallor/refactor/node-id
Browse files Browse the repository at this point in the history
Avoid sending the node id in the Attach rpc
  • Loading branch information
sorru94 authored Aug 27, 2024
2 parents 908e34d + 6821a5c commit 06a6c08
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 54 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,3 @@ help:
@echo ' <lang> - Build, install <lang> and all its dependencies and generate <lang> code'
@echo ' <lang>-install - Install <lang> files into the repo <lang> folder'
@echo ' <lang>-dirclean - Remove <lang> build directory'

1 change: 0 additions & 1 deletion proto/astarteplatform/msghub/message_hub_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package astarteplatform.msghub;
import "google/protobuf/empty.proto";

import "astarteplatform/msghub/astarte_message.proto";
import "astarteplatform/msghub/message_hub_error.proto";
import "astarteplatform/msghub/node.proto";
import "astarteplatform/msghub/interface.proto";

Expand Down
3 changes: 0 additions & 3 deletions proto/astarteplatform/msghub/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@

syntax = "proto3";

import "astarteplatform/msghub/interface.proto";

package astarteplatform.msghub;

/* This message defines a node to be attached to the Astarte message hub. */
message Node {
string uuid = 1; // The node identifier.
repeated string interfaces_json = 2; // Array of string representing all .json interface files of the node.
}
7 changes: 3 additions & 4 deletions python/astarteplatform/msghub/message_hub_service_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion python/astarteplatform/msghub/message_hub_service_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from google.protobuf import empty_pb2 as _empty_pb2
from astarteplatform.msghub import astarte_message_pb2 as _astarte_message_pb2
from astarteplatform.msghub import message_hub_error_pb2 as _message_hub_error_pb2
from astarteplatform.msghub import node_pb2 as _node_pb2
from astarteplatform.msghub import interface_pb2 as _interface_pb2
from google.protobuf import descriptor as _descriptor
Expand Down
7 changes: 3 additions & 4 deletions python/astarteplatform/msghub/node_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions python/astarteplatform/msghub/node_pb2.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from astarteplatform.msghub import interface_pb2 as _interface_pb2
from google.protobuf.internal import containers as _containers
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
Expand All @@ -7,9 +6,7 @@ from typing import ClassVar as _ClassVar, Iterable as _Iterable, Optional as _Op
DESCRIPTOR: _descriptor.FileDescriptor

class Node(_message.Message):
__slots__ = ("uuid", "interfaces_json")
UUID_FIELD_NUMBER: _ClassVar[int]
__slots__ = ("interfaces_json",)
INTERFACES_JSON_FIELD_NUMBER: _ClassVar[int]
uuid: str
interfaces_json: _containers.RepeatedScalarFieldContainer[str]
def __init__(self, uuid: _Optional[str] = ..., interfaces_json: _Optional[_Iterable[str]] = ...) -> None: ...
def __init__(self, interfaces_json: _Optional[_Iterable[str]] = ...) -> None: ...
23 changes: 17 additions & 6 deletions rust/astarte-message-hub-proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ This module provides access to the Astarte message hub protocol buffers through
```rust
use std::time;

use clap::Parser;

use astarte_message_hub_proto::astarte_message::Payload;
use astarte_message_hub_proto::message_hub_client::MessageHubClient;
use astarte_message_hub_proto::AstarteMessage;
use astarte_message_hub_proto::Node;
use astarte_message_hub_proto::pbjson_types::Empty;
use clap::Parser;
use tonic::metadata::MetadataValue;
use tonic::transport::channel::Endpoint;
use log::info;
use uuid::Uuid;

Expand All @@ -52,10 +53,21 @@ struct Cli {
async fn run_example_client() {
env_logger::init();
let args = Cli::parse();

let uuid = Uuid::parse_str(&args.uuid).unwrap();

let mut client = MessageHubClient::connect("http://[::1]:50051")
let channel = Endpoint::from_static("http://[::1]:50051")
.connect()
.await
.unwrap();

// adding the interceptor layer will include the Node ID inside the metadata
let mut client =
MessageHubClient::with_interceptor(channel, move |mut req: tonic::Request<()>| {
req.metadata_mut()
.insert_bin("node-id-bin", MetadataValue::from_bytes(uuid.as_ref()));
Ok(req)
});

let device_datastream_interface: &str = r#"{
"interface_name": "org.astarte-platform.rust.examples.datastream.DeviceDatastream",
Expand All @@ -71,10 +83,9 @@ async fn run_example_client() {
}
]
}"#;

let node_id = Uuid::parse_str(&args.uuid).unwrap();

let interfaces_json = vec![device_datastream_interface.to_string()];
let node = Node::new(&node_id, interfaces_json);
let node = Node::new(interfaces_json);

let mut stream = client.attach(node.clone()).await.unwrap().into_inner();

Expand Down
19 changes: 8 additions & 11 deletions rust/astarte-message-hub-proto/src/astarteplatform.msghub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ pub mod astarte_message {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AstarteUnset {}
/// This message defines a node to be attached to the Astarte message hub.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Node {
/// Array of string representing all .json interface files of the node.
#[prost(string, repeated, tag = "2")]
pub interfaces_json: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
/// This message defines a list of json interfaces to be added/removed to the Astarte message hub.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -220,17 +228,6 @@ pub struct InterfacesName {
#[prost(string, repeated, tag = "1")]
pub names: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
/// This message defines a node to be attached to the Astarte message hub.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Node {
/// The node identifier.
#[prost(string, tag = "1")]
pub uuid: ::prost::alloc::string::String,
/// Array of string representing all .json interface files of the node.
#[prost(string, repeated, tag = "2")]
pub interfaces_json: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
/// Generated client implementations.
pub mod message_hub_client {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
Expand Down
26 changes: 8 additions & 18 deletions rust/astarte-message-hub-proto/src/proto_message_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use self::{astarte_data_type::Data, astarte_message::Payload};
use serde::Serialize;
use std::fmt::{Display, Formatter};
use uuid::Uuid;

include!("astarteplatform.msghub.rs");

Expand Down Expand Up @@ -213,18 +212,13 @@ impl AstarteDataType {
}

impl Node {
/// Create a new [Node] with the given [uuid](Node::uuid) and [interfaces_json](Node::interfaces_json).
pub fn new(uuid: &Uuid, interfaces_json: Vec<String>) -> Self {
Self {
uuid: uuid.to_string(),
interfaces_json,
}
/// Create a new [Node] with the given `interfaces_json`.
pub fn new(interfaces_json: Vec<String>) -> Self {
Self { interfaces_json }
}

pub fn from_interfaces<'a, I, T>(
uuid: &Uuid,
interfaces: I,
) -> Result<Self, serde_json::error::Error>
/// Create a new [Node] from an iterator of interfaces.
pub fn from_interfaces<'a, I, T>(interfaces: I) -> Result<Self, serde_json::error::Error>
where
I: IntoIterator<Item = &'a T>,
T: ?Sized + Serialize + 'a,
Expand All @@ -234,7 +228,7 @@ impl Node {
.map(serde_json::to_string)
.collect::<Result<Vec<String>, serde_json::error::Error>>()?;

Ok(Self::new(uuid, interfaces_json))
Ok(Self::new(interfaces_json))
}
}

Expand Down Expand Up @@ -348,10 +342,9 @@ impl MessageHubError {

#[cfg(test)]
mod test {
use std::collections::HashMap;

use super::astarte_data_type_individual::IndividualData;
use super::*;
use std::collections::HashMap;

#[test]
fn test_astarte_message_data() {
Expand Down Expand Up @@ -445,8 +438,6 @@ mod test {

#[test]
fn create_node_from_interface_files() {
let uuid = Uuid::new_v4();

let device_datastream_interface = r#"{
"interface_name": "org.astarte-platform.rust.examples.datastream.DeviceDatastream",
"version_major": 0,
Expand Down Expand Up @@ -481,9 +472,8 @@ mod test {
.map(|s| s.to_string())
.to_vec();

let node = Node::new(&uuid, interfaces_json.clone());
let node = Node::new(interfaces_json.clone());

assert_eq!(node.uuid, uuid.to_string());
assert_eq!(node.interfaces_json.len(), 2);

for (interface, expected) in node.interfaces_json.iter().zip(interfaces_json.iter()) {
Expand Down
5 changes: 5 additions & 0 deletions scripts/python_codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ install_code (){
OUT_DIR=$1
INSTALL_DIR=$2

# Check if the directory exists and is not empty
if [ -d "$INSTALL_DIR"/astarteplatform/msghub ] && [ "$(ls -A "$INSTALL_DIR"/astarteplatform/msghub)" ]; then
rm -v "$INSTALL_DIR"/astarteplatform/msghub/*
fi

install -d "$INSTALL_DIR"/astarteplatform/msghub
install -m 644 "$OUT_DIR"/astarteplatform/msghub/* "$INSTALL_DIR"/astarteplatform/msghub
}
Expand Down

0 comments on commit 06a6c08

Please sign in to comment.