Documentation

Queen

crates.io Build Status MIT licensed Released API docs

Queen 是一个支持订阅发布模式、一对一和一对多的数据总线。

安装

Cargo.toml 文件中加入

queen = "0.12"

功能特性

  • 订阅发布
  • 一对一、一对多
  • 使用 nson 作为数据格式
  • 提供若干 Hook 函数,开发者可以按需定制鉴权,ACL等功能
  • 支持消息加密
  • ... 待补充

示例

use std::thread;
use std::time::Duration;

use queen::{Socket, Node, Port};
use queen::net::NsonCodec;
use queen::dict::*;
use queen::nson::{MessageId, msg};
use queen::error::ErrorCode;

fn main() {
    let socket = Socket::new(MessageId::new(), ()).unwrap();

    // start wire 1
    let wire1 = socket.connect(msg!{}, None, None).unwrap();

    wire1.send(msg!{
        CHAN: AUTH
    }).unwrap();

    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
    if let Some(err) = ErrorCode::has_error(&ret) {
        if err != ErrorCode::OK {
            println!("wire 1 auth error: {:?}", err);
            return
        }
    }

    println!("wire 1 auth ret: {:?}", ret);

    // start port
    let mut node = Node::<NsonCodec, ()>::new(
        socket.clone(),
        1,
        vec!["127.0.0.1:8888".parse().unwrap()],
        ()
    ).unwrap();

    thread::spawn(move || {
        node.run().unwrap();
    });

    // start port
    let port = Port::<NsonCodec>::new().unwrap();

    // start wire 2
    let wire2 = port.connect("127.0.0.1:8888", msg!{}, None, None).unwrap();

    wire2.send(msg!{
        CHAN: AUTH
    }).unwrap();

    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
    if let Some(err) = ErrorCode::has_error(&ret) {
        if err != ErrorCode::OK {
            println!("wire 2 auth error: {:?}", err);
            return
        }
    }

    println!("wire 2 auth ret: {:?}", ret);

    // wire 1 attach
    wire1.send(msg!{
        CHAN: ATTACH,
        VALUE: "hello"
    }).unwrap();

    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
    if let Some(err) = ErrorCode::has_error(&ret) {
        if err != ErrorCode::OK {
            println!("wire 1 attach error: {:?}", err);
            return
        }
    }

    println!("wire 1 attach ret: {:?}", ret);

    // wire 2 send
    wire2.send(msg!{
        CHAN: "hello",
        ACK: true,
        "hello": "world"
    }).unwrap();

    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
    if let Some(err) = ErrorCode::has_error(&ret) {
        if err != ErrorCode::OK {
            println!("wire 2 send error: {:?}", err);
            return
        }
    }

    println!("wire 2 send ret: {:?}", ret);

    // wire 1 recv
    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
    if let Some(err) = ErrorCode::has_error(&ret) {
        if err != ErrorCode::OK {
            println!("wire 2 recv error: {:?}", err);
            return
        }
    }

    println!("wire 2 recv ret: {:?}", ret);
}