Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ if(AE_BUILD_EXAMPLES)
add_subdirectory(examples/common)
add_subdirectory(examples/cloud)
add_subdirectory(examples/a_b_message_exchange)
add_subdirectory(examples/message_server)
add_subdirectory(examples/capi/oddity)
add_subdirectory(examples/benches/send_message_delays)
add_subdirectory(examples/benches/send_messages_bandwidth)
Expand Down
2 changes: 2 additions & 0 deletions aether/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ list(APPEND aether_srcs

list(APPEND aether_srcs
"server_connections/client_server_connection.cpp"
"prepared_packet/prepared_send_message.cpp"
"prepared_packet/packet_encoder.cpp"
"server_connections/channel_select_action.cpp"
"server_connections/server_connection.cpp")

Expand Down
4 changes: 4 additions & 0 deletions aether/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "aether/aether_app.h"
#include "aether/common.h"
#include "aether/config.h"
#include "aether/env.h"
#include "aether/memory.h"

#include "aether/actions/action_context.h"
Expand Down Expand Up @@ -94,6 +95,9 @@
#include "aether/modems/imodem_driver.h"
#include "aether/modems/modem_factory.h"

#include "aether/prepared_packet/packet_encoder.h"
#include "aether/prepared_packet/prepared_send_message.h"

#include "aether/aether.h"
#include "aether/channels/channel.h"
#include "aether/client.h"
Expand Down
10 changes: 7 additions & 3 deletions aether/channels/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#ifndef AETHER_CHANNELS_CHANNEL_H_
#define AETHER_CHANNELS_CHANNEL_H_

#include <optional>

#include "aether/channels/channel_statistics.h"
#include "aether/channels/channels_types.h"
#include "aether/executors/executors.h"
#include "aether/memory.h"
#include "aether/obj/obj.h"
#include "aether/executors/executors.h"
#include "aether/stream_api/istream.h"
#include "aether/channels/channels_types.h"
#include "aether/channels/channel_statistics.h"
#include "aether/types/address.h"

namespace ae {
using TransportBuildSender =
Expand All @@ -44,6 +47,7 @@ class Channel : public Obj {
*/
virtual TransportBuildSender TransportBuilder() = 0;

virtual std::optional<Endpoint> endpoint() const = 0;
ChannelTransportProperties const& transport_properties() const;
ChannelStatistics& channel_statistics();

Expand Down
2 changes: 2 additions & 0 deletions aether/channels/ethernet_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class EthernetChannel : public Channel {

AE_OBJECT_REFLECT(AE_MMBRS(aether_, poller_, dns_resolver_, address))

std::optional<Endpoint> endpoint() const override { return address; }

TransportBuildSender TransportBuilder() override;

Endpoint address;
Expand Down
2 changes: 2 additions & 0 deletions aether/channels/lora_module_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class LoraModuleChannel final : public Channel {

AE_OBJECT_REFLECT(AE_MMBRS(access_point_))

std::optional<Endpoint> endpoint() const override { return std::nullopt; }

ActionPtr<TransportBuilderAction> TransportBuilder() override;

Duration TransportBuildTimeout() const override;
Expand Down
2 changes: 2 additions & 0 deletions aether/channels/modem_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ModemChannel final : public Channel {

AE_OBJECT_REFLECT(AE_MMBRS(access_point_, address))

std::optional<Endpoint> endpoint() const override { return address; }

TransportBuildSender TransportBuilder() override;

Duration TransportBuildTimeout() const override;
Expand Down
2 changes: 2 additions & 0 deletions aether/channels/wifi_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class WifiChannel final : public Channel {
AE_OBJECT_REFLECT(AE_MMBRS(aether_, poller_, resolver_, access_point_,
address))

std::optional<Endpoint> endpoint() const override { return address; }

Duration TransportBuildTimeout() const override;

TransportBuildSender TransportBuilder() override;
Expand Down
2 changes: 2 additions & 0 deletions aether/client_messages/p2p_message_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "aether/cloud.h"

#include "aether/cloud_connections/cloud_request.h"
#include "aether/cloud_connections/cloud_server_connection.h"
#include "aether/cloud_connections/cloud_subscription.h"

#include "aether/client_messages/client_messages_tele.h"
Expand All @@ -48,6 +49,7 @@ class MessageSendStream final : public IStream<AeMessage, AeMessage> {
}},
request_policy_);
}

StreamInfo stream_info() const override { return stream_info_; }
OutDataEvent::Subscriber out_data_event() override { return out_data_event_; }
StreamUpdateEvent::Subscriber stream_update_event() override {
Expand Down
5 changes: 5 additions & 0 deletions aether/client_messages/p2p_message_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#ifndef AETHER_CLIENT_MESSAGES_P2P_MESSAGE_STREAM_H_
#define AETHER_CLIENT_MESSAGES_P2P_MESSAGE_STREAM_H_

#include <cstdint>
#include <optional>

#include "aether/common.h"

#include "aether/ae_context.h"
Expand All @@ -29,6 +32,8 @@
#include "aether/cloud_connections/cloud_server_connections.h"
#include "aether/connection_manager/client_cloud_manager.h"

#include "aether/prepared_packet/packet_encoder.h"

namespace ae {
class Client;
class Cloud;
Expand Down
8 changes: 8 additions & 0 deletions aether/connection_manager/client_cloud_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ GetCloudAction& ClientCloudManager::GetCloud(Uid client_uid) {
return *action;
}

Cloud::ptr ClientCloudManager::GetCachedCloud(Uid client_uid) {
auto cached = cloud_cache_.find(client_uid);
if ((cached == cloud_cache_.end()) || !cached->second.cloud.is_valid()) {
return {};
}
return cached->second.cloud;
}

void ClientCloudManager::StartListenForCloudUpdate() {
auto aether = aether_.Load();
assert(aether && "Aether must be loaded");
Expand Down
9 changes: 9 additions & 0 deletions aether/connection_manager/client_cloud_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,17 @@ class ClientCloudManager : public Obj {

CloudUpdateEvent::Subscriber cloud_update_event();

/**
* \brief Make request for cloud for client_uid.
* New request to client cloud is performed or cloud returned from cache.
*/
GetCloudAction& GetCloud(Uid client_uid);

/**
* \brief Returns cloud from cache for client_uid or empty Cloud::ptr.
*/
Cloud::ptr GetCachedCloud(Uid client_uid);

AE_OBJECT_REFLECT(AE_MMBRS(aether_, client_, cloud_cache_))

void StartListenForCloudUpdate();
Expand Down
75 changes: 75 additions & 0 deletions aether/prepared_packet/packet_encoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "aether/prepared_packet/packet_encoder.h"

#include <memory>
#include <utility>

#include "aether/crypto/ikey_provider.h"
#include "aether/crypto/sync_crypto_provider.h"

#include "aether/api_protocol/api_context.h"
#include "aether/api_protocol/sub_api.h"

#include "aether/work_cloud_api/ae_message.h"
#include "aether/work_cloud_api/work_server_api/authorized_api.h"
#include "aether/work_cloud_api/work_server_api/login_api.h"

namespace ae::prepared_packet {
namespace {

class PreparedSendMessageKeyProvider final : public ISyncKeyProvider {
public:
explicit PreparedSendMessageKeyProvider(PreparedSendMessage& block)
: block_{&block} {}

Key GetKey() const override { return block_->client_to_server_key; }

CryptoNonce const& Nonce() const override { return block_->next_nonce; }

private:
PreparedSendMessage* block_;
};

} // namespace

Result<std::size_t, EncodePacketError> EncodePacket(
PreparedSendMessageBlock& prepared_block, DataBuffer const& payload,
DataBuffer& out) {
if (!prepared_block.is_valid()) {
return Error{block_is_invalid};
}

auto send_message = prepared_block.Resolve();

if (send_message->message_left == 0) {
return Error{messages_exhausted};
}

// Match the existing ClientKeyProvider semantics:
// consume next nonce before encryption.
send_message->next_nonce.Next();
--send_message->message_left;

auto key_provider =
std::make_unique<PreparedSendMessageKeyProvider>(*send_message);
SyncEncryptProvider encrypt_provider{std::move(key_provider)};

ProtocolContext protocol_context;
LoginApi login_api{protocol_context, encrypt_provider};

auto api_context = ApiContext{login_api};

api_context->login_by_alias(
send_message->sender_ephemeral,
SubApi<AuthorizedApi>{
[&](auto& auth_api) {
auth_api->send_message(
AeMessage{send_message->destination_uid, DataBuffer{payload}});
},
});

out = std::move(api_context).Pack();

return Ok{out.size()};
}

} // namespace ae::prepared_packet
46 changes: 46 additions & 0 deletions aether/prepared_packet/packet_encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Prepared packet encoder.
*
* EncodePacket only builds Aether packet bytes and advances the reserved nonce
* range. It does not send, open sockets, resolve DNS, or know platform
* transport.
*/
#ifndef AETHER_PREPARED_PACKET_PACKET_ENCODER_H_
#define AETHER_PREPARED_PACKET_PACKET_ENCODER_H_

#include <string_view>

#include "aether-miscpp/types/result.h"

// IWYU pragma: begin_exports
#include "aether/prepared_packet/prepared_send_message.h"
#include "aether/types/data_buffer.h"
// IWYU pragma: end_exports

namespace ae::prepared_packet {

struct EncodePacketError {
int ec;
std::string_view msg;
};

static constexpr inline auto ok = EncodePacketError{0, "Ok!"};
static constexpr inline auto block_is_invalid =
EncodePacketError{1, "PreparedSendMessageBlock is invalid"};
static constexpr inline auto messages_exhausted =
EncodePacketError{2, "Reserved message count exhausted"};

/**
* \brief Encode send_message packet for PreparedSendMessageBlock
* \param block - prepared send message block; block must be valid.
* \param payload - message payload.
* \param out - output buffer where result is stored.
* \return Result with either out size or error.
*/
Result<std::size_t, EncodePacketError> EncodePacket(
PreparedSendMessageBlock& block, DataBuffer const& payload,
DataBuffer& out);

} // namespace ae::prepared_packet

#endif // AETHER_PREPARED_PACKET_PACKET_ENCODER_H_
Loading
Loading