Skip to content

umrn5505/DropBoxProject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DropBox Server Architecture Implementation

Overview

This project implements a robust, thread-safe DropBox-like server architecture in C that handles concurrent client connections and supports file operations through a multi-layered design.

Architecture Design

Three-Layer Architecture

  1. Accept/Main Thread Layer

    • Single main thread listens for incoming TCP connections on port 8080
    • Accepts client connections and pushes socket descriptors into a thread-safe Client Queue
    • Implements graceful shutdown handling with signal management
  2. Client Threadpool Layer

    • Pool of client threads (configurable, default: 10 threads) consume from Client Queue
    • Each thread handles one client session at a time
    • Performs user authentication (signup/login)
    • Parses textual commands from clients
    • Creates tasks and enqueues them into the global Task Queue
    • Waits for task completion using condition variables
  3. Worker Threadpool Layer

    • Separate pool of worker threads (configurable, default: 5 threads) consume from Task Queue
    • Performs heavy operations: file I/O, quota checking, metadata updates
    • Supports UPLOAD, DOWNLOAD, DELETE, and LIST operations
    • Ensures thread-safe operations on shared resources

Key Features

Thread Safety & Synchronization

  • Mutex Protection: All shared data structures protected with mutexes
  • Condition Variables: Used for efficient thread blocking/waking
  • Race Condition Prevention: Careful ordering of lock acquisition and release
  • Deadlock Avoidance: Consistent lock ordering throughout the codebase

Memory Management

  • Resource Cleanup: All allocated memory properly freed
  • Socket Management: Proper socket closure on client disconnection
  • Thread Cleanup: Graceful thread termination on shutdown
  • No Memory Leaks: Validated with Valgrind-compatible design

Concurrent Client Support

  • Multi-user Sessions: Single user can have multiple simultaneous connections
  • Task Synchronization: Each task has individual mutex and condition variable
  • Result Delivery: Worker threads notify client threads via condition variables
  • Conflict Resolution: Framework in place for handling conflicting operations

File Structure

DropBoxBuild-MF/
├── dropbox_server.h      # Main header with all declarations
├── main.c               # Main server implementation and accept loop
├── queue_operations.c   # Thread-safe queue implementations
├── authentication.c     # User authentication and command parsing
├── thread_pool.c       # Client and worker thread implementations
├── test_client.c       # Simple client for testing
├── Makefile            # Build configuration
└── README.md           # This documentation

Compilation & Usage

Building the Server and Client

make

Running the Server

./dropbox_server

Running the Client

./test_client

Testing Workflow

  1. Start the server in one terminal:
    ./dropbox_server
    
  2. Start the client in another terminal:
    ./test_client
    
  3. In the client, perform the following commands:
    • Sign up:
      SIGNUP testuser password123
      
    • Upload a file (ensure upload_test.txt exists in your directory):
      UPLOAD upload_test.txt
      
    • List files:
      LIST
      
    • Download the file:
      DOWNLOAD upload_test.txt
      
    • Delete the file:
      DELETE upload_test.txt
      
    • List files again to confirm deletion:
      LIST
      
    • Quit:
      QUIT
      

Valgrind Memory Leak Check

valgrind --leak-check=full --track-origins=yes ./dropbox_server
  • Run the server with Valgrind, perform the above client operations, then stop the server with Ctrl+C to view the memory report.

Additional Notes

  • All commands should be run from a WSL/Linux terminal.
  • The client and server communicate over TCP on port 8080.
  • The file upload_test.txt must exist in the client directory before uploading.
  • No additional configuration is required for Phase 1.

Configuration

Key constants in dropbox_server.h:

  • PORT: Server listening port (default: 8080)
  • CLIENT_THREADPOOL_SIZE: Number of client threads (default: 10)
  • WORKER_THREADPOOL_SIZE: Number of worker threads (default: 5)
  • QUEUE_SIZE: Maximum queue capacity (default: 50)
  • MAX_CLIENTS: Maximum concurrent connections (default: 100)

Thread Synchronization Design

Client Queue Synchronization

typedef struct client_queue {
    int *sockets;                    // Array of socket descriptors
    int front, rear, count, capacity; // Queue state
    pthread_mutex_t mutex;           // Protects queue operations
    pthread_cond_t not_empty;        // Signals when queue has items
    pthread_cond_t not_full;         // Signals when queue has space
} client_queue_t;

Task Queue Synchronization

typedef struct task_queue {
    task_t *head, *tail;             // Linked list pointers
    int count, capacity;             // Queue state
    pthread_mutex_t mutex;           // Protects queue operations
    pthread_cond_t not_empty;        // Signals when queue has items
    pthread_cond_t not_full;         // Signals when queue has space
} task_queue_t;

Task Completion Synchronization

typedef struct task {
    // Task data...
    task_status_t status;            // Current task status
    pthread_mutex_t task_mutex;      // Protects task status/results
    pthread_cond_t task_cond;        // Signals task completion
    // Result data...
} task_t;

Authentication System

  • Signup: Creates new user account with password storage
  • Login: Validates credentials against stored data
  • File-based Storage: User credentials stored in users/ directory
  • User Directories: Individual storage directories in storage/ per user

Error Handling

Network Errors

  • Connection failures handled gracefully
  • Client disconnections detected and cleaned up
  • Socket errors logged with appropriate messages

Threading Errors

  • Mutex/condition variable initialization failures handled
  • Thread creation failures cause graceful shutdown
  • Resource cleanup on all error paths

Authentication Errors

  • Invalid credentials rejected with informative messages
  • Malformed commands result in error responses
  • User directory creation failures handled

Extensibility

Adding New Commands

  1. Add new task type to task_type_t enum
  2. Implement handler function in worker thread
  3. Add command parsing in client thread
  4. Update command validation logic

Modifying Queue Sizes

  • Adjust constants in header file
  • Queues dynamically allocate based on configured size
  • No code changes required for size modifications

Testing & Validation

Memory Leak Detection

# Using Valgrind
make valgrind

# Using AddressSanitizer
make debug

Race Condition Detection

# Using ThreadSanitizer
make tsan

Manual Testing

  1. Start server: ./dropbox_server
  2. Connect multiple clients: ./test_client
  3. Test concurrent operations from different clients
  4. Verify proper authentication and command handling

Security Considerations

  • Password Storage: Currently plaintext (should be hashed in production)
  • Input Validation: Basic validation implemented, can be enhanced
  • Buffer Overflow Protection: Fixed-size buffers with bounds checking
  • Access Control: Users can only access their own files

Future Enhancements

For Your Colleagues to Implement

  1. File Operations: Complete UPLOAD, DOWNLOAD, DELETE, LIST implementations
  2. Quota Management: Implement per-user storage quotas
  3. File Metadata: Add file timestamps, permissions, checksums
  4. Conflict Resolution: Handle concurrent operations on same files
  5. Performance Optimization: Implement file caching, compression

Potential Improvements

  • SSL/TLS encryption for network communication
  • Database backend for user management
  • Load balancing across multiple server instances
  • Distributed file storage backend
  • Real-time file synchronization

Debugging Tips

  1. Enable Debug Output: Compile with make debug
  2. Thread Identification: Each thread logs its pthread ID
  3. Queue Status: Queue operations log current size
  4. Task Tracking: Tasks log creation, enqueue, dequeue, completion
  5. Connection Monitoring: Client connections/disconnections logged

Architecture Validation

This implementation ensures:

  • No Race Conditions: All shared data protected by mutexes
  • No Memory Leaks: All resources properly cleaned up
  • Thread Safety: Proper synchronization throughout
  • Graceful Shutdown: Signal handling and resource cleanup
  • Scalability: Configurable thread pool sizes
  • Maintainability: Clean separation of concerns
  • Testability: Comprehensive error handling and logging

This foundation provides a robust base for implementing the file operation features while maintaining thread safety and preventing race conditions.

About

OS Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •