Commit 13300e01b46c7822d94915ffaeac706db106829f

Authored by Notmoo-PC\Notmoo
1 parent 1e8c5c71

Clear code + ajout commentaire

src/main/java/core/ServerApp.java deleted
1   -import core.dao.HibernateDao;
2   -import core.repository.AccessPoint;
3   -import core.repository.Map;
4   -
5   -/**
6   - * Created by Guillaume on 09/05/2017.
7   - */
8   -public class ServerApp {
9   - public static void main(String args[]){
10   - HibernateDao dao = new HibernateDao();
11   - AccessPoint ap = new AccessPoint();
12   - ap.setMac_addr("aa:aa:aa:aa:aa:aa:aa:aa");
13   - dao.saveAccessPoint(ap);
14   -
15   - Map map = new Map();
16   - dao.saveMap(map);/**/
17   - }
18   -}
src/main/java/core/dao/DebianDao.java deleted
1   -package core.dao;
2   -
3   -/**
4   - * Created by Guillaume on 09/05/2017.
5   - */
6   -public class DebianDao {
7   -
8   - public String getMacAddr(String ipAddr){
9   - //TODO
10   - return null;
11   - }
12   -}
src/main/java/core/dao/HibernateDao.java
... ... @@ -12,13 +12,24 @@ import java.util.List;
12 12  
13 13  
14 14 /**
  15 + * Dao class handling access to database's data using Hibernate
  16 + *
15 17 * Created by Guillaume on 09/05/2017.
16 18 */
17 19 public class HibernateDao {
18 20  
19 21 public HibernateDao () {
20 22 }
21   -
  23 +
  24 + /**
  25 + * Execute a ITransactionProcess containing an Hibernate transaction,
  26 + * and return a callback object containing a list of results filled by
  27 + * the transactioin process and a boolean status.
  28 + *
  29 + * Handle properly Hibernate session's opening and closing, and handle exceptions that can occur.
  30 + * @param itp transaction process to run
  31 + * @return {@link TransactionCallBack}
  32 + */
22 33 private TransactionCallBack execTransactionProcess(ITransactionProcess itp) {
23 34 Session session = HibernateUtil.getSessionFactory().openSession();
24 35 TransactionCallBack reply = new TransactionCallBack();
... ... @@ -48,9 +59,18 @@ public class HibernateDao {
48 59 }
49 60 return reply;
50 61 }
51   - private <T> List<T> internal_getData(Class<T> clazz){
  62 +
  63 + /**
  64 + * Generic method that return a list of objects that
  65 + * are contained in the database handled by Hibernate.
  66 + *
  67 + * @param clazz class of elements requests
  68 + *
  69 + * @return a list of objects of class {@code clazz}
  70 + */
  71 + private <T extends IJavaBean> List<T> internal_getData(Class<T> clazz){
52 72 TransactionCallBack callBack = execTransactionProcess((session)->{
53   - TransactionCallBack reply = new TransactionCallBack<T>();
  73 + TransactionCallBack reply = new TransactionCallBack<IJavaBean>();
54 74 Query query = session.createQuery("from "+clazz.getSimpleName());
55 75 List<Object> results = query.list();
56 76 for(Object result : results){
... ... @@ -61,6 +81,12 @@ public class HibernateDao {
61 81 });
62 82 return callBack.getResults();
63 83 }
  84 +
  85 + /**
  86 + * Generic method that push javabeans to the database using Hibernate.
  87 + * @param objs
  88 + * @return status, true if the transaction is successful, false otherwise
  89 + */
64 90 private boolean internal_saveData(Object... objs){
65 91 TransactionCallBack callBack = execTransactionProcess((session)->{
66 92 TransactionCallBack reply = new TransactionCallBack();
... ... @@ -72,10 +98,21 @@ public class HibernateDao {
72 98  
73 99 return callBack!=null && callBack.isStatus();
74 100 }
75   -
  101 +
  102 + /**
  103 + *
  104 + * @param trs
  105 + * @return
  106 + */
76 107 public boolean saveTempRssi(TempRssi... trs){
77 108 return internal_saveData(trs);
78 109 }
  110 +
  111 + /**
  112 + *
  113 + * @param macAddr
  114 + * @return
  115 + */
79 116 public List<TempRssi> getTempRssi(String macAddr){
80 117 if(macAddr == null)
81 118 return new ArrayList<>();
... ... @@ -93,13 +130,28 @@ public class HibernateDao {
93 130 });
94 131 return callBack.getResults();
95 132 }
96   -
  133 +
  134 + /**
  135 + *
  136 + * @param rssis
  137 + * @return
  138 + */
97 139 public boolean saveRssiRecord(RssiRecord... rssis){
98 140 return internal_saveData(rssis);
99 141 }
  142 + /**
  143 + *
  144 + * @return
  145 + */
100 146 public List<RssiRecord> getRssiRecord(){
101 147 return internal_getData(RssiRecord.class);
102 148 }
  149 +
  150 + /**
  151 + *
  152 + * @param locationID
  153 + * @return
  154 + */
103 155 public List<RssiRecord> getRssiRecord(Integer locationID){
104 156 if(locationID == null)
105 157 return getRssiRecord();
... ... @@ -117,19 +169,44 @@ public class HibernateDao {
117 169 });
118 170 return callBack.getResults();
119 171 }
120   -
  172 +
  173 + /**
  174 + *
  175 + * @return
  176 + */
121 177 public List<Location> getLocations(){
122 178 return internal_getData(Location.class);
123 179 }
124   -
  180 +
  181 + /**
  182 + *
  183 + * @param aps
  184 + * @return
  185 + */
125 186 public boolean saveAccessPoint (final AccessPoint... aps) {
126 187 return internal_saveData(aps);
127 188 }
  189 +
  190 + /**
  191 + *
  192 + * @return
  193 + */
128 194 public List<AccessPoint> getAccessPoint () {
129 195 return internal_getData(AccessPoint.class);
130 196 }
131   -
  197 +
  198 + /**
  199 + *
  200 + * @param map
  201 + * @return
  202 + */
132 203 public boolean saveMap(final Map map){return internal_saveData(map);}
  204 +
  205 + /**
  206 + *
  207 + * @param mapId
  208 + * @return
  209 + */
133 210 public Map getMap(int mapId){
134 211 TransactionCallBack callBack = execTransactionProcess((session)->{
135 212 TransactionCallBack reply = new TransactionCallBack<Map>();
... ... @@ -145,6 +222,11 @@ public class HibernateDao {
145 222 return (callBack.getResults().isEmpty()?null:(Map)callBack.getResults().get(0));
146 223 }
147 224  
  225 + /**
  226 + *
  227 + * @param apMacAddress
  228 + * @return
  229 + */
148 230 public List<AccessPoint> getAccessPoints(String apMacAddress) {
149 231 TransactionCallBack callBack = execTransactionProcess((session)->{
150 232 TransactionCallBack reply = new TransactionCallBack<AccessPoint>();
... ... @@ -163,7 +245,11 @@ public class HibernateDao {
163 245 return reply;
164 246 }
165 247  
166   -
  248 + /**
  249 + *
  250 + * @param location
  251 + * @return
  252 + */
167 253 public int saveLocation(Location location) {
168 254 TransactionCallBack callBack = execTransactionProcess((session)->{
169 255 TransactionCallBack<Integer> reply = new TransactionCallBack<Integer>();
... ... @@ -177,6 +263,12 @@ public class HibernateDao {
177 263 return (Integer)callBack.results.get(0);
178 264 return -1;
179 265 }
  266 +
  267 + /**
  268 + *
  269 + * @param Location
  270 + * @return
  271 + */
180 272 public Location getLocation(int Location) {
181 273 TransactionCallBack callBack = execTransactionProcess((session)->{
182 274 TransactionCallBack reply = new TransactionCallBack<Location>();
... ... @@ -192,11 +284,19 @@ public class HibernateDao {
192 284 return (callBack.getResults().isEmpty()?null:(Location)callBack.getResults().get(0));
193 285 }
194 286  
195   -
  287 + /**
  288 + * This functional interface's purpose is to define a common
  289 + * type to give process to internal methods.
  290 + */
196 291 private interface ITransactionProcess{
197 292 TransactionCallBack exec(Session tr);
198 293 }
199   -
  294 +
  295 + /**
  296 + * Class designed to contains results from transaction process.
  297 + *
  298 + * @param <T>
  299 + */
200 300 private class TransactionCallBack<T>{
201 301 private boolean status;
202 302 private List<T> results;
... ...
src/main/java/core/repository/AccessPoint.java
... ... @@ -8,7 +8,7 @@ import javax.persistence.*;
8 8 */
9 9 @Entity
10 10 @Table(name ="ACCESS_POINT")
11   -public class AccessPoint implements Serializable{
  11 +public class AccessPoint implements IJavaBean{
12 12 @Id @GeneratedValue
13 13 private int id;
14 14  
... ...
src/main/java/core/repository/IJavaBean.java 0 โ†’ 100644
  1 +package core.repository;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +/**
  6 + * Common type shared by all javabeans created for this positioning server
  7 + *
  8 + * Created by Notmoo on 17/06/2017.
  9 + */
  10 +public interface IJavaBean extends Serializable{
  11 +}
... ...
src/main/java/core/repository/Location.java
... ... @@ -8,7 +8,7 @@ import java.io.Serializable;
8 8 */
9 9 @Entity
10 10 @Table(name = "LOCATION")
11   -public class Location implements Serializable {
  11 +public class Location implements IJavaBean {
12 12 @Id @GeneratedValue
13 13 private int id;
14 14 private Double x, y;
... ...
src/main/java/core/repository/Map.java
... ... @@ -8,7 +8,7 @@ import java.io.Serializable;
8 8 */
9 9 @Entity
10 10 @Table(name = "MAP")
11   -public class Map implements Serializable{
  11 +public class Map implements IJavaBean{
12 12 @Id @GeneratedValue
13 13 private int id;
14 14 private Double x_topLeft, y_topLeft;
... ...
src/main/java/core/repository/RssiRecord.java
... ... @@ -8,7 +8,7 @@ import java.io.Serializable;
8 8 */
9 9 @Entity
10 10 @Table (name = "RSSI_RECORD")
11   -public class RssiRecord implements Serializable {
  11 +public class RssiRecord implements IJavaBean {
12 12 @Id @GeneratedValue
13 13 private int id;
14 14  
... ...
src/main/java/core/repository/TempRssi.java
... ... @@ -7,7 +7,7 @@ import javax.persistence.*;
7 7 */
8 8 @Entity
9 9 @Table (name = "TEMP_RSSI")
10   -public class TempRssi {
  10 +public class TempRssi implements IJavaBean{
11 11 @Id @GeneratedValue
12 12 private int id;
13 13  
... ...
src/main/java/core/service/CalibrationService.java
... ... @@ -8,6 +8,9 @@ import core.repository.RssiRecord;
8 8 import java.util.List;
9 9  
10 10 /**
  11 + * CalibrationService is a service class providing a way to register new calibration datas and new locations,
  12 + * that can be used afterwards to compute locations.
  13 + *
11 14 * Created by Guillaume on 09/05/2017.
12 15 */
13 16 public class CalibrationService {
... ...
src/main/java/core/service/MapService.java
... ... @@ -4,6 +4,9 @@ import core.dao.HibernateDao;
4 4 import core.repository.Map;
5 5  
6 6 /**
  7 + * MapService is a service class providing methods to access map data
  8 + * stored in the database according to a map id.
  9 + *
7 10 * Created by Guillaume on 29/05/2017.
8 11 */
9 12 public class MapService {
... ...
src/main/java/core/service/MeasurementService.java
... ... @@ -7,6 +7,10 @@ import core.repository.TempRssi;
7 7 import java.util.List;
8 8  
9 9 /**
  10 + * MesaurementService is a service class providing methods to register positioning data.
  11 + * One shouldn't use this service to registrer calibration data, as a dedicated service class
  12 + * has been made to handle this kind of registrations.
  13 + *
10 14 * Created by Guillaume on 29/05/2017.
11 15 */
12 16 public class MeasurementService {
... ... @@ -16,7 +20,13 @@ public class MeasurementService {
16 20 public MeasurementService(){
17 21 dao = new HibernateDao();
18 22 }
19   -
  23 +
  24 + /**
  25 + * Register into the database a measured positioning data.
  26 + * @param apMacAddr mac address of access point that measured the data
  27 + * @param clientMacAddr mac address of client that has been use as base for measurement
  28 + * @param val value measured
  29 + */
20 30 public void registerMeasurement(String apMacAddr, String clientMacAddr, double val){
21 31 List<AccessPoint> aps = dao.getAccessPoints(apMacAddr);
22 32  
... ...
src/main/java/core/service/PositioningService.java
1 1 package core.service;
2 2  
3   -import core.dao.DebianDao;
4 3 import core.dao.HibernateDao;
5 4 import core.repository.Location;
6 5 import core.repository.RssiRecord;
... ... @@ -10,24 +9,23 @@ import java.util.ArrayList;
10 9 import java.util.List;
11 10  
12 11 /**
  12 + * PositioningService is a service class designed to provide methods to compute one's location using measured data.
  13 + *
  14 + * To register measured data, please use {@link MeasurementService}. To register calibration data, please use {@link CalibrationService}
13 15 * Created by Guillaume on 09/05/2017.
14 16 */
15 17 public class PositioningService {
16 18  
17   - private static final double DEFAULT_POSITIONING_PRECISION = 7.5;
18   -
19 19 private HibernateDao hibDao;
20   - private DebianDao debDao;
21 20  
22 21 public PositioningService () {
23   - this.debDao = new DebianDao();
24 22 this.hibDao = new HibernateDao();
25 23 }
26 24  
27   - public Location getLocation (String ipAddr) {
  25 + public Location getLocation (String macAddr) {
28 26  
29 27 //Liste des RSSISample associรฉ ร  un ap, mesurรฉ pour un client
30   - List<TempRssi> clientRssi = hibDao.getTempRssi(debDao.getMacAddr(ipAddr));
  28 + List<TempRssi> clientRssi = hibDao.getTempRssi(macAddr);
31 29  
32 30 if(clientRssi.size()>=3) {
33 31  
... ...
src/main/java/core/utils/HibernateUtil.java
... ... @@ -3,6 +3,9 @@ package core.utils;
3 3 import org.hibernate.SessionFactory;
4 4 import org.hibernate.cfg.AnnotationConfiguration;
5 5  
  6 +/**
  7 + * Singleton class granting access to Hibernate's session, built from annotations and hibernate config file
  8 + */
6 9 public class HibernateUtil {
7 10  
8 11 private static final SessionFactory sessionFactory = buildSessionFactory();
... ...
src/main/java/servlet/APCalibrationServlet.java
... ... @@ -10,6 +10,8 @@ import javax.servlet.http.HttpServletResponse;
10 10 import java.io.IOException;
11 11  
12 12 /**
  13 + * Servlet designed to receive registration requests about calibration data.
  14 + *
13 15 * Created by Guillaume on 07/06/2017.
14 16 */
15 17 public class APCalibrationServlet extends HttpServlet {
... ...
src/main/java/servlet/APMeasurementServlet.java
... ... @@ -9,6 +9,8 @@ import javax.servlet.http.HttpServletResponse;
9 9 import java.io.IOException;
10 10  
11 11 /**
  12 + * Servlet designed to receive registration requests about positioning data.
  13 + *
12 14 * Created by Guillaume on 29/05/2017.
13 15 */
14 16 public class APMeasurementServlet extends HttpServlet {
... ...
src/main/java/servlet/CalibrationServlet.java
... ... @@ -14,6 +14,8 @@ import java.net.InetAddress;
14 14 import java.util.Random;
15 15  
16 16 /**
  17 + * Servlet handling calibration request coming from clients.
  18 + *
17 19 * Created by Guillaume on 15/05/2017.
18 20 */
19 21 public class CalibrationServlet extends HttpServlet{
... ... @@ -60,10 +62,15 @@ public class CalibrationServlet extends HttpServlet{
60 62 execute(servletRequest, servletResponse);
61 63 }
62 64  
  65 + /**
  66 + * Method broadcasting UPD request to AP, request asking fo calibration data.
  67 + * @param clientMacAddress mac address of client on which ap's measurement will be based
  68 + * @param locationId id of corresponding location in database
  69 + */
63 70 private void multicastCalibrationRequest(String clientMacAddress, int locationId){
64 71 try (DatagramSocket socket = new DatagramSocket(4445)){
65 72  
66   - byte[] buf = new byte[256];
  73 + byte[] buf;
67 74  
68 75 String dString = "{\"CALIB\": \"" + clientMacAddress + "\", \"SERV\": \"" + Inet4Address.getLocalHost().getHostAddress()+"\", \"LOC_ID\": \""+locationId+"\"}";
69 76 buf = dString.getBytes();
... ...
src/main/java/servlet/MapByteServlet.java
... ... @@ -10,6 +10,8 @@ import javax.servlet.http.HttpServletResponse;
10 10 import java.io.IOException;
11 11  
12 12 /**
  13 + * Servlet designed to access to map image bytes. This kind of request is usually sent by clients.
  14 + *
13 15 * Created by Guillaume on 29/05/2017.
14 16 */
15 17 public class MapByteServlet extends HttpServlet {
... ...
src/main/java/servlet/MapScaleServlet.java
... ... @@ -10,6 +10,8 @@ import javax.servlet.http.HttpServletResponse;
10 10 import java.io.IOException;
11 11  
12 12 /**
  13 + * Servlet designed to access to map scale, which can be use to convert real-life coordinates to on-screen coordinates. This kind of request is usually sent by clients.
  14 + *
13 15 * Created by Guillaume on 29/05/2017.
14 16 */
15 17 public class MapScaleServlet extends HttpServlet {
... ...
src/main/java/servlet/PositioningServlet.java
... ... @@ -16,6 +16,8 @@ import java.net.InetAddress;
16 16 import static java.lang.Thread.sleep;
17 17  
18 18 /**
  19 + * Servlet handling positioning request coming from clients.
  20 + *
19 21 * Created by Guillaume on 15/05/2017.
20 22 */
21 23 public class PositioningServlet extends HttpServlet{
... ... @@ -30,10 +32,10 @@ public class PositioningServlet extends HttpServlet{
30 32 multicastPositioningRequest(clientMacAddr);
31 33  
32 34  
33   - Location loc = null;
  35 + Location loc;
34 36 do{
35 37 try{
36   - sleep(500l);
  38 + sleep(500L);
37 39 } catch (InterruptedException e) {
38 40 e.printStackTrace();
39 41 }
... ... @@ -56,11 +58,16 @@ public class PositioningServlet extends HttpServlet{
56 58 throws ServletException, IOException {
57 59 service(servletRequest, servletResponse);
58 60 }
59   -
  61 +
  62 + /**
  63 + * Method broadcasting UPD request to AP, request asking for positioning data.
  64 + *
  65 + * @param clientMacAddress mac address of client on which ap's measurement will be based
  66 + */
60 67 private void multicastPositioningRequest(String clientMacAddress){
61 68 try (DatagramSocket socket = new DatagramSocket(4445)){
62 69  
63   - byte[] buf = new byte[256];
  70 + byte[] buf;
64 71  
65 72 String dString = "{\"LOCATE\": \"" + clientMacAddress + "\", \"SERV\": \"" + Inet4Address.getLocalHost().getHostAddress()+"\"}";
66 73 buf = dString.getBytes();
... ...
src/main/resources/hibernate.cfg.xml
... ... @@ -10,7 +10,7 @@
10 10 <property name="connection.username">lo53_user</property>
11 11 <property name="connection.password">lo53</property>
12 12 <property name="connection.pool_size">1</property>
13   - <property name="hibernate.hbm2ddl.auto">create</property>
  13 + <property name="hibernate.hbm2ddl.auto">update</property>
14 14  
15 15 <mapping class="core.repository.AccessPoint"/>
16 16 <mapping class="core.repository.Location"/>
... ...