Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Using HTTP Sockets with java objects

Hi,
I have modelled some java classes. The main idea is there is a car service center having stations. Each station does a task (like installing brakes, installing axells ...etc).
When a car arrives a task list is created and assigned to first station. which does it's task
and moves the car to next station. I have implemented the classes. The part where I am stuck is the line in the requirement which says :  jobs move from station to station via HTTP sockets. The method of transfer is a design decision you can make.

 intermediate stations accept and push jobs to other stations. For the simulation to be realistic, the processing should take a reasonable amount of time. This can be simulated using the Thread.sleep() static method.
 

Not sure how will HTTP sockets come in this simple program to transfer jobs from one station to next.
Any suggestions or pointers ?
Thanks a lot
 
 
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

So it will be like each station will be running in a thread of it's own. And these threads will communicate with each other using TCP sockets.
Also as you say there are no HTTP sockets in Java. Could we use some library to achieve that ?
Or it's absurd to use HTTP sockets in the current code scenario ?
As these are all threads spawned on a single machine by a single process
There are such things as Websockets, but I'm not familiar with them. Maybe you meant that?
If I use TCP sockets to communicate. Then what I understand each station will have a serversocket for listening to incoming connection. And so a station could send a message to the next station using sockets with port for the next station. Is that correct ?
I am not sure. It's part of a java assignment. May be I need to clarify from prof if he meant Websockets. But in the assignment it's mentioned HTTP sockets
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1.)
I understood what you are trying to convey.
I am putting it in my own words -

So there will only one server. Each station will maintain a queue of jobs.
And will continue processing jobs in a loop
(kind of while(true))
Once a job is processed this station will communicate it to the server.
Now server will figure out which is the next station this job should go and so put it in the queue of that station.

Do you see any flaw in the approach above.

2.) I clarified, it was to be done using TCP sockets only.

3.)
As java doesn't have HTTP sockets.
But it has TCP sockets. Is it possible to implement HTTP on top of it ?
Although I won't be doing it for this project. But I get a feeling I could learn a lot by experimenting with it and extending it



HTTP is a subset if TCP/IP. You can read up on Java’s HttpURLConnection class.

But as was mentioned by Stephan, you should get your architecture and model outlined concretely first.
Each station will maintain a queue of jobs.
Only in a disconnected solution. Cause from the given information, I cannot exclude that two ore more stations can do the same job. Thus complex rescheduling would be necessary in a disconnected model. Where as in connected state, the station just processes the job it got. And it may display the possible next job.

Now server will figure out which is the next station this job should go and so put it in the queue of that station.
This is a strong indicator for a connected system. Cause in a disconnected, the route should be deterministic, thus precalulated.

But it has TCP sockets. Is it possible to implement HTTP on top of it ?
Clarify your needs. Either you need TCP/IP, then use sockets. When you need HTTP, the review your problem. Cause this implies a web service nowadays. Here you have the entire, necessary infrastructure already at hands in your IDE and used application server (JAX/Jersey, https://en.wikipedia.org/wiki/List_of_web_service_frameworks).
I figured out the requirements clearly as of now.
We need TCP sockets. I will describe the problem as per my current knowledge need suggestions if that is the right approach.
So there is an assembly line. And for simplicity lets assume for discussion that there will be only one station for each task. Although I will be implementing multiple parallel lines.
So each car goes from station to station in a linear way one after the other.
The way I am thinking of designing it is :
Now I have several station objects in a list (Which determines the order of stations) and when a Job is given it is passed from station to station. Instead of making a TCP socket between two station objects.
What I could do is make a StationManager class and so after a task is finished by a station it communicates it
to StationManager class which is running a sockerServer and this StationManager  pass the task to next stations.  So StationManager will be having a List<Station> so it will simply determine the next station object and call the process() function in the station.

I am not sure if there are any flaws in my reasoning. I am doing this type of stuff first time.
Hope I am making myself clear.
Any comments on the approach above ? Will it work out or there are better approaches ?





I think it's time that you start implementing it to see it in action. Important part: don't waste time on communications via TCP. that is just another layer. Start by implementing it as local instances, which interact with each other.

For the design: if you have really an assembly line consisting of stations, then this implies that you only to assign job packets to the line. And the line does the dispatching to the stations.
Hi,
Thanks for the advice. I did went ahead and implemented the system. And it seems to be working fine. But I need help with doing the communication using TCP sockets.

currently, the communication between stations and assemblyLineroute is happening like :
Station has an assemblyLineRoute object to which it belongs.
so station simply calls the method assemblyLineRoute.task completed() to indicate that
the task has been completed and the assemblyLineRoute maintains a queue of tasks to be processed.
assembly line routes each task from the queue to the proper stations.


I am not sure I am making myself clear here. But it looks this could be done through basic socket programming.
As I have never done it I feel restricted to being with.
Any pointers to it ?


I am posting a stripped off version containing only important methods for two classes AssemblyLineRoute and Station. Please provide any inputs how do I begin converting it into TCP sockets

package assignment1.assemblyline;

import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;

public class Station implements Runnable {
    
    private AssemblyLineRoute assemblyLineRoute;
    private AssemblyLine assemblyLine;
  
    @Override
    public void run() {
        logger.info("Task Started , task : " + task + "  ,station : " + this + " ,job : " + job);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        synchronized (DECREMENT_LOCKER) {
            decrementInventory();
        }
        //TODO: change this communication to TCP Sockets
        assemblyLineRoute.carProcessingDone(job, task, this);
        isFree = true;
        logger.info("Task Completed , task : " + task + "  ,station : " + this + " ,job : " + job);
    }
}



Open in new window

package assignment1.assemblyline;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class AssemblyLineRoute implements Runnable {
    private List<Station> stationsList;
    private ConcurrentLinkedQueue<CarTask> carTaskQueue;
    private List<Job> jobList;
    private ExecutorService threadExecutor = Executors.newCachedThreadPool();
    private int sleep = 1000; // time to sleep before processing jobTripletQueue

 
    public void carProcessingDone(Job job, Task task, Station station) {
        if (isLastStation(station)) {
               job.setFinished(true);
        } else {
            Task nextTask = job.getNextTask(task);
            Station nextStation = stationsList.get(stationsList.indexOf(nextTask));
            this.carTaskQueue.add(new CarTask(job, nextTask, nextStation));
        }
    }

    
    /**
     * This method is to run car task. 
     */
    @Override
    public void run() {
        for (Iterator<CarTask> it = carTaskQueue.iterator(); it.hasNext(); ) {
            CarTask carTask = it.next();
            Station station = carTask.getStation();
            if (station.isFree()) {
                it.remove();
                station.setFree(false);
                station.setTask(carTask.getTask());
                station.setJob(carTask.getJob());
                threadExecutor.execute(carTask.getStation());
            }
        }
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!areAllJobsDone())
            run();
        else {
            threadExecutor.shutdown();
        }
    }
}


Open in new window