Skip to content

Commit

Permalink
New service module
Browse files Browse the repository at this point in the history
  • Loading branch information
thehydroimpulse committed Mar 20, 2016
1 parent 33fd467 commit 98f0dc2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
49 changes: 49 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,55 @@ fn main() {
}
```

## Creating a Thrift Service

Thrust supports creating Thrift services, backed by non-blocking TCP sockets with Mio.

```thrift
namespace rust thrift;
// Start by defining a service in your Thrift file.
service Flock {
bool isLoggedIn(1: string token);
}
```

After using Thrust to generate the service in Rust, we can start using it.

```rust
extern crate thrust;
// Tangle is a futures implementation
extern crate tangle;

// The generated Rust module.
use thrift::Flock;

use thrust::{Spawner, Server};
use tangle::{Future, Async};

pub struct FlockService;

impl Flock for FlockService {
fn isLoggedIn(&mut self, token: String) -> Future<bool> {
if &*token == "123" {
Async::Ok(true)
} else {
Async::Ok(false)
}
}
}

fn main() {
// Create a Spawner to manage Mio event loops.
let mut spawner = Spawner::new(None);

Server::run(&spawner, FlockService);

spawner.join();


}
```

## License

MIT &mdash; go ham!
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::str;
use std::convert;
use std::string;

mod server;
mod parser;
mod ast;
mod generator;
Expand Down
28 changes: 28 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use runner::Runner;
use protocol::{Deserializer, ThriftDeserializer};
use binary_protocol::BinaryDeserializer;
use std::io::Cursor;

pub type Default = BinaryDeserializer<Cursor<Vec<u8>>>;

/// Manages incoming RPC requests from a Mio event loop and dispatches it
/// to a runner that will then deserialize the Thrift message and call the appropriate
/// RPC function.
///
/// The server will also manage the response coming back from the RPC method through
/// the use of futures. These will be coordinated back to Mio.
pub struct Server<R: Runner<Default>> {
de: Default,
runner: R
}

impl<R> Server<R>
where R: Runner<Default>
{
pub fn new(runner: R) -> Server<R> {
Server {
de: BinaryDeserializer::new(Cursor::new(Vec::new())),
runner: runner
}
}
}
1 change: 1 addition & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use protocol::{Serializer, ThriftSerializer, ThriftMessage, ThriftDeserializer,
Deserialize, Serialize, ThriftType, ThriftMessageType, Error};
use binary_protocol::{BinarySerializer, BinaryDeserializer};
use tangle::{Future, Async};
use server::{Server};

use pipeline::MessagePipeline;
use runner::Runner;
Expand Down
4 changes: 2 additions & 2 deletions src/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl Spawner {
}
}

pub fn (mut self) {
for handle in 0..self.handles.len() {
pub fn join(mut self) {
for handle in self.handles.into_iter() {
handle.join();
}
}
Expand Down

0 comments on commit 98f0dc2

Please sign in to comment.