Meeting  0.2
BTS SNIR LaSalle Avignon 2020
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 import java.net.*;
8 import java.io.IOException;
9 import java.net.DatagramPacket;
10 import java.net.Socket;
11 import java.util.concurrent.locks.ReentrantLock;
12 
23 public class Communication implements Runnable
24 {
28  private final static String TAG = "Communication";
29  private final static String adresseMulticast = "239.0.0.42";
30  private final static int PORT = 5000;
31  public static final int TIMEOUT_RECEPTION_REPONSE = 30000;
32  public final static int CODE_RECEPTION = 1;
33  private final ReentrantLock mutex = new ReentrantLock();
37  private DatagramSocket socket;
38  private InetAddress adresseIP = null;
39  private Handler handler;
40  private String trame;
41 
44  public static final String DELIMITEUR_EN_TETE = "$";
45  public static final String DELIMITEUR_CHAMP = ";";
46  public static final String DELIMITEUR_FIN = "\r\n";
47  public static final int NB_DELIMITEURS_GET_1 = 6; // $GET;1\r\n
48  public static final int NB_DELIMITEURS_GET_2 = 3; // $GET;2\r\n
49  public static final int NB_DELIMITEURS_GET_3 = 1; // $GET;3\r\n
50 
56  public Communication(Handler handler)
57  {
58  this.handler = handler;
59  try
60  {
61  socket = new DatagramSocket(PORT);
62  socket.setSoTimeout(Communication.TIMEOUT_RECEPTION_REPONSE);
63  }
64  catch (SocketException se)
65  {
66  se.printStackTrace();
67  }
68 
69  try
70  {
71  this.adresseIP = InetAddress.getByName(adresseMulticast);
72  }
73  catch (UnknownHostException e)
74  {
75  e.printStackTrace();
76  }
77  }
78 
84  public Communication(int port, Handler handler)
85  {
86  this.handler = handler;
87  try
88  {
89  socket = new DatagramSocket(port);
90  socket.setSoTimeout(Communication.TIMEOUT_RECEPTION_REPONSE);
91  }
92  catch (SocketException se)
93  {
94  se.printStackTrace();
95  }
96 
97  try
98  {
99  this.adresseIP = InetAddress.getByName(adresseMulticast);
100  }
101  catch (UnknownHostException e)
102  {
103  e.printStackTrace();
104  }
105  }
106 
112  public void setHandler(Handler handler)
113  {
114  mutex.lock();
115  this.handler = handler;
116  mutex.unlock();
117  }
118 
123  public void recevoir()
124  {
125  byte[] reception = new byte[1024];
126 
127  while (socket != null && !socket.isClosed())
128  {
129  try
130  {
131  final DatagramPacket paquetRecu = new DatagramPacket(reception, reception.length);
132  socket.receive(paquetRecu);
133 
134  trame = new String(paquetRecu.getData(), paquetRecu.getOffset(), paquetRecu.getLength());
135  Log.d(TAG, "Réception de " + paquetRecu.getAddress().getHostAddress() + ":" + paquetRecu.getPort() + " -> " + trame);
136 
137  Message msg = Message.obtain();
138  Bundle b = new Bundle();
139  b.putString("adresseIP", paquetRecu.getAddress().getHostAddress());
140  b.putInt("port", paquetRecu.getPort());
141  b.putInt("etat", Communication.CODE_RECEPTION);
142  b.putString("trame", trame);
143  msg.setData(b);
144  mutex.lock();
145  handler.sendMessage(msg);
146  mutex.unlock();
147  }
148  catch (Exception e)
149  {
150  Log.d(TAG, "Erreur recevoir() [socket.isClosed = " + socket.isClosed() + "]");
151  e.printStackTrace();
152  }
153  }
154  }
155 
161  public void envoyer(final String requete)
162  {
163  if(socket == null)
164  return;
165 
166  new Thread()
167  {
168  @Override public void run()
169  {
170  byte[] emission = new byte[1024];
171 
172  try
173  {
174  emission = requete.getBytes();
175  DatagramPacket paquetRetour = new DatagramPacket(emission, emission.length, adresseIP, PORT);
176  socket.send(paquetRetour);
177  Log.d(TAG, "send() = " + requete);
178  }
179  catch (IOException e)
180  {
181  Log.d(TAG, "Erreur send() [socket.isClosed = " + socket.isClosed() + "]");
182  e.printStackTrace();
183  }
184  }
185  }.start();
186  }
187 
193  public void envoyer(final String requete, final String adresseIP)
194  {
195  if(socket == null)
196  return;
197 
198  final InetAddress adresseIPDistante;
199  try
200  {
201  adresseIPDistante = InetAddress.getByName(adresseIP);
202  }
203  catch (UnknownHostException e)
204  {
205  e.printStackTrace();
206  return;
207  }
208 
209  new Thread()
210  {
211  @Override public void run()
212  {
213  byte[] emission = new byte[1024];
214 
215  try
216  {
217  emission = requete.getBytes();
218  DatagramPacket paquetRetour = new DatagramPacket(emission, emission.length, adresseIPDistante, PORT);
219  socket.send(paquetRetour);
220  Log.d(TAG, "send() " + adresseIP + " = " + requete);
221  }
222  catch (IOException e)
223  {
224  Log.d(TAG, "Erreur send() [socket.isClosed = " + socket.isClosed() + "]");
225  e.printStackTrace();
226  }
227  }
228  }.start();
229  }
230 
235  public void arreter()
236  {
237  if(socket == null)
238  return;
239  socket.close();
240  }
241 
246  @Override
247  public void run()
248  {
249  recevoir();
250  }
251 }
Déclaration de la classe Communication.
static final int TIMEOUT_RECEPTION_REPONSE
temps maximum d'une réponse d'un portier
InetAddress adresseIP
attribut récuperant l'adresse IP du portier
Communication(Handler handler)
constructeur de communication
static final String DELIMITEUR_FIN
void run()
méthode appelée automatiquement quand le socket reçois quelque chose
static final int PORT
port d'ecoute des portiers
void arreter()
méthode arrétant la socket, donc la communication avec les portiers
void envoyer(final String requete, final String adresseIP)
méthode envoyant une requête à l'adresse de indiqué en paramètre
Communication(int port, Handler handler)
constructeur de communication
String trame
attribut récuperant la trame
void setHandler(Handler handler)
change le handler par celui mis en paramètre
static final int CODE_RECEPTION
code de reception correcte pour le portiers
static final String DELIMITEUR_CHAMP
static final String TAG
TAG utilisé dans les log.
void envoyer(final String requete)
méthode envoyant une requête à l'adresse de multicast
Handler handler
attribut permetant d'envoyer une requête par rapport a une autre activity
static final String DELIMITEUR_EN_TETE
static final String adresseMulticast
adresse de multicast des portiers
void recevoir()
méthode recevant les trames des portiers
DatagramSocket socket
attribut récuperant les informations de la socket