Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
Added subscriber bindings start
Browse files Browse the repository at this point in the history
  • Loading branch information
Brord van Wierst authored and semenov-vladyslav committed Sep 18, 2020
1 parent 5fbd266 commit 5811b8c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 5 deletions.
9 changes: 7 additions & 2 deletions c/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ char[] get_address_id_str(Address *address);
/// Announce creation of a new Channel.
Address auth_announce(Author *author);

/// Subscribe a new subscriber.
void auth_unwrap_subscribe(Author * author, Message * preparsed);

/// Create a new keyload for a list of subscribers.
MessageLinks auth_share_keyload(Author *author , Address *link_to, PskIds *psk_ids , PubKeyWrap[] ke_pks);

Expand All @@ -68,6 +71,10 @@ Message get_transaction(Address *link_to);

Message auth_fetch_next_transaction(Author *author);

Subscriber sub_new(char seed[], char encoding[], size_t payload_length);
void sub_unwrap_announce(Subscriber *subscriber, Message *message);
Address sub_subscribe(Subscriber *subscriber, Address *announcement_link);

/*
void auth_store_state(Author * author, PubKey * pk, Address * link);
Expand All @@ -83,8 +90,6 @@ Message [ 2 ] auth_sign_packet(Author * author , Address *link_to, Bytes * publi
/// Unwrap tagged packet.
Bytes [ 2 ] auth_unwrap_tagged_packet(Author * author , Preparsed *preparsed ) ;
/// Subscribe a new subscriber.
void auth_unwrap_subscribe(Author * author, Preparsed * preparsed);
Address auth_unwrap_sequence(Author * author, Preparsed * preparsed);
Expand Down
9 changes: 8 additions & 1 deletion c/include/streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ typedef struct NextMsgId nextmsgid_t;
typedef struct Preparsed preparsed_t;

typedef struct Address address_t;

typedef struct Message message_t;

extern char *get_address_inst_str(address_t *address);
extern char *get_address_id_str(address_t *address);
extern address_t *auth_announce(author_t *author);
extern void auth_unwrap_subscribe(author_t * author, message_t *message);

typedef struct Message message_t;
extern message_t *get_transaction(address_t *link_to);
extern message_t *auth_fetch_next_transaction(author_t *author);

Expand All @@ -43,3 +46,7 @@ extern message_links_t *auth_sign_packet(author_t *author, message_links_t *link
extern address_t *get_msg_link(message_links_t *message_links);
extern address_t *get_seq_link(message_links_t *message_links);

typedef struct Subscriber subscriber_t;
extern subscriber_t *sub_new(char seed[], char encoding[], size_t payload_length);
extern void *sub_unwrap_announce(subscriber_t *subscriber, message_t *message);
address_t *sub_subscribe(subscriber_t *subscriber, address_t *announcement_link);
23 changes: 21 additions & 2 deletions c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "include/streams.h"

int main() {
bool multi_branching = false;
bool multi_branching = true;
char seed[10] = "";

const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9";
Expand Down Expand Up @@ -39,7 +39,25 @@ int main() {
message_t *ann_packet = get_transaction(ann_link);
printf("Got the transaction\n\n");


char sub_seed_a[] = "SUBSCRIBERA9SEED";
printf("Making Sub A with %s\n", sub_seed_a);
subscriber_t *subA = sub_new("sub_seed_a", encoding, size);
printf("Made an sub A... \n");

printf("Unwrapping announcement packet... \n");
sub_unwrap_announce(subA, ann_packet);
printf("Announcement unwrapped, generating subscription message...\n");
address_t *sub_link = sub_subscribe(subA, ann_link);

printf("Subscription packet created, Fetching Transaction\n");
message_t *sub_packet = get_transaction(sub_link);

printf("Accepting Sub A to author subscription list\n");
auth_unwrap_subscribe(auth, sub_packet);


printf("Sub A subscribed!\n");
/*
printf("Sending keyload\n");
message_links_t *keyload_links = auth_share_keyload_for_everyone(auth, ann_link);
printf("Made a keyload\n\n");
Expand Down Expand Up @@ -67,6 +85,7 @@ int main() {
printf("Got the link to fetch\n");
message_t *signed_packet = get_transaction(signed_packet_link);
printf("Got the transaction\n\n");
*/

return 0;
}
15 changes: 15 additions & 0 deletions c/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ pub extern "C" fn auth_announce(author: *mut Author) -> *mut Address {
Box::into_raw(Box::new(Address(msg.link)))
}

/// unwrap and add a subscriber to the list of subscribers
#[no_mangle]
pub extern "C" fn auth_unwrap_subscribe(author: *mut Author, message: *mut TangleMessage){
unsafe {
let mut auth = Box::from_raw(author);
let msg = Box::from_raw(message);

let parsed = msg.parse_header();
auth.auth.unwrap_subscribe(parsed.unwrap()).unwrap();

mem::forget(auth);
mem::forget(msg);
}
}

/// Create a new keyload for a list of subscribers.
#[no_mangle]
pub extern "C" fn auth_share_keyload(author: *mut Author, link_to: *mut Address, psk_ids: *mut PskIds, ke_pks: *mut KePks) -> *mut MessageLinks {
Expand Down
2 changes: 2 additions & 0 deletions c/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod auth;
pub mod sub;
pub mod client;
pub mod utils;

pub use auth::*;
pub use sub::*;
pub use client::*;
pub use utils::*;
72 changes: 72 additions & 0 deletions c/src/api/sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{AppInst, Address, Subscriber, Message, PskIds, KePks, MessageLinks, SeqState, Preparsed, utils, client};

use iota_streams::app_channels::api::tangle::{
Subscriber as Sub,
Message as TangleMessage,
Preparsed as PreparsedMessage,
};
use iota_streams::app::transport::{
tangle::{
TangleAddress,
AppInst as ApplicationInstance,
MsgId as MessageIdentifier,
}
};
use iota_streams::ddml::types::Bytes;

use std::mem;
use std::ffi::CStr;
use std::os::raw::{c_char, c_ulonglong};
use iota::client::Client;
use crate::constants::*;

/// Create a new subscriber
#[no_mangle]
pub extern "C" fn sub_new(seed: *const c_char , encoding: *const c_char, payload_length: *const c_ulonglong) -> *mut Subscriber {
let c_seed = unsafe {
CStr::from_ptr(seed)
};

let c_encoding = unsafe {
CStr::from_ptr(encoding)
};

Client::get();
Client::add_node(URL).unwrap();

let sub = Sub::new(c_seed.to_str().unwrap(), c_encoding.to_str().unwrap(), payload_length as usize);
Box::into_raw(Box::new(Subscriber{ sub }))
}

/// Handle Channel app instance announcement.
#[no_mangle]
pub extern "C" fn sub_unwrap_announce(subscriber: *mut Subscriber, message: *mut TangleMessage){
unsafe {
let mut sub = Box::from_raw(subscriber);
let msg = Box::from_raw(message);

let parsed = msg.parse_header();

sub.sub.unwrap_announcement(parsed.unwrap()).unwrap();
mem::forget(sub);
mem::forget(msg);
}
}

/// Subscribe to a Channel app instance.
#[no_mangle]
pub extern "C" fn sub_subscribe(subscriber: *mut Subscriber, announcement_link: *mut Address) -> *mut Address {
let mut sub = unsafe { Box::from_raw(subscriber) };
let unboxed_address = unsafe { Box::from_raw(announcement_link) };
let tangle_address = Address(
TangleAddress::new(unboxed_address.0.appinst.clone(), unboxed_address.0.msgid.clone())
);
std::mem::forget(unboxed_address);

let msg = sub.sub.subscribe(&tangle_address.0).unwrap();
mem::forget(sub);

let mut client = Client::get();
client::send_message(&mut client, &Message(msg.clone()));
Box::into_raw(Box::new(Address(msg.link)))
}
11 changes: 11 additions & 0 deletions iota-streams-app/src/message/hdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use iota_streams_ddml::{
};

use super::*;
use core::fmt;

pub const FLAG_BRANCHING_MASK: u8 = 1;

Expand Down Expand Up @@ -96,6 +97,16 @@ impl<Link: Default> Default for HDF<Link> {
}
}

impl<Link> fmt::Debug for HDF<Link>
where
Link: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{encoding: {:?}, version: {:?}, content_type: {:?}, payload_length: {:?}}}",
self.encoding, self.version, self.content_type, self.payload_length)
}
}

impl<F, Link, Store> ContentWrap<F, Store> for HDF<Link>
where
F: PRP,
Expand Down
11 changes: 11 additions & 0 deletions iota-streams-app/src/message/preparsed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;

use core::fmt;

use super::*;
use iota_streams_core::sponge::prp::PRP;
use iota_streams_ddml::command::unwrap;
Expand Down Expand Up @@ -51,3 +53,12 @@ where
}
}
}

impl<'a, F, Link> fmt::Debug for PreparsedMessage<'a, F, Link>
where
Link: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{header: {:?}, ctx: {:?}}}", self.header, "self.ctx")
}
}
9 changes: 9 additions & 0 deletions iota-streams-ddml/src/command/unwrap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use anyhow::Result;

use core::fmt;

use crate::{
io,
types::Size,
Expand Down Expand Up @@ -34,6 +36,13 @@ impl<F, IS: io::IStream> Context<F, IS> {
}
}

impl<F, IS> fmt::Debug for Context<F, IS>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{header: {:?}, ctx: {:?}}}", "self.header", "self.ctx")
}
}

impl<F, IS> Clone for Context<F, IS>
where
F: Clone,
Expand Down

0 comments on commit 5811b8c

Please sign in to comment.