Build a simple segment chat application (Discord-like) with application protocols defined by each group, using the TCP/IP protocol stack.
- Introduction
- System Architecture
- Assignment Requirements & Implementation
- 1. Hybrid Paradigm (Client-Server + P2P)
- 2. Tracker Protocol (Initialization Phase)
- 3. Client-Server Paradigm
- 4. Peer-to-Peer Paradigm & Livestream
- 5. Authentication & Visitor Mode
- 6. Channel (Chat Room) & Functionalities
- 7. Personal Server / Channel Hosting & Synchronization
- 8. User Access & Notification
- 9. System Log
- 10. Advanced Features & Extensions
- Installation & Execution Guide
- Project Structure & File Descriptions
- Usage Guide
- Enhancements & Future Development
- Team Info & Roles
This is a Discord-like Chat Application designed to demonstrate a hybrid architecture combining Client-Server and Peer-to-Peer (P2P) paradigms, with additional support for real-time livestreaming. Key features include:
- Authentication-based login and anonymous Visitor access.
- Creating, joining, and chatting in channels.
- P2P-based livestreaming using UDP.
- Centralized server (tracker + MongoDB) ensures data persistence even when the hosting peer is offline.
- System-wide connection and activity logging.
- Tracker Server: Centralized component that maintains an updated list of connected peers.
- Peer: Each client instance runs its own peer node:
- TCP socket for server connection.
- TCP socket listener for incoming P2P messages.
- UDP socket for receiving P2P livestream data.
- MongoDB Database: Stores persistent data including users, channels, messages, and logs.
Peer A <----> Peer B
\ /
\ / (P2P)
\ /
Tracker (Server) -- MongoDB
- Requirement: Support both paradigms.
- Implementation:
tracker.pymaintains active peers.user.pyconnects to tracker and enables P2P messaging and livestreaming.
- Requirement: Implement
submit_info,add_list,get_listcommands. - Implementation:
user.py→connect_to_tracker()sendsCONNECTcommand.- Tracker stores peer info and replies with peer list on
GET_LIST.
- Requirement: Message storage, authentication, and channel data must go through the server.
- Implementation:
- Implemented via
channelRequest.py,channelService.py, andMongoDB.
- Implemented via
- Requirement: Use P2P to broadcast real-time data (chat or livestream).
- Implementation:
- Chat:
send_p2p_broadcast()via TCP. - Livestream:
start_livestream()→ OpenCV + Base64 + UDP broadcasting.
- Chat:
- Requirement:
- Visitors: View-only, no registration needed.
- Authenticated users: Full permissions.
- Implementation:
Login.pyhandles both modes.authService.pyvalidates@hcmut.edu.vnemails.
- Requirement: Users can create, join, and interact in channels.
- Implementation:
- Create via “+ Create Channel” (UI →
Home.py). - Backend logic:
channelService.py,channelController.py. - Visitor view controlled by
allow_visitor.
- Create via “+ Create Channel” (UI →
- Requirement: Each user can host their own channel; synchronization with server is required.
- Implementation:
- Local files:
offline_<username>.txt,hosted_<channel>.json. - Sync logic in
user.py→sync_local_channels_with_server()andchannelService.sync_channels().
- Local files:
- Requirement: Notify users on new messages.
- Implementation:
- 5-second polling via
poll_new_messages()in UI. - Tracker supports “MSG_RECV” command for pushing updates.
- 5-second polling via
- Requirement: Log all system events in ASCII format.
- Implementation:
Run.pyconfigures logging withsystem.log.- Events: login, message sending, livestream toggling, etc.
- MongoDB for scalable data persistence.
- Invisible Mode in UI status dropdown.
- GUI built with PyQt6.
- Optional: multi-seeding, REST APIs for bot integration.
- Python 3.8+
- Install dependencies:
pip install -r requirements.txt
- URI configured in db.py. Ensure your MongoDB instance is accessible.
- Expected log: Connected to MongoDB successfully!
python tracker.py(default port 5000)
- Mở terminal khác, chạy:
python Run.py
- (launches local server + UI)
- Via GUI → Register → Must use @hcmut.edu.vn email
- Login: Full access
- Login as Viewer: View-only if the channel allows
.
├── Run.py # Main app entry, starts internal server and GUI
├── tracker.py # Tracker server (peer registry)
├── user.py # Peer class: P2P logic, UDP, livestream, sync
├── system.log # Log file for all system events
├── authService.py # User authentication logic
├── channelService.py # Channel CRUD operations and message sync
├── authRequest.py # Entry point for login/register requests
├── channelRequest.py # Channel request handler
├── UI/
│ ├── Login.py # PyQt login/register window
│ ├── Home.py # Main chat interface (channels, messages, members)
├── db.py # MongoDB connection
├── ... # Additional model/controller files- Start the Tracker by running
tracker.py. - Launch the Main Application by executing
Run.py, which opens the login GUI. - Register an account (if not already registered).
- Log in using your credentials or click “Login as Viewer” for read-only access.
- Click “+ Create Channel” in the sidebar to create a new channel, with the option to enable/disable visitor access.
- In the Channels tab, select the newly created channel.
- Click “Join Channel” (if you are the owner, you'll be joined automatically).
- Type your message and click “Send” — it will be saved to the central server.
- You can send direct P2P messages by calling the
send_p2p_broadcast()function either from the console or from withinuser.py.
- Enable “Live Mode (P2P)” or click “Start Livestream” in the interface to start broadcasting your webcam.
- Peers currently online will receive the video stream via UDP.
- If the peer cannot send a message (e.g., due to connectivity loss), it will be saved in a local file (
offline_<username>.txt). - When the peer reconnects,
sync_offline_messages()automatically sends the cached messages. - Similarly, hosted channels store messages locally when offline, and they are synchronized to the server upon reconnection.
- A dropdown allows users to switch between:
- Online: Fully connected and visible to other users.
- Offline: Disconnected from the tracker; no messages sent/received.
- Invisible: Functions like Online but appears Offline to others.
