Compare commits
3 Commits
c228129e21
...
db
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d8cdcfeb12 | ||
![]() |
0698deee41 | ||
![]() |
97f6a4bdb9 |
97
Cargo.lock
generated
97
Cargo.lock
generated
@@ -44,6 +44,12 @@ dependencies = [
|
|||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libsqlite3-sys"
|
name = "libsqlite3-sys"
|
||||||
version = "0.35.0"
|
version = "0.35.0"
|
||||||
@@ -54,11 +60,18 @@ dependencies = [
|
|||||||
"vcpkg",
|
"vcpkg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "my_udp_server"
|
name = "my_udp_server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -67,6 +80,24 @@ version = "0.3.32"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
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]]
|
[[package]]
|
||||||
name = "rusqlite"
|
name = "rusqlite"
|
||||||
version = "0.37.0"
|
version = "0.37.0"
|
||||||
@@ -78,15 +109,81 @@ dependencies = [
|
|||||||
"fallible-streaming-iterator",
|
"fallible-streaming-iterator",
|
||||||
"hashlink",
|
"hashlink",
|
||||||
"libsqlite3-sys",
|
"libsqlite3-sys",
|
||||||
|
"serde_json",
|
||||||
"smallvec",
|
"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]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.15.1"
|
version = "1.15.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
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]]
|
[[package]]
|
||||||
name = "vcpkg"
|
name = "vcpkg"
|
||||||
version = "0.2.15"
|
version = "0.2.15"
|
||||||
|
@@ -4,4 +4,5 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rusqlite = "0.37.0"
|
rusqlite = {version = "0.37.0", features = ["serde_json"]}
|
||||||
|
serde_json = "1.0.145"
|
||||||
|
49
src/main.rs
49
src/main.rs
@@ -1,12 +1,13 @@
|
|||||||
use std::net::UdpSocket; // UDP server
|
use std::net::UdpSocket; // UDP server
|
||||||
use rusqlite::{params, Connection, Result}; // Database operations and result handling
|
use rusqlite::{params, Connection, Result}; // Database operations and result handling
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
use serde_json::{Value};
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
|
// Set up db and make sure it works
|
||||||
let _ = create_database();
|
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_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
||||||
let socket: UdpSocket = match socket_result {
|
let socket: UdpSocket = match socket_result {
|
||||||
@@ -32,7 +33,18 @@ fn main() -> std::io::Result<()> {
|
|||||||
String::from_utf8_lossy(&buffer[..bytes_received])
|
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
|
// Echo the data back to the client
|
||||||
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
||||||
@@ -49,9 +61,11 @@ fn create_database() -> Result<()> {
|
|||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE IF NOT EXISTS packets (
|
"CREATE TABLE IF NOT EXISTS packets (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
raw TEXT NOT NULL,
|
date INTEGER NOT NULL,
|
||||||
age INTEGER NOT NULL,
|
source TEXT,
|
||||||
date INTEGER NOT NULL
|
summary TEXT,
|
||||||
|
final_destination TEXT,
|
||||||
|
text TEXT
|
||||||
)",
|
)",
|
||||||
[], // No parameters needed
|
[], // No parameters needed
|
||||||
)?;
|
)?;
|
||||||
@@ -60,18 +74,31 @@ fn create_database() -> Result<()> {
|
|||||||
Ok(())
|
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 conn = Connection::open("pkt_database.db")?;
|
||||||
|
|
||||||
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||||
|
|
||||||
|
//println!{"{}", p_json["type"]}
|
||||||
|
|
||||||
|
//println!("{}", p_json);
|
||||||
|
//println!("{}", &p_json["source"]);
|
||||||
|
|
||||||
// Insert
|
// Insert
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
|
"INSERT INTO packets (date,
|
||||||
params![raw, age, ds], // Bind parameters
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user