ajout TP1

This commit is contained in:
Laureηt 2023-04-22 17:35:59 +02:00
commit 35f382698c
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
7 changed files with 435 additions and 0 deletions

BIN
TP1/Comanche.class Normal file

Binary file not shown.

44
TP1/Comanche.java Normal file
View file

@ -0,0 +1,44 @@
import java.net.*;
import java.io.*;
public class Comanche implements Runnable {
private Socket s;
public Comanche(Socket s) {
this.s = s;
}
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(Integer.parseInt(args[0]));
while (true) {
new Thread(new Comanche(s.accept())).start();
}
}
public void run() {
System.out.println("new connection : " + this.s);
try {
InputStreamReader in = new InputStreamReader(s.getInputStream());
PrintStream out = new PrintStream(s.getOutputStream());
String rq = new LineNumberReader(in).readLine();
System.out.println(rq);
if (rq.startsWith("GET ")) {
File f = new File(rq.substring(5, rq.indexOf(' ', 4)));
if (f.exists() && !f.isDirectory()) {
InputStream is = new FileInputStream(f);
byte[] data = new byte[is.available()];
is.read(data);
is.close();
String s = new String(data);
out.print("HTTP/1.0 200 OK\n\n" + s);
} else {
out.print("HTTP/1.0 404 Not Found\n\n <html>Document not found.</html>");
}
}
out.close();
s.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

BIN
TP1/LoadBalancer.class Normal file

Binary file not shown.

85
TP1/LoadBalancer.java Normal file
View file

@ -0,0 +1,85 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class LoadBalancer extends Thread {
static String hosts[] = { "localhost", "localhost" };
static int ports[] = { 8081, 8082 };
static int nbHosts = 2;
static Random rand = new Random();
static ServerSocket ss;
Socket client_socket;
public LoadBalancer(Socket socket) {
this.client_socket = socket;
}
@Override
public void run() {
try {
InputStream client_is = this.client_socket.getInputStream();
OutputStream client_os = this.client_socket.getOutputStream();
// on selectionne un serveur au pif
int id = rand.nextInt(nbHosts);
Socket server_socket = new Socket(hosts[id], ports[id]);
System.out.println("Established connection with server " + id + " : " + server_socket);
InputStream server_is = server_socket.getInputStream();
OutputStream server_os = server_socket.getOutputStream();
// création du buffer de transfert
byte[] buffer = new byte[1024];
int buffer_length;
// transfère de la requête du client vers le server
buffer_length = client_is.read(buffer);
System.out.println("Received " + buffer + " from client");
server_os.write(buffer, 0, buffer_length);
System.out.println("Sent the buffer to server " + id);
// transfère de la requête du server vers le client
buffer_length = server_is.read(buffer);
System.out.println("Received " + buffer + " from server");
client_os.write(buffer, 0, buffer_length);
System.out.println("Sent the buffer to client");
// fermeture des streams server
server_os.close();
server_is.close();
// fermeture des streams client
client_os.close();
client_is.close();
// fermeture des sockets client et serer
this.client_socket.close();
server_socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
System.out.println("Début du load balancer");
try {
ss = new ServerSocket(8080);
System.out.println("ServerSocket créé");
while (true) {
Socket new_client = ss.accept();
System.out.println("Nouvelle connection: " + new_client);
LoadBalancer ld = new LoadBalancer(new_client);
ld.start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ss.close();
}
}
}

88
TP1/LoadBalancer.java.bak Normal file
View file

@ -0,0 +1,88 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class LoadBalancer extends Thread {
static String hosts[] = { "localhost", "localhost" };
static int ports[] = { 8081, 8082 };
static int nbHosts = 2;
static Random rand = new Random();
Socket client_socket;
InputStream client_is;
OutputStream client_os;
InputStream server_is;
OutputStream server_os;
Socket server_socket;
public LoadBalancer(Socket socket) {
this.client_socket = socket;
}
@Override
public void run() {
try {
client_is = this.client_socket.getInputStream();
client_os = this.client_socket.getOutputStream();
// on selectionne un serveur au pif
int id = rand.nextInt(nbHosts);
server_socket = new Socket(hosts[id], ports[id]);
System.out.println("Established connection with server " + id + " : " + server_socket);
server_is = server_socket.getInputStream();
server_os = server_socket.getOutputStream();
// création du buffer de transfert
byte[] buffer = new byte[1024];
int buffer_length;
// transfère de la requête du client vers le server
buffer_length = client_is.read(buffer);
System.out.println("Received " + buffer + " from client");
server_os.write(buffer, 0, buffer_length);
System.out.println("Sent the buffer to server " + id);
// transfère de la requête du server vers le client
buffer_length = server_is.read(buffer);
System.out.println("Received " + buffer + " from server");
server_os.write(buffer, 0, buffer_length);
System.out.println("Sent the buffer to client");
} catch (Exception e) {
e.printStackTrace();
} finally {
// fermeture des streams server
server_os.close();
server_is.close();
// fermeture des streams client
client_os.close();
client_is.close();
// fermeture des sockets client et serer
this.client_socket.close();
server_socket.close();
}
}
public static void main(String[] args) {
System.out.println("Début du load balancer");
try {
ServerSocket ss = new ServerSocket(8080);
System.out.println("ServerSocket créé");
while (true) {
Socket new_client = ss.accept();
System.out.println("Nouvelle connection: " + new_client);
LoadBalancer ld = new LoadBalancer(new_client);
ld.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

210
TP1/TP-socket.html Normal file
View file

@ -0,0 +1,210 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html;
charset=windows-1252">
<title></title>
<meta name="GENERATOR" content="OpenOffice.org 1.1.4 (Win32)">
<meta name="CREATED" content="20051027;135681">
<meta name="CHANGEDBY" content="Daniel Hagimont">
<meta name="CHANGED" content="20051106;19023960">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta name="Originator" content="Microsoft Word 10">
<meta name="Template" content="C:\PROGRAM FILES\MICROSOFT
OFFICE\OFFICE\html.dot">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:Zoom>95</w:Zoom>
<w:GrammarState>Clean</w:GrammarState>
<w:HyphenationZone>21</w:HyphenationZone>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Tableau Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman"}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout></xml><![endif]--><!--[if gte mso 9]>
<xml><o:shapedefaults v:ext="edit" spidmax="1027"/>
</xml><![endif]--><!--[if gte vml 1]><v:shapetype id="_x0000_t75"
coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="_x0000_s1025" type="#_x0000_t75" style='width:172.5pt;
height:114pt'>
<v:imagedata src="TP1_fichiers/image001.gif" o:title="logo_inpg"/>
</v:shape><![endif]-->
<style>
<!--
P { color: #000000 }
A:link { color: #0000ff }
A:visited { color: #800080 }
-->
</style>
</head>
<body dir="ltr" lang="fr-FR" link="#0000ff" text="#000000"
vlink="#800080">
<p align="center"><br>
<b><font style="font-size: 20pt;" size="5">Cours Intergiciels<br>
</font></b></p>
<p align="center"><font style="font-size: 20pt;" size="5"><b>ENSEEIHT
Département Informatique 2ième année</b></font></p>
<p align="center"><font style="font-size: 20pt;" size="5"><b>TP
sockets</b></font></p>
<p style="margin-bottom: 0cm;" align="center"><font size="3">Daniel
Hagimont</font></p>
<p style="margin-bottom: 0cm;" align="center"><font size="3">Daniel.Hagimont@enseeiht.fr</font></p>
<p align="center"><br>
<br>
</p>
<p style="margin-left: 1.27cm; text-indent: -0.64cm;"><a
name="_Toc432321016"></a> <font face="Arial"><font
style="font-size: 13pt;" size="3"><b>Objectif</b></font></font></p>
<p style="margin-left: 1.27cm;">L'objectif de ce TP est l'initiation
à la programmation répartie. Il consiste à programmer une
application typique comportant de la répartition à l'aide de
Socket. Vous devez implanter un répartiteur de charge (<i>Load
Balancer</i>) qui reçoit des requêtes HTTP et les redirige de
façon aléatoire vers un ensemble de serveurs Web. <br>
</p>
<p style="margin-left: 1.27cm;">Le début de cette classe est le
suivant :<br>
</p>
<blockquote>
<blockquote>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> <font face="Times, serif"><font
style="font-size: 11pt" size="2"><i>public class
LoadBalancer {</i></font></font></p>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i>static
String hosts[] = {"localhost", "localhost"};</i></font></font><br>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i>static
int ports[] = {8081,8082};</i></font></font><br>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i>static
int nbHosts = 2;</i></font></font><br>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i>static
Random rand = new Random();</i></font></font><br>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i></i></font></font><br>
<font face="Times, serif"><font style="font-size: 11pt" size="2"><i>}</i></font></font>
</blockquote>
</blockquote>
<blockquote>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> A chaque réception d'une requête HTTP (une
connexion TCP), <i>LoadBalancer</i> transfère la requête à un
des serveurs web (les adresses de ces serveurs sont données par
les tables <i>hosts</i> et <i>ports</i>) et <i>LoadBalancer</i>
transfère le résultat de la requête à l'émetteur. Le choix du
serveur Web est aléatoire (<i>rand.nextInt(nbHosts)</i> retourne
un entier entre 0 et <i>nbHosts-1</i>). Pour être efficace, <i>LoadBalancer</i>
est évidemment multi-threadé.</p>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> <br>
</p>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> Pour la programmation des entrées/sorties, on
utilisera :</p>
</blockquote>
<ul>
<ul>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> <font style="font-size: 11pt" size="2"><i>-
InputStream<br>
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </i></font><font
size="2"><i></i></font><font style="font-size: 11pt"
size="2"><i>- public int read(byte[] b); // bloquante,
retourne le nombre de bytes lus</i></font></p>
</ul>
</ul>
<ul>
<ul>
</ul>
<ul>
<p class="western" style="margin-bottom: 0cm" lang="fr-FR"
align="JUSTIFY"> <font style="font-size: 11pt" size="2"><i>-
OutputStream</i></font><font style="font-size: 11pt"
size="2"><i><br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; - public void
write(byte[] b, int off, int len); // écrit les len bytes
à la position off</i></font></p>
</ul>
<ul>
<p class="western" style="margin-right: 0.56cm; margin-bottom:
0cm; font-style: normal" lang="fr-FR" align="JUSTIFY"> et on
suppose que les requêtes et réponses sont lues ou écrites en
un seul appel de méthode avec un buffer de 1024 bytes. Notez
que cette hypothèse n'est pas vérifieée dans la vrai vie.<br>
</p>
</ul>
</ul>
<br>
<p></p>
<p style="margin-left: 1.27cm; text-indent: -0.64cm;"><a
name="_Toc432321016"></a> <font size="3"><font face="Arial"><b>Tests</b></font></font></p>
<p style="margin-left: 1.27cm;"> <br>
Pour tester, vous disposez d'un petit serveur Web (Comanche.java).<br>
Vous pouvez en lancer 2 depuis 2 terminaux avec :<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; java
Comanche 8081<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; java
Comanche 8082<br>
</p>
<p style="margin-left: 1.27cm;">Lancez votre LoadBalancer sur la
même machine (il accepte les connexions sur le port 8080).<br>
</p>
<p style="margin-left: 1.27cm;">Entrez dans un navigateur (sur la
même machine) l'URL : localhost:8080/page.html<br>
(la page page.html est sensée est présente dans le répertoire où
vous avez lancé Comanche)<br>
<br>
</p>
</body>
</html>

8
TP1/page.html Normal file
View file

@ -0,0 +1,8 @@
<html>
<h1>
Welcome in Comanche!
</h1>
<br>
This is a test page to test the Comanche server.
</html>