Thursday, 13 December 2012


Synchronized Clock

PROBLEM STATEMENT


Write a program to demonstrate working of Synchronized Clocks. The program contains to client process and one server which host a Synchronized Clock. The client process randomly sends messages to the server. The server maintains a log of messages and the times at which they were sent. The messages should finally display the messages accepted, discarded and the valid times (G)
at the moment of receipt of messages.   

DESCRIPTION


Synchronization
Two clocks are said to be synchronized at a particular instance of time if the clock skew of the two clocks is less than some specified constant d
A set of clocks are said to be synchronized if the clock skew of any two clocks in this set is less than d

Every message carries a connection identifier and timestamp.
For each connection, the server records in a table the most recent timestamp it has seen.
If any incoming message for a connection is lower than the timestamp stored for that connection, the message is rejected as duplicate.
The global Variable is
        G = Current Time – MaxLifeTime – MaxClockSkew.

SOURCE PROGRAM


SCServer.java
import java.io.*;
import java.net.*;
import java.sql.*;

public class SCServer
{
    public static void main(String args[])throws Exception
    {
        InetAddress lclhost;
        lclhost=InetAddress.getLocalHost();
        long maxtime,skewtime,datatime;
        String maxtimestr,skewtimestr;
        BufferedReader br;

        ClntServer ser=new ClntServer(lclhost);
       
        System.out.println("Enter the maximum time");
        br = new BufferedReader(new InputStreamReader(System.in));
        maxtimestr=br.readLine();
        System.out.println("Enter the maximum skew time");
        br = new BufferedReader(new InputStreamReader(System.in));
        skewtimestr=br.readLine();
        maxtime=Long.parseLong(maxtimestr);
        skewtime=Long.parseLong(skewtimestr);
       
        while(true)
        {
            datatime = System.currentTimeMillis();
            long G = datatime-maxtime-skewtime;
            System.out.println("G ="+G);
            ser.setTimeStamp(new Timestamp(G));
            ser.recPort(8001);
            ser.recData();
        }           
    }
}

class ClntServer
{
    InetAddress lclhost;
    int recport;
    Timestamp obtmp;

    ClntServer(InetAddress lclhost)
    {
        this.lclhost = lclhost;
    }
   
    void recPort(int recport)
    {
        this.recport = recport;
    }
       
    void setTimeStamp(Timestamp obtmp)
    {
        this.obtmp = obtmp;
    }

    void recData()throws Exception
    {
        String msgstr="";
        DatagramSocket ds;
        DatagramPacket dp;
        BufferedReader br;
        byte buf[] = new byte[256];       

        ds = new DatagramSocket(recport);
        dp = new DatagramPacket(buf,buf.length);
        ds.receive(dp);
        ds.close();

        msgstr = new String(dp.getData(),0,dp.getLength());
        System.out.println(msgstr);

        Timestamp obtmp = new Timestamp(Long.parseLong(msgstr));

        if(this.obtmp.before(obtmp) == true)
        {
            System.out.println("The Message is accepted");
        }
        else
        {
            System.out.println("The Message is rejected");   
        }
    }
}

SCClient.java
import java.io.*;
import java.net.*;

public class SCClient
{
    public static void main(String args[])throws Exception
    {
        InetAddress lclhost;
        lclhost=InetAddress.getLocalHost();
        while(true)
        {
            Client cntl=new Client(lclhost);
            cntl.sendPort(9001);
            cntl.sendData();
        }
    }
}

class Client
{
    InetAddress lclhost;
    int senport;

    Client(InetAddress lclhost)
    {
        this.lclhost=lclhost;
    }
    void sendPort(int senport)
    {
        this.senport=senport;
    }
   
    void sendData()throws Exception
    {
         DatagramPacket dp;
        DatagramSocket ds;
        BufferedReader br;
        br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the data");
        String str=br.readLine();
        ds = new DatagramSocket(senport);
        dp = new DatagramPacket(str.getBytes(),str.length(),lclhost,senport-1000);
        ds.send(dp);
        ds.close();
    }
 }              


OUTPUT

SCServer Output

E:\pracs\DCpracs\SynClk_prac4>java SCServer
Enter the maximum time
132
Enter the maximum skew time
45
G =1170980369291
33467678
The Message is rejected
G =1170980376869
22
The Message is rejected
G =1170980379541
5555555553333333333
The Message is accepted
G =1170980383119

SCClient OutPut

E:\pracs\DCpracs\SynClk_prac4>java SCClient
Enter the data
33467678
Enter the data
22
Enter the data
5555555553333333333

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment