Переглянути джерело

Start moving to lib

master
Noelle 4 роки тому
джерело
коміт
33bbba9a91
6 змінених файлів з 2347 додано та 53 видалено
  1. 2135
    0
      GPUs.txt
  2. 5
    0
      config.xml
  3. 61
    0
      log.txt
  4. 49
    1
      src/lib.rs
  5. 97
    52
      src/main.rs
  6. BIN
      work/client.db

+ 2135
- 0
GPUs.txt
Різницю між файлами не показано, бо вона завелика
Переглянути файл


+ 5
- 0
config.xml Переглянути файл

@@ -0,0 +1,5 @@
<config>
<!-- Folding Slots -->
<slot id='0' type='CPU'/>
<slot id='1' type='GPU'/>
</config>

+ 61
- 0
log.txt Переглянути файл

@@ -0,0 +1,61 @@
*********************** Log Started 2020-03-12T19:40:18Z ***********************
19:40:18:************************* Folding@home Client *************************
19:40:18: Website: https://foldingathome.org/
19:40:18: Copyright: (c) 2009-2018 foldingathome.org
19:40:18: Author: Joseph Coffland <joseph@cauldrondevelopment.com>
19:40:18: Args:
19:40:18: Config: <none>
19:40:18:******************************** Build ********************************
19:40:18: Version: 7.5.1
19:40:18: Date: May 11 2018
19:40:18: Time: 19:59:04
19:40:18: Repository: Git
19:40:18: Revision: 4705bf53c635f88b8fe85af7675557e15d491ff0
19:40:18: Branch: master
19:40:18: Compiler: GNU 6.3.0 20170516
19:40:18: Options: -std=gnu++98 -O3 -funroll-loops
19:40:18: Platform: linux2 4.14.0-3-amd64
19:40:18: Bits: 64
19:40:18: Mode: Release
19:40:18:******************************* System ********************************
19:40:18: CPU: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
19:40:18: CPU ID: GenuineIntel Family 6 Model 158 Stepping 10
19:40:18: CPUs: 6
19:40:18: Memory: 7.72GiB
19:40:18: Free Memory: 276.59MiB
19:40:18: Threads: POSIX_THREADS
19:40:18: OS Version: 5.3
19:40:18: Has Battery: false
19:40:18: On Battery: false
19:40:18: UTC Offset: -4
19:40:18: PID: 15921
19:40:18: CWD: /home/noelle/Documents/git/hello
19:40:18: OS: Linux 5.3.0-7629-generic x86_64
19:40:18: OS Arch: AMD64
19:40:18: GPUs: 0
19:40:18:CUDA Device 0: Platform:0 Device:0 Bus:1 Slot:0 Compute:6.1 Driver:10.2
19:40:18: OpenCL: Not detected: Failed to open dynamic library 'libOpenCL.so':
19:40:18: libOpenCL.so: cannot open shared object file: No such file or
19:40:18: directory
19:40:18:***********************************************************************
19:40:18:<config>
19:40:18: <!-- Folding Slots -->
19:40:18:</config>
19:40:18:Connecting to assign1.foldingathome.org:8080
19:40:18:Updated GPUs.txt
19:40:18:Read GPUs.txt
19:40:18:Trying to access database...
19:40:18:Successfully acquired database lock
19:40:18:Enabled folding slot 00: PAUSED cpu:4 (not configured)
19:40:18:Enabled folding slot 01: PAUSED gpu:0:GP107 [GeForce GTX 1050 LP] 1862 (not configured)
19:40:18:ERROR:Exception: Could not bind socket to 0.0.0.0:7396: Address already in use
19:40:22:Caught signal SIGINT(2) on PID 15921
19:40:22:Exiting, please wait. . .
19:40:23:Saving configuration to config.xml
19:40:23:<config>
19:40:23: <!-- Folding Slots -->
19:40:23: <slot id='0' type='CPU'/>
19:40:23: <slot id='1' type='GPU'/>
19:40:23:</config>
19:40:23:Set client configured
19:40:23:Clean exit

+ 49
- 1
src/lib.rs Переглянути файл

@@ -103,4 +103,52 @@ impl Worker {
thread: Some(thread),
}
}
}
}

pub struct Response {
header: String,
content: String,
}

impl Response {
pub fn new(hdr: &str, cnt: &str) -> Response {
Response {
header: String::from(hdr),
content: String::from(cnt),
}
}

pub fn set_header(&mut self, hdr: &str) {
self.header = String::from(hdr);
}

pub fn get_header(&self) {
self.header
}

pub fn set_content(&mut self, cnt: &str) {
self.content = String::from(cnt);
}

pub fn add_content(&mut self, cnt: &str) {
self.content.push_str(cnt);
}

pub fn get_content(&self) {
self.content
}
}

pub struct Route {
path: &'static str,
action: fn(&&str, &mut Response),
}

impl Route {
fn new(p: String, a: fn(&&str, &mut Response)) {
Route {
path: p,
action: (a),
}
}
}

+ 97
- 52
src/main.rs Переглянути файл

@@ -6,16 +6,11 @@ use std::fs;
// use std::str;
use std::path::Path;

use hello::ThreadPool;
use hello::{ThreadPool, Response, Route};

extern crate regex;
use regex::Regex;

struct Response {
header: String,
path: String,
}

fn main() {
let listener = TcpListener::bind("127.0.0.1:26382").unwrap();
let pool = ThreadPool::new(4);
@@ -33,70 +28,38 @@ fn main() {
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();

let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap();
let mut r = Response::new("","");
let bf = &String::from_utf8_lossy(&buffer[..]);
// let get = b"GET / HTTP/1.1\r\n";
// let sleep = b"GET /sleep HTTP/1.1\r\n";

let mut r = Response{
header: String::from(""),
path: String::from(""),
};

if buffer.starts_with(b"GET") {

let caps = hdr.captures(bf);
if buffer.starts_with(b"GET") {
let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap();
let caps = hdr.captures(&bf);
match caps {
Some(cap) => {
let c = cap.get(1).unwrap().as_str();
println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists());
if c == "" {
println!("Asked to send root!");
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
r.path.push_str("index.html");
} else if Path::new(c).exists() {
if Path::new(c).is_dir() {
let mut cs = c.to_string();
if cs.chars().last().unwrap().to_string() != "/" {
cs.push_str("/");
}
cs.push_str("index.html");
if Path::new(&cs).exists() {
println!("Asked to send {}", &cs);
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
r.path.push_str(&cs);
} else {
println!("Asked to send {} and I couldn't find it", &cs);
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.path.push_str("404.html");
}
} else {
println!("Asked to send {}", &c);
r.header.push_str("HTTP/1.1 200 OK\r\n\r\n");
r.path.push_str(c);
}
} else {
println!("Asked to send {} and I could not find it", &c);
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.path.push_str("404.html");
}
route(&c, &mut r);
},
None => {
println!("This wasn't even a well-formed header");
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.path.push_str("404.html");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
}
}
} else {
println!("It didn't start with GET!");
r.header.push_str("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.path.push_str("404.html");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
}
// println!("Sending {}: {}", r.header, r.path);
let contents = fs::read_to_string(r.path).unwrap();
let response = format!("{}{}", r.header, contents);
let response = format!("{}{}", r.header, r.content);

stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
@@ -104,4 +67,86 @@ fn handle_connection(mut stream: TcpStream) {


// println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
}

fn response_add_file(p: &str, mut r: &mut Response) {
r.set_content(fs::read_to_string(p).unwrap());
}

fn route(c: &str, mut r: &mut Response) {

let routes = vec![
Route {
path: "api/hi",
action: hi,
},
Route {
path: "api/bye",
action: bye,
},
];


// println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists());
if c == "" {
println!("Asked to send root!");
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file("index.html", &mut r);
} else if c.starts_with("api/") {
let valid_route = routes.iter().find(|&x| x.path == c);
match valid_route {
Some(rt) => {
(rt.action)(&c, &mut r);
},
None => {
none_api(&c, &mut r);
}
}
} else if Path::new(c).exists() {
route_basic(&c, &mut r);
} else {
println!("Asked to send {} and I could not find it", &c);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
}

}

fn route_basic(c: &&str, mut r: &mut Response) {
if Path::new(c).is_dir() {
let mut cs = c.to_string();
if cs.chars().last().unwrap().to_string() != "/" {
cs.push_str("/");
}
cs.push_str("index.html");
if Path::new(&cs).exists() {
println!("Asked to send {}", &cs);
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file(&cs, &mut r);
} else {
println!("Asked to send {} and I couldn't find it", &cs);
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
response_add_file("404.html", &mut r);
}
} else {
println!("Asked to send {}", &c);
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
response_add_file(&c, &mut r);
}
}

fn none_api(_c: &&str, r: &mut Response) {
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.set_content("There is currently no API endpoint at that address.");
}

fn hi(_c: &&str, r: &mut Response) {
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
r.set_content("Hello there!");
}

fn bye(_c: &&str, r: &mut Response) {
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
r.set_content("Leaving so soon?");
}

BIN
work/client.db Переглянути файл


Завантаження…
Відмінити
Зберегти