Skip to content

CS-Personal-Data-Acquisition-Prototype/Rust-Tcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust-Tcp

Overview

Purpose

A TCP server built in Rust to facilitate communication between the UI-Layer, Pi_Transmit, and the integrated database.
Its designed to be multithreaded and hosted through AWS to enable future scalability and expansion through region deployment.

To see detailed information on all the avaliable request and endpoint information, visit the API Specification Document.

Features

  • Thread safe server with RESTful design principles that is ready for multithreading
  • SQLite3 integrated database
  • Responses can be generated from files
  • Polymorphic and type-agnostic design utilizing dependency injecting
    • Database integrations can be expanded by implementing the Database trait and abstract functions
    • Chosen database format is injected and initialized on program start
    • All model functionality is implemented through the BaseModel trait and abstract functions
    • Models are directly parsed into HttpResponse by calling BaseModel::public_json()
  • Raw requests deserialized to strongly typed HttpRequest struct
  • Request headers pre-parsed to read exact body from stream
  • Compiler enforced route handling with respect to method, headers, path, and body
  • Routing returns initialized HttpResponse struct
  • HttpHeader utilizes a factory builder pattern to support HttpResponse functionality

Libraries Used

url serde serde_json chrono rusqlite toml

  • Url used for parsing requests url
  • Serde and serde_json used for serialization and deserialization
  • Chrono used for UTC timestamps
  • Rusqlite used for database integration
  • Toml used for config file parsing

Repository Structure

  • tcp-server/
    • Cargo.toml
    • src/
      • config.toml - The programs configuration file
      • main.rs - Holds the entrypoint as well as the client handling and request routing logic
      • data/
        • mod.rs
        • database.rs - Holds the Database trait implementation
        • mock_database.rs - Holds a mock Database implementation for testing
        • sqlite_database.rs - Holds the SQLite Database implementation for production
        • test_sqlite_db.rs - Holds testing functionality for sqlite_database.rs
      • http/
        • mod.rs
        • http_header.rs - Holds the HttpStatus, HttpHeaderType, and HttpHeader struct implementations
        • http_method.rs - Holds the HttpMethod struct implementation
        • http_path.rs - Holds the HttpPath struct implementation
        • http_request.rs - Holds the HttpRequest struct implementation with byte buffer parsing logic
        • http_response.rs - Holds the HttpResponse struct implementation with stream sending logic
      • models/
        • mod.rs
        • base_model.rs - Holds the BaseModel trait implementation
        • user_model.rs - Holds the User implementation of the BaseModel trait
        • sensor_model.rs - Holds the Sensor implementation of the BaseModel trait
        • session_model.rs - Holds the Session implementation of the BaseModel trait
        • session_sensor_model.rs - Holds the SessionSensor implementation of the BaseModel trait
        • session_sensor_data_model.rs - Holds the SessionSensorData implementation of the BaseModel trait
      • views/
        • 404.html - A fallback to provide a 404 response when HTML is expected
        • index.html - A placeholder webpage to be replaced with files generated by the UI-Layer repository

Setup

  1. Open a terminal where the repository will be cloned

  2. Install the Rust toolchain if not already installed

    • run the command
      curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • Hit enter again for the default installation
    • Check it was installed correctly by running the following commands
      rustc --version
      cargo --version
  3. Clone the repository by running the following command

    git clone https://github.com/CS-Personal-Data-Acquisition-Prototype/Rust-Tcp.git
    
  4. Edit the configuration file config.toml in the src directory

  5. Run the program by following the Usage section

Configuration

The configuration file doesn't have any headers.
The default values look like the following:

database_file = "data_acquisition.db"   # name of local database file
local_addr = "0.0.0.0:7878"             # local address to listen for TCP requests on

Usage

This crates defaults to a mock database connection when using cargo build or cargo run.
To utilize an SQLite database the crate must be built and ran with --features sql.

  1. Open a terminal in the directory 'tcp-server'.

  2. To run the project in debug use the command: cargo run [--features sql]

  3. To run the project in release mode use the command: cargo run --release [--features sql]

Future Work

  • User authentication, utilizing existing session cookie infastructure
  • Multithread with pooling as demand increases, existing code is already thread-safe
  • Parsing request url query string to support API query parameters

License Notice

To apply the Apache License to your work, attach the following boilerplate notice. The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.

Copyright 2025 CS 46X Personal Data Acquisition Prototype Group

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE-TWO
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5