Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 6 is ready #31

Open
wants to merge 1 commit into
base: b21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Client/Client.jar
Binary file not shown.
27 changes: 27 additions & 0 deletions Client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>org.example</groupId>
<artifactId>mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
</project>
3 changes: 3 additions & 0 deletions Client/src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: sample.Main

247 changes: 247 additions & 0 deletions Client/src/main/java/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package sample;

import com.sun.jdi.event.ExceptionEvent;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.SerializationUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.*;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;

public class Controller implements Initializable {
@FXML
Pane pane;
@FXML
Button buttonLoad;
@FXML
Label labelInfo;
@FXML
TextField textFieldServerIp;
@FXML
ComboBox comboBoxMode;
String[] modes;
//Label labelClientId;
//TextField textFieldClientId;
HBox hBoxBottom;
JSONObject jsonObjectRates;
public void getDateFromServer() {
try {
Socket socket = new Socket(getServerIpString(), 5000);
Calendar calendar = ((Calendar) getObjectFromServer(socket));
labelInfo.setText(" Seconds : " + calendar.get(Calendar.SECOND) + " Minutes : "
+ calendar.get(Calendar.MINUTE) + " Hours : " + calendar.get(Calendar.HOUR)
+ " Day : " + calendar.get(Calendar.DAY_OF_MONTH) + " Month : " + calendar.get(Calendar.MONTH)
+ " Year : " + calendar.get(Calendar.YEAR));
socket.close();
} catch (Exception ex) {
labelInfo.setText("Error. Couldn't load data from server." + ex.getMessage());
}
}

public void getAphorismFromServer() {
try {
Socket socket = new Socket(getServerIpString(), 6000);
String aphorism = (String) getObjectFromServer(socket);
labelInfo.setText(aphorism);
socket.close();
} catch (Exception ex) {
labelInfo.setText("Error. Couldn't load data from server." + ex.getMessage());
}
}

private Object getObjectFromServer(Socket socket) throws Exception {
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
int packetSize = dataInputStream.readInt();
byte[] bytes = new byte[packetSize];
dataInputStream.read(bytes);
return SerializationUtils.deserialize(bytes);
}

private String getServerIpString() {
String serverIp;
if (textFieldServerIp.getText() == "")
serverIp = "localhost";
else
serverIp = textFieldServerIp.getText();
return serverIp;
}

private void listenToServerEvents() {

try {
labelInfo.setText("");
Socket socket = new Socket(getServerIpString(), 7000);
Thread readingThread = new Thread() {
public void run() {
try {
while (true) {
TextField textFieldId = (TextField) hBoxBottom.getChildren().get(1);
sendData(socket, Integer.parseInt(textFieldId.getText()));
String serverMessage = (String) getObjectFromServer(socket);
if (!serverMessage.equals(""))
Platform.runLater(() -> labelInfo.setText(labelInfo.getText() + "\n" + serverMessage));
}
} catch (Exception ex) {

}
}
};
readingThread.start();
} catch (Exception ex) {
labelInfo.setText(labelInfo.getText() + "\n" + ex.getMessage());
}
}

private void sendData(Socket socket, Object object) throws Exception {
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
byte[] bytes = SerializationUtils.serialize((Serializable) object);
dataOutputStream.writeInt(bytes.length);
dataOutputStream.write(bytes);
dataOutputStream.flush();
}

public void setButtonEvent() {

switch ((String) comboBoxMode.getSelectionModel().getSelectedItem()) {
case "Date":
deleteBottomLine();
buttonLoad.setOnAction((e) -> getDateFromServer());
break;
case "Aphorism":
deleteBottomLine();
buttonLoad.setOnAction((e) -> getAphorismFromServer());
break;
case "Events log": {
deleteBottomLine();
createClientIdLine();
buttonLoad.setOnAction((e) -> listenToServerEvents());
break;
}
case "Currency": {
deleteBottomLine();
createCurrencyLine();
buttonLoad.setOnAction((e) -> showCurrencyRelation());
break;
}
}
}
private void showCurrencyRelation()
{
try {
double leftPrice = 1.0 / getCurrencyValue(getCurrencyCombobox(0).getSelectionModel().getSelectedItem());
double rightPrice = 1.0 / getCurrencyValue(getCurrencyCombobox(1).getSelectionModel().getSelectedItem());
labelInfo.setText("1 = " + rightPrice / leftPrice);
}
catch (Exception ex)
{
labelInfo.setText("Please set values properly. "+ex.getMessage());
}
}
private void createClientIdLine() {
if (hBoxBottom == null) {
hBoxBottom = new HBox();
Label labelClientId = new Label();
labelClientId.setText("Client id");
TextField textFieldClientId = new TextField();
hBoxBottom.getChildren().addAll(labelClientId, textFieldClientId);
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().add(hBoxBottom);
}
}
private double getCurrencyValue(String currency)
{
return jsonObjectRates.getDouble(currency);
}
private void createCurrencyLine() {
if (hBoxBottom == null) {
hBoxBottom = new HBox();
ComboBox comboBoxLeftValue = new ComboBox();
comboBoxLeftValue.setMinSize(pane.getWidth() / 2, comboBoxLeftValue.getHeight());
ComboBox comboBoxRightValue = new ComboBox();
comboBoxRightValue.setMinSize(pane.getWidth() / 2, comboBoxLeftValue.getHeight());
hBoxBottom.getChildren().addAll(comboBoxLeftValue, comboBoxRightValue);
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().add(hBoxBottom);
ArrayList<String> values=getCurrecniesListFromSite();
ComboBox<String> comboBoxLeft=getCurrencyCombobox(0);
ComboBox<String> comboBoxRight=getCurrencyCombobox(1);
comboBoxLeft.getItems().addAll(values);
comboBoxRight.getItems().addAll(values);
}
}

private void deleteBottomLine() {
if (hBoxBottom != null) {
VBox vBox = (VBox) pane.getChildren().get(0);
vBox.getChildren().remove(hBoxBottom);
hBoxBottom = null;
}
}
private ComboBox<String> getCurrencyCombobox(int index)
{
return (ComboBox<String>) hBoxBottom.getChildren().get(index);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
labelInfo.setWrapText(true);
modes = new String[]{"Date", "Aphorism", "Events log", "Currency"};
comboBoxMode.getItems().addAll(modes);
}

private ArrayList<String> getCurrecniesListFromSite() {
try {
jsonObjectRates = getJsonObjectRates();
Iterator iterator = jsonObjectRates.keys();
ArrayList<String> names = new ArrayList<String>();
while (iterator.hasNext()) {
names.add( (String) iterator.next());
}
return names;
} catch (Exception ex) {
}
return null;
}
private JSONObject getJsonObjectRates() throws Exception
{
JSONObject jsonObject = readJsonFromUrl("https://openexchangerates.org/api/latest.json?app_id=5a11aa4d56564bbdb101289a6fa338d7");
JSONObject jsonRates = jsonObject.getJSONObject("rates");
return jsonRates;
}
//Copied code
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}
50 changes: 50 additions & 0 deletions Client/src/main/java/sample/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public File getResourceAsFile() {
try {
InputStream inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("/sample.fml");
if (inStream == null) {
return null;
}
File tempFile = File.createTempFile(String.valueOf(inStream.hashCode()), ".tmp");
tempFile.deleteOnExit();

try (FileOutputStream outStream = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
return tempFile;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


public static void main(String[] args) {
launch(args);
}
}
3 changes: 3 additions & 0 deletions Client/src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: sample.Main

35 changes: 35 additions & 0 deletions Client/src/main/resources/sample.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="421.0" prefWidth="238.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox layoutY="-4.0" prefHeight="421.0" prefWidth="234.0">
<children>
<HBox alignment="CENTER" prefHeight="24.0" prefWidth="419.0">
<children>
<ComboBox fx:id="comboBoxMode" onAction="#setButtonEvent" prefHeight="26.0" prefWidth="156.0" />
<Button fx:id="buttonLoad" mnemonicParsing="false" prefHeight="26.0" prefWidth="158.0" text="Get data" />
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="49.0" prefWidth="419.0">
<children>
<Label alignment="CENTER" prefHeight="50.0" prefWidth="51.0" text="Host IP : " />
<TextField fx:id="textFieldServerIp" prefHeight="26.0" prefWidth="173.0" text="localhost" />
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="305.0" prefWidth="234.0">
<children>
<Label fx:id="labelInfo" alignment="CENTER" prefHeight="370.0" prefWidth="234.0" />
</children>
</HBox>
</children>
</VBox>
</children>
</Pane>
6 changes: 6 additions & 0 deletions Server/Aphorisms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Actions speak louder than words
All for one and one for all
Don't fire until you see the whites of their eyes
Early to bed, early to rise, makes a man healthy, wealthy, and wise
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime
Give him an inch and he'll take a mile
Binary file added Server/NetCLockServer.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions Server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>org.example</groupId>
<artifactId>mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>

</project>
Loading