Commit 77a63cce authored by decauwsemaecker.glen@gmail.com's avatar decauwsemaecker.glen@gmail.com
Browse files

update and expand URL scheme resource example

parent 32437cc4
......@@ -13,12 +13,15 @@ The code
So first of all we need to import the things, we need:
```rust
use system::scheme::Scheme;
use system::error::{Error, Result, ENOENT, EBADF, EINVAL};
extern crate syscall; // add "redox_syscall: "*" to your cargo dependencies
use syscall::scheme::SchemeMut;
use syscall::error::{Error, Result, ENOENT, EBADF, EINVAL};
use std::cmp::min;
```
We start by defining our scheme struct, which will implement the `Scheme` trait and hold the state of the scheme.
We start by defining our mutable scheme struct, which will implement the `SchemeMut` trait and hold the state of the scheme.
```rust
struct VecScheme {
......@@ -36,20 +39,20 @@ impl VecScheme {
First of all we implement, `open()`. Let it accept a reference, which will be the initial content of the vector.
Note that we do ignore `flags` and `mode`.
Note that we do ignore `flags`, `uid` and `gid`.
```rust
impl Scheme for VecScheme {
fn open(&mut self, path: &str, _flags: usize, _mode: usize) -> Result<usize> {
self.vec = path.as_bytes().to_owned();
Result::Ok(path.len())
impl SchemeMut for VecScheme {
fn open(&mut self, path: &[u8], _flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
self.vec.extend_from_slice(path);
Ok(path.len())
}
```
So, now we implement read
So, now we implement read:
```rust
fn read(&mut self, _: usize, buf: &mut [u8]) -> Result<usize> {
fn read(&mut self, _id: usize, buf: &mut [u8]) -> Result<usize> {
let res = min(buf.len(), self.vec.len());
for b in buf {
......@@ -67,7 +70,7 @@ So, now we implement read
Now, we will add `write`, which will simply push to the vector:
```rust
fn write(&mut self, _: usize, buf: &[u8]) -> Result<usize> {
fn write(&mut self, _id: usize, buf: &[u8]) -> Result<usize> {
for &i in buf {
self.vec.push(i);
}
......@@ -77,30 +80,50 @@ Now, we will add `write`, which will simply push to the vector:
}
```
Note that leaving out the missing methods results in errors, when calling them.
In both our read and write implementation we ignored the `id` parameter for simplicity sake.
Note that leaving out the missing methods results in errors, when calling them, because of its default implementation.
Last, we need the `main` function:
```rust
fn main() {
use system::scheme::Packet;
let mut scheme = VecScheme::new();
// Create the handler
let mut socket = File::create(":vec").unwrap();
loop {
let mut packet = Packet::default();
while socket.read(&mut packet).unwrap() == size_of::<Packet>() {
scheme.handle(&mut packet);
socket.write(&packet).unwrap();
}
}
use syscall::data::Packet;
use std::fs::File;
use std::io::{Read, Write};
use std::mem::size_of;
let mut scheme = VecScheme::new();
// Create the handler
let mut socket = File::create(":vec").unwrap();
loop {
let mut packet = Packet::default();
while socket.read(&mut packet).unwrap() == size_of::<Packet>() {
scheme.handle(&mut packet);
socket.write(&packet).unwrap();
}
}
}
```
Now, go ahead and test it!
How to compile and run this example
-----------------------------------
For the time being there is no easy way to compile and deploy binaries for/on the redox platform.
There is however a way and it goes as follows:
1. Add your folder (as a soft link) into your local Redox repository (e.g. schemes/);
2. Add your binary to the root Makefile (e.g. under the schemes target);
3. Compile Redox via `make`;
Note: Do **not** add your folder to the git history, this 3-step process is only meant for local testing purposes. Also make sure to name your folder and binary as specified in your binary's `cargo.toml`;
Now, go ahead and test it by running `make qemu` (or a variant)!
Exercise for the reader
Exercises for the reader
------------------------
Write a scheme that can run `Brainfuck` code.
+ Compile and run the `VecScheme` example on the Redox platform;
+ There is also a Scheme trait, which is immutable. Come up with some use cases for this one;
+ Write a scheme that can run `Brainfuck` code;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment