Compare commits

...

3 Commits

Author SHA1 Message Date
mattbk
d8cdcfeb12 Pass JSON into the database as fields. 2025-10-12 20:59:51 -05:00
mattbk
0698deee41 Accept JSON from mwtchahrd. 2025-10-05 20:37:22 -05:00
mattbk
97f6a4bdb9 Clean up db more. 2025-10-04 22:46:44 -05:00
3 changed files with 137 additions and 12 deletions

97
Cargo.lock generated
View File

@@ -44,6 +44,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "libsqlite3-sys"
version = "0.35.0"
@@ -54,11 +60,18 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "my_udp_server"
version = "0.1.0"
dependencies = [
"rusqlite",
"serde_json",
]
[[package]]
@@ -67,6 +80,24 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "proc-macro2"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rusqlite"
version = "0.37.0"
@@ -78,15 +109,81 @@ dependencies = [
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"serde_json",
"smallvec",
]
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
"serde_core",
]
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "syn"
version = "2.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
[[package]]
name = "vcpkg"
version = "0.2.15"

View File

@@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
rusqlite = "0.37.0"
rusqlite = {version = "0.37.0", features = ["serde_json"]}
serde_json = "1.0.145"

View File

@@ -1,12 +1,13 @@
use std::net::UdpSocket; // UDP server
use rusqlite::{params, Connection, Result}; // Database operations and result handling
use std::time::{SystemTime, UNIX_EPOCH};
use serde_json::{Value};
fn main() -> std::io::Result<()> {
// Set up db and make sure it works
let _ = create_database();
let _ = insert_packet("foo", 1);
//let _ = insert_packet("foo");
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
let socket: UdpSocket = match socket_result {
@@ -32,7 +33,18 @@ fn main() -> std::io::Result<()> {
String::from_utf8_lossy(&buffer[..bytes_received])
);
let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]), 1);
let p_raw = &String::from_utf8_lossy(&buffer[..bytes_received]);
// look for type code in JSON here, only insert if data
let p: Value = serde_json::from_str(p_raw)?;
//p.what_is_this;
//println!("{}",p["type"]);
if p["type"] != "status" {
let _ = insert_packet(p);
}
// Echo the data back to the client
socket.send_to(&buffer[..bytes_received], src_addr)?;
@@ -49,9 +61,11 @@ fn create_database() -> Result<()> {
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
date INTEGER NOT NULL,
source TEXT,
summary TEXT,
final_destination TEXT,
text TEXT
)",
[], // No parameters needed
)?;
@@ -60,18 +74,31 @@ fn create_database() -> Result<()> {
Ok(())
}
fn insert_packet(raw: &str, age: i32) -> Result<()> {
fn insert_packet(p_json: serde_json::Value) -> Result<()> {
let conn = Connection::open("pkt_database.db")?;
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
//println!{"{}", p_json["type"]}
//println!("{}", p_json);
//println!("{}", &p_json["source"]);
// Insert
conn.execute(
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
params![raw, age, ds], // Bind parameters
)?;
"INSERT INTO packets (date,
source,
summary,
final_destination,
text) VALUES (?1, ?2, ?3, ?4, ?5)",
params![ds,
&p_json["source"],
&p_json["summary"],
&p_json["final_destination"],
&p_json["text"]], // Bind parameters
)?;
println!("Row inserted successfully.");
//println!("Row inserted successfully.");
Ok(())
}