Skip to content

mdkaba/P2P-Shopping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 

Repository files navigation

Peer-to-Peer Shopping System

image

A distributed shopping prototype implemented in Java using UDP, TCP, and multithreading.
The system allows clients to register, search for items, negotiate prices, and complete purchases through a custom communication protocol. A persistent central server coordinates interactions between buyers and sellers.

🚀 Features

Networking & Protocol

  • Custom peer-to-peer shopping protocol over UDP (control messages) and TCP (transactions)
  • Asynchronous message delivery using separate sending/receiving threads
  • Fully custom message format (CSV-like), serialization, and parsing utilities
  • Hardcoded server and client ports for simplified discovery

Multithreading

  • Server spawns a new thread for each incoming UDP message
  • Client uses independent threads for:
    • CLI interaction
    • UDP reception
    • TCP inbound/outbound communication
    • Timers and expiration checks
  • Concurrency control using synchronized blocks and volatile fields

State & Persistence

  • Multiple item lifecycle states:
    • lookingFor
    • negotiating
    • reserved
    • found
    • offering
    • buying
  • Server-side persistence of:
    • Registered clients
    • Item lists
    • Current request number
  • Custom serialization/deserialization for Items, Offers, and RegisteredClient

TCP Transaction Flow

  • Two-way TCP transaction between buyer and seller:
    • INFORM_REQ / INFORM_RES exchange
    • Payment validation
    • Shipping confirmation
  • Independent sender/receiver threads for reliability

🛠 Technology Stack

  • Java
  • UDP + TCP sockets
  • Java concurrency (threads, synchronized, volatile)
  • File-based persistence

🧩 System Architecture

image

📦 Project Structure

├── src/
│   ├── Client.java
│   ├── Server.java
│   ├── Item.java
│   ├── Offer.java
│   ├── RegisteredClient.java
│   ├── OneShotUDPClient.java
│   ├── TCPClient.java
│   ├── TCPServer.java
│   └── Utils.java
├── project.jar
└── README.txt

📚 Protocol Overview

UDP Messages

  • REGISTER
  • DEREGISTER
  • LOOKING_FOR
  • SEARCH
  • OFFER
  • ACCEPT
  • REFUSE
  • FOUND
  • RESERVE
  • BUY
  • CANCEL
  • CANCEL_BUY
  • NOT_AVAILABLE

TCP Messages

  • INFORM_REQ
  • INFORM_RES
  • PAYMENT_SUCCESSFUL
  • SHIPPING_INFO

▶️ Running the Project

1. Compile (if using source files)

javac src/*.java -d out
jar cfe project.jar Main -C out .

2. Start the server

java -jar project.jar

Choose server when prompted.

3. Start one or more clients

java -jar project.jar

Provide:

  • Server IP
  • Desired client UDP port
  • Desired client TCP port

4. Perform operations

Available through the client CLI:

  • Register
  • Search for items
  • Make or respond to offers
  • Buy items
  • Cancel transactions

🧪 Testing & Validation

Testing performed across:

  1. Localhost (multiple terminals)
  2. Home LAN (multiple computers)
  3. University lab (separate physical machines)

Validated scenarios:

  • Successful negotiation and purchase
  • Offer timeout expiration
  • Negotiation timeouts
  • Concurrent buyers/sellers
  • Cancelled transactions
  • TCP binding issues on multi-homed systems
  • Input interrupt handling using buffer + busy state

All test cases passed during the final demonstration.

👥 Team Contributions

Maïka Trépanier

  • TCP client and server modules
  • Inbound message handlers
  • Timer logic (client & server)

Mamadou Kaba

  • Outbound client message functions (register, offer, buy, cancel)
  • Major server logic (message execution, negotiation, reservation, buy sequence)
  • OneShotUDPClient implementation

Lifu Zhang

  • Item, Offer, RegisteredClient classes
  • Serialization & persistency mechanisms
  • Migration to global constants for protocol strings

About

A distributed shopping system using UDP, TCP, and multithreading, enabling clients to register, search for items, negotiate prices, and complete purchases through a custom protocol.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages