Skip to content
This repository was archived by the owner on Jun 19, 2026. It is now read-only.

rohergun/DAS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Distributed-Averaging-System

Overview

The DAS ((Distributed Average System) project is a master-slave communication system implemented using UDP.
Program is operating in two modes Master and Slave mode. Master mode collects the data that has been sent from Slave mode, calculates their average and broadcast the result lastly listens for the signal to terminate program.

UDP Protocol Workflow

  1. Slave to Master Communication:
    Slave send integer value or control signal to Master via UDP packets
  2. Master Communication:
    Master listens on a specified port for incoming UDP packets
    Based on the message does either : append the value to the list, computes the average or terminates the program.

Implementation

Execution :

Program requires two arguments

  1. port : The port number for communication
  2. number : Integer to be sent with Slave mode or initial value of Master mode
  • Command to run : java DAS <port> <number>

Code Explanation :

main Method :

Handles command line arguments and mode selection.

Handles exceptions for the arguments.

If a SocketExpection occurs program starts to run in Slave mode otherwise runs in Master.


runMaster Method :

Handles the master mode functionality with managing received data,handles signals and broadcasting messages.

Creates a list starts with appending the initial data.
The line byte[] buffer = new byte[1024] allocate a fixed sized array for store UDP packet data.

Then starts listening the data and extracts the data from datagram packet using:

String receivedData = new String(packet.getData(), 0, packet.getLength());

packet.getData() : retrieves the array contains raw byte data.

Data is converted to String for easier interpretation.

Parsing Integer number receivedValue = Integer.parseInt(receivedData) and if it's not valid sends NumberFormatException.

if recivedValue == 0 : compute the average for given integers using computeAverage(List<Integer> numbers) method
if recivedValue == -1 : terminate the program
else : display the receivedValue and add it to the list


runSlave Method :

Creates a datagramSocket without binding a specific port for UDP communication.
try (DatagramSocket socket = new DatagramSocket())
The method gerBroadcastAddress() dynamically determines the broadcast address of local network. Ensures Slave can send packets master in same network.

Then prepares the message to send converts Slave's integer number to String.
Encapsulates to the packet data , length of the data , broadcast address and port number :
DatagramPacket packet = new DatagramPacket(data, data.length, broadcastAddress, masterPort);
Sends the packet using socket.send() method.
Displays the Data that has sent and Port number of the Master.


computeAverage(List<Integer> numbers):

Purpose: Computes the average of integers received by the MASTER.

Details:

  • Iterates over the list of integers, excluding any 0 values.
  • Calculates the sum and the count of valid numbers.
  • Returns the average or 0 if no valid numbers are present.

broadcastMessage(DatagramSocket socket, int value):

Purpose: Sends a message to all SLAVE nodes via UDP broadcast.

Details:

  • Converts the integer value into a byte array.
  • Determines the broadcast address dynamically using getBroadcastAddress.
  • Sends the message to the broadcast address on the specified port.
  • Logs the broadcast operation.

getBroadcastAddress():

Purpose: Dynamically identifies the network's broadcast address.

Details:

  • Iterates through available network interfaces.
  • Excludes loopback and inactive interfaces.
  • Returns the first valid broadcast address or throws an exception if none are found

Difficulties And Known Errors :

Difficulty that I encounter with is setting the broadcastMessage method and indemnifying networks address.

I find a solution with creating getBroadcastAddress method and dynamically identifying networks address instead of using 'localhost' .

Know Errors :

There is no known error that I encounter.

About

DAS (Distributed Average System), project is a master-slave communication system implemented using UDP protocol.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages