Imprimer
Catégorie : Embedded Systems
Affichages : 49

Preamble


  • These exercises allow to "recreate" a server that has functionalities similar to the server used for micro-controllers exercises.
  • In order to test the server without implementing the µC part, a client application is also needed.

 

1°/ Server part

 

1.1°/ Setup the project

public class Measure {

    private double value;
    private int type;
    public static final int UNSET = 0;
    public static final int VOLTAGE = 1;
    public static final int DURATION = 2;

    public Measure(double value, int type) {
        this.value = value;
        if ((type != VOLTAGE) && (type != DURATION)) {
            type = UNSET;
        }
        this.type = type;
    }

    public Measure() {
        this.value = 0;
        this.type = UNSET;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}
 
import java.util.*;

class ServerData {

    // HashMap key is a client id
    HashMap<Integer, ArrayList<Measure> > measures;

	public ServerData() {
		measures = new HashMap<Integer, ArrayList<Measure> >();
	}

	public boolean exists(int id) {
		if (measures.containsKey(id)) {
			return true;
		}
		return false;
	}

	public void registerClient(int id) {
        // create a list for the new client
		ArrayList<Measure> mes = new ArrayList<>();
        // add that list to the map
		measures.put(id,mes);
	}

	public void addMeasure(int id, double value, int type) {
        // get the List for the given client id
		ArrayList<Measure> mes = measures.get(id);
        // add a new Measure to that list
		mes.add(new Measure(value, type));
	}

	public double averageValue(int id, int measureType) {
		/* TO FULFILL:
		  for a given client id, compute the average of all values stored in measures but only for the given type of measure
                  NB: if measureType is = 0 (UNSET), return 0.0
		*/
	}

	public double minMaxValue(int measureType, int minOrMax) {
		/* TO FULFILL:
		  compute the minimum/maximum value stored in measures but only for the given type of measure.
                  If minOrMax = 0, computes the minimum, otherwise the maximum
		*/
	}

	public double valueCloseTo(int id, double v, double epsilon, int measureType  ) {
		/* TO FULFILL:
		  for a given client id, find the first value stored in measures, for the given type, that is close to v, with a precision of epsilon (i.e. (v-found value) < epsilon )
		*/
	}
}

 

  • The above class is used by the server to store data transmitted by clients using some particular requests

1.2°/ Writing the server

import java.io.*;
import java.net.*;
import java.util.*;
 
class MainServer {
 
    ServerSocket sockConn; Socket sockComm;
    PrintStream ps; BufferedReader br;
    ServerData data;
    int idClient;
 
    MainServer(int port) throws IOException {
        sockConn = new ServerSocket(port);
        data = new ServerData();
        idClient = 0; // incremented at each connection
    }
    ...
}
 

 

 

2°/ Client part
 
 

Create files named MainClient.java and ESP8266Client.java according to the template of the courses

Remark : the client application may implement a variable number of checks on what is typed by the user. For example, it could check if request parameters have valid values. But in practice, no checks are done by the client, assuming that it is in charge of the server to test everything, and if something is not valid, to send back an error.

 

3°/ Add-ons

 

 

4°/ Testing with a µC