Rough out db basics.
This commit is contained in:
48
src/main.rs
48
src/main.rs
@@ -1,8 +1,13 @@
|
||||
// UDP server
|
||||
use std::net::UdpSocket;
|
||||
use std::net::UdpSocket; // UDP server
|
||||
use rusqlite::{params, Connection, Result}; // Database operations and result handling
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
let _ = create_database();
|
||||
let _ = insert_packet("foo", 1);
|
||||
|
||||
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
||||
let socket: UdpSocket = match socket_result {
|
||||
Ok(s) => s,
|
||||
@@ -17,7 +22,6 @@ fn main() -> std::io::Result<()> {
|
||||
// Buffer for incoming data (512 bytes)
|
||||
let mut buffer: [u8; 512] = [0; 512];
|
||||
|
||||
|
||||
loop {
|
||||
// Receive data from the client
|
||||
let (bytes_received, src_addr) = socket.recv_from(&mut buffer)?;
|
||||
@@ -32,3 +36,41 @@ fn main() -> std::io::Result<()> {
|
||||
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Database functions
|
||||
fn create_database() -> Result<()> {
|
||||
// Connect to SQLite database (creates the file if it doesn't exist)
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
// Create a table
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS packets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
raw TEXT NOT NULL,
|
||||
age INTEGER NOT NULL,
|
||||
date INTEGER NOT NULL
|
||||
)",
|
||||
[], // No parameters needed
|
||||
)?;
|
||||
|
||||
println!("Database and table created successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_packet(raw: &str, age: i32) -> Result<()> {
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||
|
||||
// Insert
|
||||
conn.execute(
|
||||
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
|
||||
params![raw, age, ds], // Bind parameters
|
||||
)?;
|
||||
|
||||
println!("Row inserted successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user