# Every Matrix Homework **Repository Path**: weikebing/every-matrix-homework ## Basic Information - **Project Name**: Every Matrix Homework - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-25 - **Last Updated**: 2026-01-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Betting Server - Simplified Implementation Guide ## Project Overview HTTP-based backend for managing betting offer stakes with three endpoints. ## Requirements Met - ✅ **GET /{customerId}/session** - 10-minute sessions, same within 10 minutes - ✅ **POST /{betOfferId}/stake?sessionkey={key}** - Only valid sessions accepted - ✅ **GET /{betOfferId}/highstakes?sessionkey={key}** - Top 20 stakes, one per customer, sorted descending - ✅ **No persistence** - In-memory storage only - ✅ **High concurrency** - Thread-safe with ConcurrentHashMap - ✅ **No external frameworks** - Uses only JDK built-in libraries ## Project Structure ``` src/com/betting/ ├── BettingServer.java # Main HTTP server ├── ManualTest.java # Manual test utilities ├── server/ # HTTP server components │ ├── HttpHandler.java │ ├── HttpRequestAdapter.java │ ├── HttpResponse.java │ ├── HttpUtils.java │ ├── Router.java │ └── endpoint/ │ ├── RootEndpoint.java │ ├── SessionEndpoint.java │ └── StakeEndpoint.java ├── session/ # Session management │ ├── SessionManager.java │ └── SessionData.java └── stake/ # Stake storage and retrieval ├── StakeManager.java └── StakeEntry.java ``` ## How to Use ### 1. Build the Project **Windows:** ```cmd build.bat ``` **Linux/macOS:** ```bash ./build.sh ``` Generates: `BettingServer.jar` ### 2. Run the Server ```bash java -jar BettingServer.jar ``` Server starts on: `http://localhost:8080` ### 3. Test the API ```bash # Get session curl http://localhost:8080/1234/session # Post stake (replace SESSION_KEY) curl -X POST -d 5000 "http://localhost:8080/888/stake?sessionkey=SESSION_KEY" # Get high stakes curl http://localhost:8080/888/highstakes?sessionkey=SESSION_KEY ``` ## API Endpoints ### 1. Create Session ``` GET /1234/session → ABC1234 ``` ### 2. Submit Stake ``` POST /888/stake?sessionkey=ABC1234 Body: 5000 → (empty response, HTTP 200) ``` ### 3. Get High Stakes ``` GET /888/highstakes?sessionkey=ABC1234 → 1234=5000,5678=3000 ``` ## Key Features ### Session Management - Sessions expire after 10 minutes - Same session returned within 10 minutes - 32-character alphanumeric keys (letters and digits only), generated using SecureRandom - Session key is tied to a specific customer ID - Periodic cleanup of expired sessions (does not affect stakes) ### Stake Management - Only the highest stake per customer per offer (Top20 maintained; submissions below the current cutoff and not already in Top20 are dropped to keep memory bounded) - When Top20 is full, new stakes must exceed the lowest stake (or equal with lower customer ID) to be included - Per-offer Top-20 structure maintained incrementally using TreeSet (O(log 20) updates) - Thread-safe per-offer locking with ReentrantLock for consistent updates/logging ### Performance - Updates are O(log 20) per stake post (bounded Top-20 maintenance using TreeSet) - High stakes query is O(20) to format result - ConcurrentHashMap for thread-safe session and offer storage - Fixed thread pool (10-50 threads) with CallerRunsPolicy for request handling ## Design Decisions ### Why No Persistence? - Requirement: "no persistence" - Simpler implementation - Faster read/write operations ### Why Periodic Session Cleanup? - Long-running stability: prevents expired sessions accumulating indefinitely - Stakes are not deleted when sessions expire (per requirements) ## Notes - Data is lost on server restart - Each customer appears once in high stakes - Maximum 20 results returned - Empty string returned for no stakes