-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (88 loc) · 3.24 KB
/
server.js
File metadata and controls
111 lines (88 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// server.js - Express server for CDMA simulation (ES Module version)
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import { encode, decode, combineSignals, generateWalshMatrix, displayWalshMatrix } from "./cdma.js";
// Needed for __dirname in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
// Routes
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// CDMA simulation endpoint
app.post("/simulate", (req, res) => {
try {
const { stations } = req.body;
if (!stations || stations.length === 0) {
return res.status(400).json({ error: "No stations provided" });
}
for (let i = 0; i < stations.length; i++) {
if (!stations[i] || !/^[01]+$/.test(stations[i].trim())) {
return res.status(400).json({
error: `Station ${i + 1} has invalid data. Please use only 0s and 1s.`
});
}
}
const walshSize = Math.pow(2, Math.ceil(Math.log2(stations.length)));
const walshMatrix = generateWalshMatrix(walshSize);
console.log(`\n=== CDMA Simulation ===`);
console.log(`Stations: ${stations.length}`);
console.log(`Walsh Matrix Size: ${walshSize}x${walshSize}`);
displayWalshMatrix(walshMatrix.slice(0, stations.length));
const encodedSignals = stations.map((bits, idx) => {
const bitArray = bits.trim().split("").map(Number);
const encoded = encode(bitArray, walshMatrix[idx]);
console.log(`Station ${idx + 1}: ${bits} -> [${encoded.join(', ')}]`);
return encoded;
});
const combined = combineSignals(encodedSignals);
console.log(`Combined Signal: [${combined.join(', ')}]`);
const decoded = walshMatrix.slice(0, stations.length).map((code, idx) => {
const decodedBits = decode(combined, code);
console.log(`Decoded Station ${idx + 1}: [${decodedBits.join('')}]`);
return decodedBits;
});
console.log(`=== End Simulation ===\n`);
res.json({
encodedSignals,
combined,
decoded,
walshCodes: walshMatrix.slice(0, stations.length),
originalData: stations
});
} catch (error) {
console.error("Simulation error:", error);
res.status(500).json({ error: "Internal server error during simulation" });
}
});
// Walsh matrix endpoint
app.get("/walsh/:size", (req, res) => {
try {
const size = parseInt(req.params.size);
if (isNaN(size) || size < 1 || (size & (size - 1)) !== 0) {
return res.status(400).json({ error: "Size must be a positive power of 2" });
}
const matrix = generateWalshMatrix(size);
res.json({ size, matrix });
} catch (error) {
res.status(500).json({ error: "Error generating Walsh matrix" });
}
});
// Error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: "Something went wrong!" });
});
app.use((req, res) => {
res.status(404).json({ error: "Route not found" });
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 CDMA Visualizer Server running on http://localhost:${PORT}`);
});