0

I'm creating a token ring with sensors where every sensor is a process apart. When i start a sensor it communicates with the gateway and gets the list of the actual sensors already on the system . The problem is that every time i start a new process i want every already existing sensor to get the updated list, so to understand that other sensors have been added and the list is no longer the one they had but a new updated one.(So lets say the processes must always have the same list). I use a server which i call serverSocket which listens for messages. I can make it possible for the server to understand that the list has been changed but what i cant do is how to change the value of the sensorList found on my SensorClient class, to be updated? In the code bellow i show what i'm doing but the sensorList keeps being the old one,not being updated :/ Can anyone please help me? Thank you :)

SensorClient where i start a new process sensor

 public class SensorClient {
    public static void main(String[] args) throws Exception {
    Sensor sensor = new      Sensor(type,identificator,portnumber,ipnumber,gatewayAddr,timestamp);
     Gson gson = new Gson();
      String message = gson.toJson(sensor);
       Client c = Client.create();
       WebResource r = c.resource("http://localhost:9999/gateway/");
       ClientResponse response = r.path("sensors/add").type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, message);

 if (response.getStatus() == 200) {
       repeat = false;
      Type collectionType = new TypeToken<ArrayList<Sensor>>(){}.getType();
     ArrayList<Sensor> sensorList =    gson.fromJson(response.getEntity(String.class), collectionType);
     System.out.println("Starting the sensor ...");
     System.out.println("Push exit when you want to delete the sensor!");
     int position = 0;
     for(int i = 0; i< sensorList.size();i++){    if(sensorList.get(i).getIdentificator().equalsIgnoreCase(sensor.getIdentificator()) ) position = i;
}
sensors.Sensor.simulation(type, identificator);// special thread for sensors simulations
createSensor.getInstance().setPrevNextWhenAdd(position,sensorList);

serverSocket serverSocket = new serverSocket(portnumber,sensorList,position,sensorList.get(position).getNext());
 serverSocket.start();

StopSensor stopSensor = new StopSensor(identificator,portnumber,position,sensorList);
stopSensor.start();

oneSensor s = new oneSensor(portnumber,sensorList);
 s.start();
 } else {
          repeat = true;
          count +=1;
          System.out.println("Error. Wrong data! ");
                }
              }
            while (repeat );
        }
    }
                            }

The serverSocket thread

public class serverSocket extends Thread {
    public int port,nextPort;
    ArrayList<gateway.Sensor> sensorList;
    public static int position;
    public serverSocket(int port, ArrayList<gateway.Sensor> sensorList,int position,int nextPort) {
    this.port = port;
    this.nextPort=nextPort;
    this.sensorList= sensorList;
    this.position=position;}
    public void run() {
            ServerSocket welcomeSocket;
            Socket connectionSocket;
            try {
                welcomeSocket = new ServerSocket(port);
                while (true) {
                    connectionSocket = welcomeSocket.accept();

                    receivedMessages thread = new receivedMessages(connectionSocket,sensorList,position,nextPort);
                    thread.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println("Error!!!!!!!!!");
            }
        }
}

The receivedMessages thread

    public class receivedMessages extends Thread {

        private BufferedReader inFromClient;
        private Socket connectionSocket;
        ArrayList<gateway.Sensor> sensorList;
        int position,nextPort;
        public receivedMessages(Socket socket, ArrayList<gateway.Sensor> sensorList,int position,int nextPort){
                connectionSocket = socket;
                this.sensorList=sensorList;
                this.position=position;
                this.nextPort=nextPort;
 try {
      inFromClient = new BufferedReader( new InputStreamReader(connectionSocket.getInputStream()));
                   } catch (IOException e) { e.printStackTrace(); }
                }
@Override
public void run() {

try {

     String message = (inFromClient.readLine().toString());
     if (message.startsWith("Next") || message.startsWith("Previous")) {
     System.out.println(message);
     } else if (message.startsWith("The")) {
     System.out.println(message);                        createSensor.getInstance().setPrevNextWhenDelete(position, sensorList);
     } else  {// i receive the message that the list has changed
     System.out.println(message);
     sensorList = createSensor.getInstance().getSensorList();
     System.out.println("Updated " + sensorList);}

This class has methods used by gateway to register a sensor when it makes the request

public class createSensor {

  private static createSensor instance = null;
  private ArrayList<Sensor> sensor = new ArrayList<>();
  public int position, prevPosition, nextPosition, prevPort, nextPort;

 private createSensor() { } 
 public static synchronized createSensor getInstance() { 
        if (instance == null) {
            instance = new createSensor();
        }
        return instance;
    }
 public synchronized ArrayList insertSensor(String type, String identificator, int port, String id, String gatwayAddr, long timestamp) throws IOException {

sensor.add(new Sensor(type, identificator, port, id, gatwayAddr, timestamp));
                    return new ArrayList<>(sensor); // 
                }
            }
 public synchronized boolean hasMeasurements() {
     while (InnerBuffer.getInstance().emptyInnerBuffer())
                    return false;
                return true;
            }

public synchronized void setPrevNextWhenDelete(int position,ArrayList<Sensor> sensorList) throws IOException {
        //code
            }

public synchronized ArrayList<Sensor> getSensorList() {
                return new ArrayList<>(sensor);
            }

public synchronized int size() {
          return sensor.size();
            }

 public synchronized String returnRecentMeasurement (String id){
   String recentMeasurement=null;
      for (Sensor sensori : sensor) {
       if (sensori.getIdentificator().equalsIgnoreCase(id))
          recentMeasurement= InnerBuffer.getInstance().returnRecentMeasurements(id);
                    else
                        recentMeasurement = null;}
                return recentMeasurement;
            }
public synchronized void  setPrevNextWhenAdd() throws IOException {  //some other code where int position, prevPosition, nextPosition, prevPort, nextPort get their values. }}
Mimian
  • 11
  • 6
  • Is this a duplicate of http://stackoverflow.com/questions/9898066/java-push-from-server-to-clients? Also, take a look at [Java Message Service](https://en.wikipedia.org/wiki/Java_Message_Service) as a solution. – Ted Hopp Sep 12 '16 at 17:13
  • i would use some kind of distributed memory, for example Hazelcast or Infinispan – MGorgon Sep 13 '16 at 17:17
  • @MGorgonc thank u for answering. actually after reading this i saw some tutorial but for my java level it looks kinda difficult, is there any other way more simple? – Mimian Sep 13 '16 at 18:20
  • This is just too long a question. Can you reduce it in scope to make it easier to answer? – Gray Sep 14 '16 at 00:45
  • @Gray hey thanks for answering, i actually resolved the problem :) – Mimian Sep 15 '16 at 21:11
  • That's good. You should either delete this question or answer it yourself @Mimian. – Gray Sep 16 '16 at 03:56

0 Answers0