Meeting  1.1
BTS SNIR LaSalle Avignon 2021
Communication.java
Aller à la documentation de ce fichier.
1 package com.lasalle.meeting;
2 
3 import android.os.Bundle;
4 import android.os.Handler;
5 import android.os.Message;
6 import android.util.Log;
7 
8 import java.io.IOException;
9 import java.net.DatagramPacket;
10 import java.net.DatagramSocket;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.List;
14 import java.util.concurrent.LinkedBlockingQueue;
15 import java.util.concurrent.locks.ReentrantLock;
16 
29 public class Communication implements Runnable
30 {
34  private static final String TAG = "_Communication";
35 
39  private InetAddress adresseIP = null;
40  public final static String adresseMulticast = "239.0.0.42";
41  private final static int PORT = 5000;
42  public final static int TYPE_RECEPTION = 1;
43  private final ReentrantLock mutex = new ReentrantLock();
44  private DatagramSocket socket = null;
45  private LinkedBlockingQueue<DatagramPacket> queueEmission;
46  private Handler handler;
47 
51  public static final String DELIMITEUR_EN_TETE = "$";
52  public static final String DELIMITEUR_CHAMP = ";";
53  public static final String DELIMITEUR_FIN = "\r\n";
54  public static final int TRAME_INCONNUE = -1;
55  public static final int DEMANDE_INFORMATIONS = 1;
56  public static final int DEMANDE_DISPONIBILITE = 3;
57  public static final int MODIFICATION_INFORMATIONS = 1;
58  public static final int MODIFICATION_DISPONIBILITE = 3;
59  public static final int NB_CHAMPS_INFORMATIONS = 4;
60  public static final int NB_CHAMPS_DISPONIBILITE = 1;
61  public static final int NB_CHAMPS_DISPONIBILITE_CODE = 2;
62  public static final int NB_CHAMPS_DEMANDE_INFORMATIONS = 7;
63  public static final int NB_CHAMPS_DEMANDE_DISPONIBILITE = 1;
64  public static final int NB_CHAMPS_MODIFICATION_DISPONIBILITE = 2;
65  public static final int NB_CHAMPS_RETOUR_MODIFICATION_DISPONIBILITE = 3;
66  public static final int CHAMP_NOM = 0;
67  public static final int CHAMP_DESCRIPTION = 1;
68  public static final int CHAMP_LIEU = 2;
69  public static final int CHAMP_SUPERFICIE = 3;
70  public static final int CHAMP_DISPONIBILITE = 4;
71  public static final int CHAMP_INDICE_DE_CONFORT = 5;
72  public static final int CHAMP_TEMPERATURE = 6;
73  public static final int CHAMP_CODE = 1;
74 
79  public Communication(Handler handler)
80  {
81  this.handler = handler;
82 
83  try
84  {
85  socket = new DatagramSocket(PORT);
86  Log.d(TAG, "Création de la socket UDP sur le port " + PORT);
87  }
88  catch(Exception e)
89  {
90  e.printStackTrace();
91  }
92 
93  queueEmission = new LinkedBlockingQueue<DatagramPacket>();
94  }
95 
96  public Communication()
97  {
98  this.handler = null;
99 
100  try
101  {
102  socket = new DatagramSocket();
103  Log.d(TAG, "Création d'une socket UDP");
104  }
105  catch(Exception e)
106  {
107  e.printStackTrace();
108  }
109 
110  queueEmission = new LinkedBlockingQueue<DatagramPacket>();
111  }
112 
113  public void setHandler(Handler handler)
114  {
115  this.handler = handler;
116  }
117 
123  public void envoyer(String trame, String adressePortier)
124  {
125  if(socket == null || socket.isClosed())
126  return;
127 
128  Log.d(TAG, "envoyer() : adressePortier = " + adressePortier);
129 
130  final InetAddress adresseIPDistante;
131  try
132  {
133  adresseIPDistante = InetAddress.getByName(adressePortier);
134  }
135  catch (UnknownHostException e)
136  {
137  e.printStackTrace();
138  return;
139  }
140 
141  // Crée et démarre un thread pour envoyer la trame
142  new Thread()
143  {
144  @Override public void run()
145  {
146  byte[] emission = new byte[1024];
147 
148  try
149  {
150  emission = trame.getBytes();
151  DatagramPacket paquetRetour = new DatagramPacket(emission, emission.length, adresseIPDistante, PORT);
152  socket.send(paquetRetour);
153  Log.d(TAG, "envoyer() : " + trame + " à " + adresseIPDistante + ":" + PORT);
154  }
155  catch (IOException e)
156  {
157  e.printStackTrace();
158  Log.d(TAG, "Erreur émission !");
159  }
160  }
161  }.start();
162  }
163 
167  public void recevoir()
168  {
169  byte[] reception = new byte[1024];
170 
171  while (socket != null && !socket.isClosed())
172  {
173  try
174  {
175  final DatagramPacket paquetRecu = new DatagramPacket(reception, reception.length);
176  socket.receive(paquetRecu);
177  final String donnees = new String(paquetRecu.getData(), paquetRecu.getOffset(), paquetRecu.getLength());
178  Log.d(TAG, "Réception [" + paquetRecu.getAddress().getHostAddress() + ":" + paquetRecu.getPort() + "] -> " + donnees);
179 
180  if(verifierTrame(donnees))
181  {
182  // Envoie les données reçues à l'activité
183  envoyerMessage(TYPE_RECEPTION, paquetRecu.getAddress().getHostAddress(), paquetRecu.getPort(), donnees);
184  }
185  }
186  catch (Exception e)
187  {
188  e.printStackTrace();
189  Log.d(TAG, "Erreur réception !");
190  }
191  }
192 
193  Log.d(TAG, "recevoir()");
194  }
195 
199  private void envoyerMessage(int type, String adresse, int port, String donnees)
200  {
201  if(handler == null)
202  return;
203  Message msg = Message.obtain();
204  msg.what = type;
205  Bundle b = new Bundle();
206  b.putString("adresseIP", adresse);
207  b.putInt("port", port);
208  b.putString("donnees", donnees);
209  msg.setData(b);
210  mutex.lock();
211  handler.sendMessage(msg);
212  mutex.unlock();
213  Log.d(TAG, "envoyerMessage() -> handler.sendMessage()");
214  }
215 
221  public String fabriquerTrameDemande(int typeTrame)
222  {
223  /*
224  * Protocole
225  *
226  * Demande informations du portier :
227  * $GET;1\r\n
228  *
229  * Demande disponibilité du portier :
230  * $GET;3\r\n
231  */
232 
233  String trame = null;
234  Log.d(TAG, "fabriquerTrameDemande() type = " + typeTrame);
235 
236  switch(typeTrame)
237  {
239  trame = DELIMITEUR_EN_TETE + "GET;1" + DELIMITEUR_FIN;
240  break;
242  trame = DELIMITEUR_EN_TETE + "GET;3" + DELIMITEUR_FIN;
243  break;
244  default:
245  Log.d(TAG, "fabriquerTrameDemande() : type de trame inconnu !");
246  }
247 
248  Log.d(TAG,"fabriquerTrameDemande() trame = " + trame);
249 
250  return trame;
251  }
252 
258  public String fabriquerTrameModification(int typeTrame, List<String> parametres)
259  {
260  /*
261  * Protocole
262  *
263  * Actualiser les informations d’un portier :
264  * $SET;1;nomSalle;description;emplacement;surface\r\n
265  *
266  * Actualiser la disponibilité d’un portier :
267  * $SET;3;disponibilité\r\n
268  *
269  * Exemple d'utilisation :
270  * List<String> parametres = Arrays.asList("B11", "Salle de cours", "Batiment BTS", "25");
271  * String trame = communication.fabriquerTrameModification(Communication.MODIFICATION_INFORMATIONS, parametres);
272  */
273 
274  // Vérifications
275  if(parametres == null)
276  return null;
277 
278  if(parametres.size() < 1)
279  return null;
280 
281  Log.d(TAG, "fabriquerTrameModification() type = " + typeTrame);
282  Log.d(TAG, "fabriquerTrameModification() Nb parametres = " + parametres.size());
283  for(int i=0;i<parametres.size();++i)
284  {
285  Log.d(TAG, "fabriquerTrameModification() parametres[" + i + "] = " + parametres.get(i));
286  }
287 
288  String trame = null;
289 
290  switch(typeTrame)
291  {
293  if(parametres.size() != NB_CHAMPS_INFORMATIONS)
294  return null;
295  trame = DELIMITEUR_EN_TETE + "SET;1;" + parametres.get(CHAMP_NOM) + DELIMITEUR_CHAMP + parametres.get(CHAMP_DESCRIPTION) + DELIMITEUR_CHAMP + parametres.get(CHAMP_LIEU) + DELIMITEUR_CHAMP + parametres.get(CHAMP_SUPERFICIE) + DELIMITEUR_FIN;
296  break;
297 
299  if(parametres.size() >= NB_CHAMPS_DISPONIBILITE && parametres.size() <= (NB_CHAMPS_DISPONIBILITE+1))
300  {
301  if (parametres.get(0).equals("0"))
302  trame = DELIMITEUR_EN_TETE + "SET;3;" + parametres.get(0) + DELIMITEUR_FIN;
303  else
304  trame = DELIMITEUR_EN_TETE + "SET;3;" + parametres.get(0) + ";" + parametres.get(1) + DELIMITEUR_FIN;
305  }
306  break;
307 
308  default:
309  Log.d(TAG, "fabriquerTrameModification() : type de trame inconnu !");
310  }
311 
312  Log.d(TAG,"fabriquerTrameModification() trame = " + trame);
313 
314  return trame;
315  }
316 
321  public boolean verifierTrame(String trame)
322  {
323  Log.d(TAG, "verifierTrame() " + (trame.startsWith(DELIMITEUR_EN_TETE) && trame.endsWith(DELIMITEUR_FIN)));
324 
325  return (trame.startsWith(DELIMITEUR_EN_TETE) && trame.endsWith(DELIMITEUR_FIN));
326  }
327 
331  public void arreter()
332  {
333  if(socket == null)
334  return;
335  socket.close();
336  }
337 
341  @Override
342  public void run()
343  {
344  Log.d(TAG, "Démarre le thread réception");
345  recevoir();
346  }
347 
353  public static int recupererTypeTrame(String[] champs)
354  {
355  int typeTrame = Communication.TRAME_INCONNUE;
356 
357  switch(champs.length)
358  {
360  Log.d(TAG, "handleMessage() Trame DEMANDE_INFORMATIONS");
362  break;
364  Log.d(TAG, "handleMessage() Trame DEMANDE_DISPONIBILITE");
366  break;
368  Log.d(TAG, "handleMessage() Trame MODIFICATION_DISPONIBILITE");
370  }
371 
372  return typeTrame;
373  }
374 }
static final int NB_CHAMPS_DISPONIBILITE_CODE
Communication entre l&#39;application et le portier.
String fabriquerTrameDemande(int typeTrame)
Fabrique la trame de demande.
static final int MODIFICATION_INFORMATIONS
InetAddress adresseIP
Adresse IP du portier.
Communication(Handler handler)
Constructeur par défaut de la classe Communication.
static final String DELIMITEUR_FIN
void envoyerMessage(int type, String adresse, int port, String donnees)
Envoie un message.
static int recupererTypeTrame(String[] champs)
Détermine le type de trame.
static final int NB_CHAMPS_DEMANDE_INFORMATIONS
static final int MODIFICATION_DISPONIBILITE
void run()
Assure la réception des trames.
static final int PORT
Port d&#39;écoute des portiers.
void arreter()
Arrête la socket, donc la communication avec les portiers.
static final int NB_CHAMPS_DISPONIBILITE
String fabriquerTrameModification(int typeTrame, List< String > parametres)
Fabrique la trame de modification.
static final int NB_CHAMPS_MODIFICATION_DISPONIBILITE
void envoyer(String trame, String adressePortier)
Envoyer la trame.
void setHandler(Handler handler)
boolean verifierTrame(String trame)
Vérifie la trame.
static final int TYPE_RECEPTION
Code du message indiquant une réception de données.
static final String DELIMITEUR_CHAMP
static final String TAG
TAG pour les logs.
Handler handler
Handler permettant l&#39;échange de Message avec l&#39;activité
LinkedBlockingQueue< DatagramPacket > queueEmission
Queue d&#39;émission des trames.
static final int CHAMP_INDICE_DE_CONFORT
static final String DELIMITEUR_EN_TETE
static final String adresseMulticast
Adresse multicast des portiers.
void recevoir()
Recevoir les trames des portiers.
static final int NB_CHAMPS_DEMANDE_DISPONIBILITE
static final int NB_CHAMPS_RETOUR_MODIFICATION_DISPONIBILITE
DatagramSocket socket
Socket UDP.