0% found this document useful (0 votes)
5 views

ClientController.java

The ClientController class manages communication between a client application and a server for handling student data. It provides methods to check server connection, send, modify, delete, and retrieve student information, as well as handle local storage of data when the connection is lost. The class utilizes XMLHandler for saving and loading data locally and employs sockets for network communication with the server.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ClientController.java

The ClientController class manages communication between a client application and a server for handling student data. It provides methods to check server connection, send, modify, delete, and retrieve student information, as well as handle local storage of data when the connection is lost. The class utilizes XMLHandler for saving and loading data locally and employs sockets for network communication with the server.

Uploaded by

corbeille.omega
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

package client.

controller;

import client.model.Etudiant;

import client.utils.XMLHandler;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

import java.net.ConnectException;

import java.util.List;

import javax.swing.JOptionPane;

public class ClientController {

private static final String SERVER_IP = “localhost”;

private static final int SERVER_PORT = 12345;

private final XMLHandler xmlHandler;

private boolean isConnected = false;

public ClientController() {

this.xmlHandler = new XMLHandler();

public boolean checkConnection() {

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT)) {

isConnected = true;

return true;

} catch (IOException e) {
isConnected = false;

return false;

public String envoyerEtudiant(Etudiant etudiant) {

if (!checkConnection()) {

xmlHandler.saveToXML(etudiant);

return “Connexion perdue. Donnée sauvegardée localement.”;

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

oos.writeObject(”AJOUT”);

oos.writeObject(etudiant);

return (String) ois.readObject();

} catch (ConnectException e) {

xmlHandler.saveToXML(etudiant);

return “Connexion perdue. Donnée sauvegardée localement.”;

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

return “Erreur lors de l’envoi des données.”;

}
public List<Etudiant> getEtudiants() {

if (!checkConnection()) {

JOptionPane.showMessageDialog(null, “Connexion au serveur perdue”, “Erreur”,


JOptionPane.ERROR_MESSAGE);

return null;

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

oos.writeObject(”LISTE”);

return (List<Etudiant>) ois.readObject();

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

return null;

public String modifierEtudiant(Etudiant etudiant) {

if (!checkConnection()) {

return “Connexion perdue. Modification impossible.”;

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {


oos.writeObject(”MODIFIER”);

oos.writeObject(etudiant);

return (String) ois.readObject();

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

return “Erreur lors de la modification.”;

public String supprimerEtudiant(int numeroEt) {

if (!checkConnection()) {

return “Connexion perdue. Suppression impossible.”;

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

oos.writeObject(”SUPPRIMER”);

oos.writeObject(numeroEt);

return (String) ois.readObject();

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

return “Erreur lors de la suppression.”;


}

public void envoyerFichiersEnAttente() {

if (!checkConnection()) return;

List<Etudiant> etudiantsEnAttente = xmlHandler.loadFromXML();

if (etudiantsEnAttente == null || etudiantsEnAttente.isEmpty()) return;

try (Socket socket = new Socket(SERVER_IP, SERVER_PORT);

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

oos.writeObject(”AJOUT_MULTIPLE”);

oos.writeObject(etudiantsEnAttente);

String response = (String) ois.readObject();

if (response.startsWith(”SUCCES”)) {

xmlHandler.clearXMLFile();

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

public boolean isConnected() {

return isConnected;
}

You might also like