LOCATION TRACKER

 

                                                    LOCATION TRACKER USING RMI


1. Interface – LocationService.java

import java.rmi.Remote;

import java.rmi.RemoteException;


public interface LocationService extends Remote {

    String updateLocation(String deviceId, double latitude, double longitude) throws RemoteException;

    String getLocation(String deviceId) throws RemoteException;

}


2. Implementation – LocationServiceImpl.java

import java.rmi.server.UnicastRemoteObject;

import java.rmi.RemoteException;

import java.util.HashMap;


public class LocationServiceImpl extends UnicastRemoteObject implements LocationService {

    private HashMap<String, String> locationMap;


    protected LocationServiceImpl() throws RemoteException {

        super();

        locationMap = new HashMap<>();

    }


    public String updateLocation(String deviceId, double latitude, double longitude) throws RemoteException {

        String location = "Latitude: " + latitude + ", Longitude: " + longitude;

        locationMap.put(deviceId, location);

        return "Location updated for " + deviceId;

    }


    public String getLocation(String deviceId) throws RemoteException {

        return locationMap.getOrDefault(deviceId, "Location not found for device: " + deviceId);

    }

}


3. Server – LocationServer.java

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;


public class LocationServer {

    public static void main(String[] args) {

        try {

            LocationService service = new LocationServiceImpl();

            Registry registry = LocateRegistry.createRegistry(1099);

            registry.rebind("LocationService", service);

            System.out.println("Location Service is running...");

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}


4. Client – LocationClient.java

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;


public class LocationClient {

    public static void main(String[] args) {

        try {

            Registry registry = LocateRegistry.getRegistry("localhost", 1099);

            LocationService service = (LocationService) registry.lookup("LocationService");


            System.out.println(service.updateLocation("Device123", 37.7749, -122.4194));  // San Francisco

            System.out.println(service.getLocation("Device123"));


        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

5.Expected Output

Server








Client



Comments

Popular posts from this blog

HEALTH ADMINISTRATION

LIBRARY MANAGEMENT SYSTEM