Alter 6 files

Add `ProgramExemplaryOutput.txt`
Add `pom.xml`
Add `module-info.java`
Add `Juliet.java`
Add `PlayWriter.java`
Add `Romeo.java`
This commit is contained in:
akp 2023-03-02 15:37:58 +00:00
parent bafce3855f
commit 54ee4d2bc2
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 11648 additions and 0 deletions

File diff suppressed because it is too large Load diff

51
romeo-juliet/pom.xml Normal file
View file

@ -0,0 +1,51 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.akpain.fsad.ass2</groupId>
<artifactId>romeo-juliet</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>net.akpain.fsad.ass2.PlayWright</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,4 @@
module org.example {
requires javafx.controls;
exports net.akpain.fsad.ass2;
}

View file

@ -0,0 +1,160 @@
package net.akpain.fsad.ass2;
/*
* Juliet.java
*
* Juliet class. Implements the Juliet subsystem of the Romeo and Juliet ODE system.
*/
import javafx.util.Pair;
import java.lang.Thread;
import java.net.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.time.chrono.IsoChronology;
import java.util.Arrays;
import java.util.zip.InflaterOutputStream;
public class Juliet extends Thread {
private ServerSocket ownServerSocket = null; //Juliet's (server) socket
private Socket serviceMailbox = null; //Juliet's (service) socket
private double currentLove = 0;
private double b = 0;
private final int port = 8001;
//Class construtor
public Juliet(double initialLove) {
currentLove = initialLove;
b = 0.01;
try {
ownServerSocket = new ServerSocket(port, 1, InetAddress.getLocalHost());
System.out.println("Juliet: Good pilgrim, you do wrong your hand too much, ...");
} catch(Exception e) {
System.out.println("Juliet: Failed to create own socket " + e);
}
}
//Get acquaintance with lover;
// Receives lover's socket information and share's own socket
public Pair<InetAddress,Integer> getAcquaintance() {
System.out.println("Juliet: My bounty is as boundless as the sea,\n" +
" My love as deep; the more I give to thee,\n" +
" The more I have, for both are infinite.");
return new Pair<>(ownServerSocket.getInetAddress(), port);
}
//Retrieves the lover's love
public double receiveLoveLetter() {
Socket sock = null;
try {
sock = ownServerSocket.accept();
} catch (IOException e) {
PlayWriter.die(e);
}
assert sock != null;
serviceMailbox = sock;
byte[] input = null;
try {
input = sock.getInputStream().readAllBytes();
} catch (IOException e) {
PlayWriter.die(e);
}
assert input != null;
// sock.close();
System.err.printf("[J] RECV %s\n", PlayWriter.bytesToHex(input));
Double res = null;
try {
res = Double.parseDouble(PlayWriter.stringFromByteArray(input).substring(0, input.length - 1));
} catch (NumberFormatException e) {
PlayWriter.die(e);
}
assert res != null;
System.out.println("Juliet: Romeo, Romeo! Wherefore art thou Romeo? (<-" + res + ")");
return res;
}
//Love (The ODE system)
//Given the lover's love at time t, estimate the next love value for Romeo
public double renovateLove(double partnerLove){
System.out.println("Juliet: Come, gentle night, come, loving black-browed night,\n" +
" Give me my Romeo, and when I shall die,\n" +
" Take him and cut him out in little stars.");
currentLove = currentLove+(-b*partnerLove);
return currentLove;
}
//Communicate love back to playwriter
public void declareLove(){
System.out.println("Juliet: Good night, good night!\n" +
" Parting is such sweet sorrow,\n" +
" That I shall say good night till it be morrow.");
assert serviceMailbox != null;
OutputStream stream = null;
try {
stream = serviceMailbox.getOutputStream();
} catch (IOException e) {
PlayWriter.die(e);
}
assert stream != null;
try {
stream.write((currentLove + "J").getBytes());
stream.close();
serviceMailbox.close();
} catch (IOException e) {
PlayWriter.die(e);
}
serviceMailbox = null;
}
//Execution
public void run () {
try {
while (!this.isInterrupted()) {
//Retrieve lover's current love
double RomeoLove = this.receiveLoveLetter();
//Estimate new love value
this.renovateLove(RomeoLove);
//Communicate back to lover, Romeo's love
this.declareLove();
}
}catch (Exception e){
System.out.println("Juliet: " + e);
}
if (this.isInterrupted()) {
System.out.println("Juliet: I will kiss thy lips.\n" +
"Haply some poison yet doth hang on them\n" +
"To make me die with a restorative.");
}
}
}

View file

@ -0,0 +1,283 @@
package net.akpain.fsad.ass2;
/*
* PlayWriter.java
*
* PLayWriter class.
* Creates the lovers, and writes the two lover's story (to an output text file).
*/
import java.net.Socket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import javafx.scene.control.TreeItem;
import javafx.util.Pair;
public class PlayWriter {
private Romeo myRomeo = null;
private InetAddress RomeoAddress = null;
private int RomeoPort = 0;
private Socket RomeoMailbox = null;
private Juliet myJuliet = null;
private InetAddress JulietAddress = null;
private int JulietPort = 0;
private Socket JulietMailbox = null;
double[][] theNovel = null;
int novelLength = 0;
public PlayWriter()
{
novelLength = 500; //Number of verses
theNovel = new double[novelLength][2];
theNovel[0][0] = 0;
theNovel[0][1] = 1;
}
//Create the lovers
public void createCharacters() {
//Create the lovers
System.out.println("PlayWriter: Romeo enters the stage.");
myRomeo = new Romeo(theNovel[0][0]);
myRomeo.start();
System.out.println("PlayWriter: Juliet enters the stage.");
myJuliet = new Juliet(theNovel[0][1]);
myJuliet.start();
}
//Meet the lovers and start letter communication
public void charactersMakeAcquaintances() {
Pair<InetAddress, Integer> ra = myRomeo.getAcquaintance();
RomeoAddress = ra.getKey();
RomeoPort = ra.getValue();
try {
RomeoMailbox = new Socket(RomeoAddress, RomeoPort);
} catch (IOException e) {
die(e);
}
System.out.println("PlayWriter: I've made acquaintance with Romeo");
Pair<InetAddress, Integer> ja = myJuliet.getAcquaintance();
JulietAddress = ja.getKey();
JulietPort = ja.getValue();
try {
JulietMailbox = new Socket(JulietAddress, JulietPort);
} catch (IOException e) {
die(e);
}
System.out.println("PlayWriter: I've made acquaintance with Juliet");
}
//Request next verse: Send letters to lovers communicating the partner's love in previous verse
public void requestVerseFromRomeo(int verse) {
System.out.println("PlayWriter: Requesting verse " + verse + " from Romeo. -> (" + theNovel[verse-1][1] + ")");
String send = theNovel[verse-1][1] + "J";
OutputStream stream = null;
try {
stream = RomeoMailbox.getOutputStream();
} catch (IOException e) {
PlayWriter.die(e);
}
assert stream != null;
try {
stream.write(send.getBytes());
// stream.close();
} catch (IOException e) {
PlayWriter.die(e);
}
}
//Request next verse: Send letters to lovers communicating the partner's love in previous verse
public void requestVerseFromJuliet(int verse) {
System.out.println("PlayWriter: Requesting verse " + verse + " from Juliet. -> (" + theNovel[verse-1][0] + ")");
String send = theNovel[verse-1][0] + "R";
OutputStream stream = null;
try {
stream = JulietMailbox.getOutputStream();
} catch (IOException e) {
PlayWriter.die(e);
}
assert stream != null;
try {
stream.write(send.getBytes());
// stream.close();
} catch (IOException e) {
PlayWriter.die(e);
}
}
//Receive letter from Romeo with renovated love for current verse
public void receiveLetterFromRomeo(int verse) {
//System.out.println("PlayWriter: Receiving letter from Romeo for verse " + verse + ".");
String res = null;
try {
InputStream out = RomeoMailbox.getInputStream();
res = stringFromByteArray(out.readAllBytes());
out.close();
} catch (IOException e) {
PlayWriter.die(e);
}
assert res != null;
res = res.substring(0, res.length()-1);
theNovel[verse][0] = Double.parseDouble(res);
System.out.println("PlayWriter: Romeo's verse " + verse + " -> " + theNovel[verse][0]);
}
//Receive letter from Juliet with renovated love fro current verse
public void receiveLetterFromJuliet(int verse) {
String res = null;
try {
InputStream out = JulietMailbox.getInputStream();
res = stringFromByteArray(out.readAllBytes());
out.close();
} catch (IOException e) {
PlayWriter.die(e);
}
assert res != null;
res = res.substring(0, res.length()-1);
theNovel[verse][1] = Double.parseDouble(res);
System.out.println("PlayWriter: Juliet's verse " + verse + " -> " + theNovel[verse][1]);
}
//Let the story unfold
public void storyClimax() {
for (int verse = 1; verse < novelLength; verse++) {
//Write verse
System.out.println("PlayWriter: Writing verse " + verse + ".");
requestVerseFromRomeo(verse);
receiveLetterFromRomeo(verse);
requestVerseFromJuliet(verse);
receiveLetterFromJuliet(verse);
System.out.println("PlayWriter: Verse " + verse + " finished.");
}
}
//Character's death
public void charactersDeath() {
myRomeo.interrupt();
myJuliet.interrupt();
}
//A novel consists of introduction, conflict, climax and denouement
public void writeNovel() {
System.out.println("PlayWriter: The Most Excellent and Lamentable Tragedy of Romeo and Juliet.");
System.out.println("PlayWriter: A play in IV acts.");
//Introduction,
System.out.println("PlayWriter: Act I. Introduction.");
this.createCharacters();
//Conflict
System.out.println("PlayWriter: Act II. Conflict.");
this.charactersMakeAcquaintances();
//Climax
System.out.println("PlayWriter: Act III. Climax.");
this.storyClimax();
//Denouement
System.out.println("PlayWriter: Act IV. Denouement.");
this.charactersDeath();
}
//Dump novel to file
public void dumpNovel() {
FileWriter Fw = null;
try {
Fw = new FileWriter("RomeoAndJuliet.csv");
} catch (IOException e) {
System.out.println("PlayWriter: Unable to open novel file. " + e);
}
System.out.println("PlayWriter: Dumping novel. ");
StringBuilder sb = new StringBuilder();
for (int act = 0; act < novelLength; act++) {
String tmp = theNovel[act][0] + ", " + theNovel[act][1] + "\n";
sb.append(tmp);
//System.out.print("PlayWriter [" + act + "]: " + tmp);
}
try {
BufferedWriter br = new BufferedWriter(Fw);
br.write(sb.toString());
br.close();
} catch (Exception e) {
System.out.println("PlayWriter: Unable to dump novel. " + e);
}
}
public static void main (String[] args) {
PlayWriter Shakespeare = new PlayWriter();
Shakespeare.writeNovel();
Shakespeare.dumpNovel();
System.exit(0);
}
public static void die(Exception e) {
System.err.printf("Unhandled error: %s\n", e.toString());
for (StackTraceElement st: e.getStackTrace()) {
System.err.println(st.toString());
}
System.exit(1);
}
public static String stringFromByteArray(byte[] ba) {
StringBuilder sb = new StringBuilder();
for (byte b: ba) {
sb.append(b);
}
return sb.toString();
}
// TODO: REMOVE THIS
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
}

View file

@ -0,0 +1,149 @@
package net.akpain.fsad.ass2;
/*
* Romeo.java
*
* Romeo class. Implements the Romeo subsystem of the Romeo and Juliet ODE system.
*/
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import javafx.util.Pair;
public class Romeo extends Thread {
private ServerSocket ownServerSocket = null; //Romeo's (server) socket
private Socket serviceMailbox = null; //Romeo's (service) socket
private final int port = 8002;
private double currentLove = 0;
private double a = 0; //The ODE constant
//Class construtor
public Romeo(double initialLove) {
currentLove = initialLove;
a = 0.02;
try {
ownServerSocket = new ServerSocket(port, 1, InetAddress.getLocalHost());
System.out.println("Romeo: What lady is that, which doth enrich the hand\n" +
" Of yonder knight?");
} catch(Exception e) {
System.out.println("Romeo: Failed to create own socket " + e);
}
}
//Get acquaintance with lover;
public Pair<InetAddress,Integer> getAcquaintance() {
System.out.println("Romeo: Did my heart love till now? forswear it, sight! For I ne'er saw true beauty till this night.");
return new Pair<>(ownServerSocket.getInetAddress(), port);
}
//Retrieves the lover's love
public double receiveLoveLetter()
{
Socket sock = null;
try {
sock = ownServerSocket.accept();
} catch (IOException e) {
PlayWriter.die(e);
}
assert sock != null;
serviceMailbox = sock;
byte[] input = null;
try {
input = sock.getInputStream().readAllBytes();
} catch (IOException e) {
PlayWriter.die(e);
}
assert input != null;
System.err.printf("[R] RECV %s\n", PlayWriter.bytesToHex(input));
Double res = null;
try {
res = Double.parseDouble(PlayWriter.stringFromByteArray(input).substring(0, input.length - 1));
} catch (NumberFormatException e) {
PlayWriter.die(e);
}
assert res != null;
System.out.println("Romeo: O sweet Juliet... (<-" + res + ")");
return res;
}
//Love (The ODE system)
//Given the lover's love at time t, estimate the next love value for Romeo
public double renovateLove(double partnerLove){
System.out.println("Romeo: But soft, what light through yonder window breaks?\n" +
" It is the east, and Juliet is the sun.");
currentLove = currentLove+(a*partnerLove);
return currentLove;
}
//Communicate love back to playwriter
public void declareLove(){
System.out.println("Romeo: I would I were thy bird");
assert serviceMailbox != null;
SocketChannel chan = serviceMailbox.getChannel();
try {
chan.write(ByteBuffer.wrap((currentLove + "R").getBytes()));
chan.close();
serviceMailbox.close();
} catch (IOException e) {
PlayWriter.die(e);
}
serviceMailbox = null;
}
//Execution
public void run () {
try {
while (!this.isInterrupted()) {
//Retrieve lover's current love
double JulietLove = this.receiveLoveLetter();
//Estimate new love value
this.renovateLove(JulietLove);
//Communicate love back to playwriter
this.declareLove();
}
}catch (Exception e){
System.out.println("Romeo: " + e);
}
if (this.isInterrupted()) {
System.out.println("Romeo: Here's to my love. O true apothecary,\n" +
"Thy drugs are quick. Thus with a kiss I die." );
}
}
}