Created
November 16, 2022 04:42
-
-
Save nathansizemore/09a9c95f1e50341a04ff6756bc3b7541 to your computer and use it in GitHub Desktop.
libtabellarius function example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2022 Nathan Sizemore <[email protected]> | |
// | |
// This Source Code Form is subject to the terms of the Mozilla Public | |
// License, v. 2.0. If a copy of the MPL was not distributed with this | |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | |
extern crate libtabellarius; | |
#[macro_use] | |
extern crate log; | |
extern crate uuid; | |
use libtabellarius::{core::comm::message::Message, function}; | |
use uuid::Uuid; | |
static NAME: &'static str = "function-name"; | |
static VERSION: &'static str = "0.1.0"; | |
static DOMAIN: &'static str = "domain-name"; | |
static SERVICE: &'static str = "service-name"; | |
fn main() { | |
loop { | |
let r = function::init(NAME, VERSION, SERVICE); | |
if r.is_err() { | |
let e = r.unwrap_err(); | |
error!("{e}"); | |
return; | |
} | |
let (tx, rx) = r.unwrap(); | |
while let Ok(parcel) = rx.recv() { | |
if parcel.is_err() { | |
// The function has encountered an error. | |
// This is non-recoverable and the process must restart. | |
break; | |
} | |
let msg = parcel.unwrap(); | |
// Do stuff with msg... | |
let some_bytes = [0u8; 69]; // Serialized data (JSON, XML, etc...) | |
let reply = build_reply(&msg, &some_bytes); | |
let _ = tx.send(reply); | |
} | |
} | |
} | |
fn build_reply(orig: &Message, buf: &[u8]) -> Message { | |
let mut reply = Message::new(buf.len()); | |
reply.ids.set_id(Uuid::new_v4().as_u128()); | |
reply.ids.set_trace_id(orig.ids.trace_id()); | |
reply.ids.set_reply_to_id(orig.ids.id()); | |
reply.to.set_domain(orig.from.domain()); | |
reply.to.set_service(orig.from.service()); | |
reply.to.set_function(orig.from.function()); | |
reply.from.set_domain(DOMAIN); | |
reply.from.set_service(SERVICE); | |
reply.from.set_function(NAME); | |
reply.data.extend_from_slice(buf); | |
reply | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment