Browse Source

initial server commit, tcp connection established, no protocol defined

master
Michael Preisach 7 years ago
commit
6ddb9d4355
  1. 2
      .gitignore
  2. 16
      .idea/misc.xml
  3. 8
      .idea/modules.xml
  4. 14
      .idea/noobctf-server.iml
  5. 6
      .idea/vcs.xml
  6. 30
      Cargo.lock
  7. 8
      Cargo.toml
  8. 35
      src/main.rs
  9. 48
      src/protocol.rs

2
.gitignore

@ -0,0 +1,2 @@
/target
**/*.rs.bk

16
.idea/misc.xml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="MacroExpansionManager">
<option name="directoryName" value="zuWvPryl" />
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="/usr/bin" />
<option name="version" value="2" />
</component>
</project>

8
.idea/modules.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/noobctf-server.iml" filepath="$PROJECT_DIR$/.idea/noobctf-server.iml" />
</modules>
</component>
</project>

14
.idea/noobctf-server.iml

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

30
Cargo.lock

@ -0,0 +1,30 @@
[[package]]
name = "lazy_static"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "noobctf-server"
version = "0.1.0"
dependencies = [
"rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rppal"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
"checksum rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47fa3e09d34f2992f47f3d4315f8ee8cfffc1fe1912d4d08258b66cc18dc3b00"

8
Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "noobctf-server"
version = "0.1.0"
authors = ["Michael Preisach <michael@preisach.at>"]
edition = "2018"
[dependencies]
rppal = "0.11.1"

35
src/main.rs

@ -0,0 +1,35 @@
use std::net::{TcpStream, TcpListener};
use std::io::prelude::*;
use std::io;
mod protocol;
fn handle_connection(stream: TcpStream) -> io::Result<()> {
let mut outstream = stream.try_clone()?;
let mut reader = io::BufReader::new(stream);
let mut text = String::new();
reader.read_line(&mut text)?;
println!("got '{}'", text.trim_right());
outstream.write_all("hello from the server as well!".as_bytes())?;
Ok(())
}
fn main() {
let address = "127.0.0.1:8000";
let listener = TcpListener::bind(address).expect("could not start server");
// accept connections and get a TcpStream
for connection in listener.incoming() {
match connection {
Ok(stream) => {
stream.set_nonblocking(false);
if let Err(e) = handle_connection(stream) {
println!("error {:?}", e);
}
}
Err(e) => { println!("connection failed {}\n", e); }
}
}
}

48
src/protocol.rs

@ -0,0 +1,48 @@
pub struct Protocol {
segments: Vec<bool>,
}
impl Protocol {
pub fn new() -> Protocol {
Protocol {
segments: [false, false, false, false, false, false, false, false].to_vec(),
}
}
pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> {
if index < 8 && index >= 0 {
Ok(self.segments[index])
} else {
Err("get_segment: Index out of bounds")
}
}
pub fn move_state(&mut self, input : &str) -> Result<&'static str, &'static str> {
let arguments:Vec<&str> = input.split_whitespace().collect();
if arguments.len() < 1 {
return Err("No argument found!");
}
match arguments[0] {
"setsegment" => {
let index = arguments[1].parse::<usize>().unwrap();
let value = match arguments[2] {
"on" => true,
"off" => false,
_ => self.segments[index],
};
}
"display" => {
//show the saved state on the display
println!("Showing state on display");
}
"exit" => {
//close connection
println!("Closing connection.");
}
"shutdown" => {
//exit program
println!("Closing connection and shut down server");
}
}
}
}
Loading…
Cancel
Save