Module proj_billing::billing [] [src]

Contains the billing protocols I have implemented.

Stuff common to all of the protocols is included in this file

Example

extern crate proj_billing;
extern crate proj_net;
extern crate proj_crypto;
extern crate sodiumoxide;

use proj_net::server::Server;
use proj_net::server;
use proj_net::client::Client;
use proj_net::client;
use proj_net::Keypair;
use proj_crypto::asymmetric::key_exchange::gen_keypair;
use proj_crypto::asymmetric::sign;
use proj_crypto::asymmetric::key_id;
use proj_crypto::asymmetric::PublicKey ;
use proj_billing::billing;
use proj_billing::billing::sign_on_meter::SignOnMeter;
use proj_billing::billing::BillingProtocol;
use proj_billing::billing::consumption::Consumption;
use std::thread;
use std::collections::HashMap;
use std::time::Duration;
 
 
fn server_thread(sign_keys: billing::Keys, exchange_keypair: Keypair, pks: HashMap<key_id::PublicKeyId, PublicKey>,
                 prices: <SignOnMeter<Server> as BillingProtocol<Server, f64>>::Prices, socket: &str) -> f64 {
    let mut listener = server::listen(socket).unwrap();
    let mut stream = server::do_key_exchange(listener.incoming().next().unwrap(), &exchange_keypair, &pks).unwrap();

    let mut server = SignOnMeter::new_server(stream, sign_keys, &prices);
     
    server.change_prices(&prices);

    server.pay_bill()
}

fn meter_thread(keys: billing::Keys, exchange_keypair: Keypair, pks: HashMap<key_id::PublicKeyId, PublicKey>,
                cons: <SignOnMeter<Client> as BillingProtocol<Client, f64>>::Consumption, socket: &str) {
    thread::sleep(Duration::from_millis(20)); // wait for the server to start

    let mut stream = client::start(socket, exchange_keypair, &pks).unwrap();
    stream.blocking_off(5);

    let ref prices = &<SignOnMeter<Client> as BillingProtocol<Client, f64>>::null_prices();

    let mut meter = SignOnMeter::new_meter(stream, prices, billing::MeterKeys::SignOnMeter(keys));

    thread::sleep(Duration::from_millis(2)); // give the server chance to send us it's new prices

    meter.consume(&cons);

    meter.send_billing_information();
}

fn main() {
    sodiumoxide::init();
    let socket_path = "127.0.0.1:1024";
    let (m_pk_s, m_sk_s) = sign::gen_keypair();
    let (s_pk_s, s_sk_s) = sign::gen_keypair();
     
    let m_keys_s = billing::Keys {
        my_sk: m_sk_s,
        their_pk: s_pk_s,
    };

    let s_keys_s = billing::Keys {
        my_sk: s_sk_s,
        their_pk: m_pk_s,
    };

    let m_keypair = gen_keypair();
    let s_keypair = gen_keypair();
    let mut pks = HashMap::new();
    pks.insert(key_id::id_of_pk(&m_keypair.0), m_keypair.0.clone());
    pks.insert(key_id::id_of_pk(&s_keypair.0), s_keypair.0.clone());
    let pks_server = pks.clone();

    let consumption = <SignOnMeter<Client> as BillingProtocol<Client, f64>>::Consumption::new(1.0, 0);

    let prices = [1.0; 24*7];

    let socket_path_clone = socket_path.clone();
    let socket_path_clone2 = socket_path.clone();
    let server_thread = thread::spawn(move || -> f64 {server_thread(s_keys_s, s_keypair, pks_server, prices, socket_path_clone)});  
    let _ = thread::spawn(move || {meter_thread(m_keys_s, m_keypair, pks, consumption, socket_path_clone2);}); 

    let ret = server_thread.join().unwrap();

    assert_eq!(ret, 1.0);
}

Modules

consumption

Consumption schemes

sign_on_meter
three_party

Structs

Keys

Cryptographic Keys

Enums

MeterKeys

Ugly hack to make new_meter have the right parameters

Traits

BillingProtocol

Functionality which all billing protocols must provide.