forked from goturak/Teaching-HEIGVD-RES-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,035 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
examples/04-StreamingTimeServer/StreamingTimeServer/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>ch.heigvd.res.examples</groupId> | ||
<artifactId>StreamingTimeServer</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.3</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<shadedArtifactAttached>true</shadedArtifactAttached> | ||
<shadedClassifierName>standalone</shadedClassifierName> <!-- Can be any name that makes sense --> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>ch.heigvd.res.examples.StreamingTimeServer</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
<name>StreamingTimeServer</name> | ||
</project> |
134 changes: 134 additions & 0 deletions
134
...eServer/StreamingTimeServer/src/main/java/ch/heigvd/res/examples/StreamingTimeServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package ch.heigvd.res.examples; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintWriter; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.util.Date; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* A very simple example of TCP server. When the server starts, it binds a | ||
* server socket on any of the available network interfaces and on port 2205. It | ||
* then waits until one (only one!) client makes a connection request. When the | ||
* client arrives, the server does not even check if the client sends data. It | ||
* simply writes the current time, every second, during 15 seconds. | ||
* | ||
* To test the server, simply open a terminal, do a "telnet localhost 2205" and | ||
* see what you get back. Use Wireshark to have a look at the transmitted TCP | ||
* segments. | ||
* | ||
* @author Olivier Liechti | ||
*/ | ||
public class StreamingTimeServer { | ||
|
||
static final Logger LOG = Logger.getLogger(StreamingTimeServer.class.getName()); | ||
|
||
private final int TEST_DURATION = 15000; | ||
private final int PAUSE_DURATION = 1000; | ||
private final int NUMBER_OF_ITERATIONS = TEST_DURATION / PAUSE_DURATION; | ||
private final int LISTEN_PORT = 2205; | ||
|
||
/** | ||
* This method does the entire processing. | ||
*/ | ||
public void start() { | ||
LOG.info("Starting server..."); | ||
|
||
ServerSocket serverSocket = null; | ||
Socket clientSocket = null; | ||
BufferedReader reader = null; | ||
PrintWriter writer = null; | ||
|
||
try { | ||
LOG.log(Level.INFO, "Creating a server socket and binding it on any of the available network interfaces and on port {0}", new Object[]{Integer.toString(LISTEN_PORT)}); | ||
serverSocket = new ServerSocket(LISTEN_PORT); | ||
logServerSocketAddress(serverSocket); | ||
|
||
while (true) { | ||
LOG.log(Level.INFO, "Waiting (blocking) for a connection request on {0} : {1}", new Object[]{serverSocket.getInetAddress(), Integer.toString(serverSocket.getLocalPort())}); | ||
clientSocket = serverSocket.accept(); | ||
|
||
LOG.log(Level.INFO, "A client has arrived. We now have a client socket with following attributes:"); | ||
logSocketAddress(clientSocket); | ||
|
||
LOG.log(Level.INFO, "Getting a Reader and a Writer connected to the client socket..."); | ||
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
writer = new PrintWriter(clientSocket.getOutputStream()); | ||
|
||
LOG.log(Level.INFO, "Starting my job... sending current time to the client for {0} ms", TEST_DURATION); | ||
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { | ||
writer.println(String.format("{'time' : '%s'}", new Date())); | ||
writer.flush(); | ||
LOG.log(Level.INFO, "Sent data to client, doing a pause..."); | ||
Thread.sleep(PAUSE_DURATION); | ||
} | ||
|
||
reader.close(); | ||
writer.close(); | ||
clientSocket.close(); | ||
|
||
|
||
} | ||
|
||
} catch (IOException | InterruptedException ex) { | ||
LOG.log(Level.SEVERE, ex.getMessage()); | ||
} finally { | ||
LOG.log(Level.INFO, "We are done. Cleaning up resources, closing streams and sockets..."); | ||
try { | ||
reader.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(StreamingTimeServer.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
writer.close(); | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(StreamingTimeServer.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
try { | ||
serverSocket.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(StreamingTimeServer.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A utility method to print server socket information | ||
* | ||
* @param serverSocket the socket that we want to log | ||
*/ | ||
private void logServerSocketAddress(ServerSocket serverSocket) { | ||
LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{serverSocket.getLocalSocketAddress()}); | ||
LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(serverSocket.getLocalPort())}); | ||
LOG.log(Level.INFO, " is bound: {0}", new Object[]{serverSocket.isBound()}); | ||
} | ||
|
||
/** | ||
* A utility method to print socket information | ||
* | ||
* @param clientSocket the socket that we want to log | ||
*/ | ||
private void logSocketAddress(Socket clientSocket) { | ||
LOG.log(Level.INFO, " Local IP address: {0}", new Object[]{clientSocket.getLocalAddress()}); | ||
LOG.log(Level.INFO, " Local port: {0}", new Object[]{Integer.toString(clientSocket.getLocalPort())}); | ||
LOG.log(Level.INFO, " Remote Socket address: {0}", new Object[]{clientSocket.getRemoteSocketAddress()}); | ||
LOG.log(Level.INFO, " Remote port: {0}", new Object[]{Integer.toString(clientSocket.getPort())}); | ||
} | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); | ||
|
||
StreamingTimeServer server = new StreamingTimeServer(); | ||
server.start(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>ch.heigvd.res.examples</groupId> | ||
<artifactId>DumbHttpClient</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.3</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<shadedArtifactAttached>true</shadedArtifactAttached> | ||
<shadedClassifierName>standalone</shadedClassifierName> <!-- Can be any name that makes sense --> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>ch.heigvd.res.examples.DumbHttpClient</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
</project> |
80 changes: 80 additions & 0 deletions
80
...05-DumbHttpClient/DumbHttpClient/src/main/java/ch/heigvd/res/examples/DumbHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package ch.heigvd.res.examples; | ||
|
||
import java.io.*; | ||
import java.net.Socket; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* This is not really an HTTP client, but rather a very simple program that | ||
* establishes a TCP connection with a real HTTP server. Once connected, the | ||
* client sends "garbage" to the server (the client does not send a proper | ||
* HTTP request that the server would understand). The client then reads the | ||
* response sent back by the server and logs it onto the console. | ||
* | ||
* @author Olivier Liechti | ||
*/ | ||
public class DumbHttpClient { | ||
|
||
static final Logger LOG = Logger.getLogger(DumbHttpClient.class.getName()); | ||
|
||
final static int BUFFER_SIZE = 1024; | ||
|
||
/** | ||
* This method does the whole processing | ||
*/ | ||
public void sendWrongHttpRequest() { | ||
Socket clientSocket = null; | ||
OutputStream os = null; | ||
InputStream is = null; | ||
|
||
try { | ||
clientSocket = new Socket("www.lematin.ch", 80); | ||
os = clientSocket.getOutputStream(); | ||
is = clientSocket.getInputStream(); | ||
|
||
String malformedHttpRequest = "Hello, sorry, but I don't speak HTTP...\r\n\r\n"; | ||
os.write(malformedHttpRequest.getBytes()); | ||
|
||
ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int newBytes; | ||
while ((newBytes = is.read(buffer)) != -1) { | ||
responseBuffer.write(buffer, 0, newBytes); | ||
} | ||
|
||
LOG.log(Level.INFO, "Response sent by the server: "); | ||
LOG.log(Level.INFO, responseBuffer.toString()); | ||
} catch (IOException ex) { | ||
LOG.log(Level.SEVERE, null, ex); | ||
} finally { | ||
try { | ||
is.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(DumbHttpClient.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
try { | ||
os.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(DumbHttpClient.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException ex) { | ||
Logger.getLogger(DumbHttpClient.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); | ||
|
||
DumbHttpClient client = new DumbHttpClient(); | ||
client.sendWrongHttpRequest(); | ||
|
||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
examples/06-PresenceApplication/PresenceApplication/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>ch.heigvd.res.examples</groupId> | ||
<artifactId>PresenceApplication</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.3</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<shadedArtifactAttached>true</shadedArtifactAttached> | ||
<shadedClassifierName>standalone</shadedClassifierName> <!-- Can be any name that makes sense --> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>ch.heigvd.res.examples.PresenceApplication</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
<name>PresenceApplication</name> | ||
</project> |
38 changes: 38 additions & 0 deletions
38
...ication/PresenceApplication/src/main/java/ch/heigvd/res/examples/PresenceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ch.heigvd.res.examples; | ||
|
||
/** | ||
* The server reacts to the following commands, defined in the protocol: | ||
* - HELLO name: the user "behind" the client is not anonymous anymore | ||
* - SAY message: the message is broadcasted to connected clients | ||
* - WHO: the server returns the list of connected users | ||
* - BYE: the client is disconnected and the others are notified | ||
* | ||
* @author Olivier Liechti | ||
*/ | ||
public class PresenceApplication { | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s %n"); | ||
|
||
Thread listenThread = new Thread(new PresenceServer()); | ||
listenThread.start(); | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
PresenceClient c1 = new PresenceClient(); | ||
c1.connect("localhost", Protocol.PRESENCE_DEFAULT_PORT, "Sacha"); | ||
new PresenceClient().connect("localhost", Protocol.PRESENCE_DEFAULT_PORT, "Fabienne"); | ||
new PresenceClient().connect("localhost", Protocol.PRESENCE_DEFAULT_PORT, "Olivier"); | ||
c1.disconnect(); | ||
new PresenceClient().connect("localhost", Protocol.PRESENCE_DEFAULT_PORT, "Jean"); | ||
new PresenceClient().connect("localhost", Protocol.PRESENCE_DEFAULT_PORT, "Nicole"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.