Skip to content

ju475/chatSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

143 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatSystem

Overview

ChatSystem is a distributed, real-time chat system implemented in Java 21 and built with Maven.
It enables multiple users on the same local network to communicate without any centralized server, relying on peer-to-peer discovery, structured message exchange, multithreading, and local persistence.

Each instance runs independently while dynamically discovering other peers, maintaining local state consistency, and persisting messages.


Key Features

Networking & Distribution

  • Fully decentralized architecture (no server, no master node).
  • Automatic peer discovery using UDP broadcast.
  • Reliable message exchange combining UDP (discovery & signaling) and TCP (data).
  • Structured communication through typed Payloads and Tags.
  • Asynchronous message handling using dispatchers and pipelines.

System Architecture

  • Central ChatSystem model representing the global application state.
  • Explicit lifecycle management (BOOTSTRAP → RUNNING → SHUTDOWN).
  • Clear separation between:
    • network
    • services
    • persistence
    • threading
  • Event-driven design using listeners and state transitions.

Multithreading

  • Centralized thread supervision via ThreadManager.
  • Dedicated threads for:
    • UDP listening
    • UDP sending
    • TCP server
    • Payload dispatching
  • Thread-safe pipelines (Pipe) for asynchronous processing.

Persistence

  • Local SQLite database for message storage.
  • Repository pattern for persistence abstraction.
  • Centralized database lifecycle management through DatabaseManager.
  • Messages persist across application restarts.

User & Session Management

  • Unique user identification (login + IP address).
  • Dynamic login updates propagated to all peers.
  • Automatic detection of disconnections.
  • Central contact list with listener notifications.

Graphical User Interface

  • A graphical interface is available in the gui package.
  • The GUI interacts with the core system and services without breaking the decentralized model.

Main Components

Core Models (models)

  • ChatSystem
    Represents the global application state and lifecycle.

  • ChatSystemContext
    Dependency container holding services, repositories, and network components.

  • ChatSystemBootstrap
    Responsible for initializing the system, threads, and network services.

  • ChatSystemShutdownManager
    Ensures graceful shutdown and peer notification.

  • ChatSystemState
    Enumeration of system states (DISCONNECTED, CONNECTING, CONNECTED, SHUTTING_DOWN, etc.).

  • User
    Represents a local or remote peer.

  • ContactList
    Singleton storing known users and notifying listeners on updates.


Network Layer (networks)

  • NetworkManager
    Central orchestrator for all network components.

  • ServerTCP
    TCP server responsible for reliable message delivery.

  • SenderUDP / ListenerUDP
    Peer discovery and system announcements.

  • Payload / Tag
    Typed network messages with explicit semantics.

  • Pipe
    Thread-safe pipeline enabling asynchronous message processing.

  • PayloadDispatcher
    Routes incoming payloads to appropriate handlers.


Services Layer (services)

  • SessionService
    Manages the local user session (login, address, state).

  • MessageService
    Handles message sending, receiving, and retrieval.

  • ApplicationService
    High-level application control and lifecycle coordination.


Persistence Layer (persistence)

  • MessageRepository
    Abstraction for message persistence.

  • SQLiteMessageRepository
    SQLite-based implementation of the repository.

  • DatabaseManager
    Singleton managing the database connection lifecycle.


Threading (threading)

  • ThreadManager
    Centralized registration, startup, and shutdown of all application threads.

Controller Layer (controller)

  • Contains application controllers coordinating interactions between the GUI, services, and core models.
  • This layer is intentionally kept thin and is not detailed here.

Installation

Requirements

  • Java 21 or later
  • Maven 3.8+

Build

From the project root:

mvn clean package

Run

The project is configured so that the main class is already defined in the pom.xml.

You can run the application using:

java -jar target/ChatSystem-1.0-SNAPSHOT-shaded.jar

Testing

Unit tests are located in src/test/java and cover:

  • Network flow and payload handling
  • Thread lifecycle and concurrency safety
  • Persistence (SQLite repositories)
  • Contact and user management

Run all tests with:

mvn test

Project Structure (Simplified)

src/
 ├─ main/java/
 │   ├─ networks/
 │   ├─ services/
 │   ├─ persistence/
 │   ├─ threading/
 │   ├─ models/
 │   ├─ gui/
 │   ├─ controller/
 │   └─ Main.java
 └─ test/java/
     ├─ networks/
     ├─ persistence/
     ├─ threading/
     └─ models/

Notes

  • UDP broadcast is used for peer discovery.
  • TCP is used for reliable message exchange.
  • The system is designed for extensibility, allowing:
    • New payload types
    • Additional services
    • Alternative persistence implementations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages