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

Java 8 #2

Open
wants to merge 9 commits into
base: master
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.solvd.lab.v2.automation</groupId>
<artifactId>autolab2</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>


</project>
30 changes: 30 additions & 0 deletions src/main/java/com/solvd/lab/v2/automation/classes/c10/Chat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.solvd.lab.v2.automation.classes.c10;

import com.solvd.lab.v2.automation.classes.c10.bo.ConnectMessage;
import com.solvd.lab.v2.automation.classes.c10.bo.ResponseMessage;
import com.solvd.lab.v2.automation.constant.TimeConstant;
import com.solvd.lab.v2.automation.io.impl.stream.ObjectReader;
import com.solvd.lab.v2.automation.io.interfaces.Packable;
import com.solvd.lab.v2.automation.util.SerializationUtil;

public class Chat {
public static void main(String[] args) throws InterruptedException {
while (true) {
Packable obj1 = SerializationUtil.readObject(SerializationUtil.getREADER_RESPONSE1());
Packable obj2 = SerializationUtil.readObject(SerializationUtil.getREADER_RESPONSE2());
Packable[] obje = {obj1, obj2};
for(Packable obj:obje){
if (obj != null) {
if(obj == obje[0]){
System.out.println("client 2: "+((ResponseMessage) Client1.getResponse()).getResp());
SerializationUtil.writeObject(null, SerializationUtil.getREADER_RESPONSE1().getPath());
} else {
System.out.println("client 1: "+((ResponseMessage) Client2.getResponse()).getResp());
SerializationUtil.writeObject(null, SerializationUtil.getREADER_RESPONSE2().getPath());
}
}
}

}
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/solvd/lab/v2/automation/classes/c10/Client1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.solvd.lab.v2.automation.classes.c10;

import com.solvd.lab.v2.automation.classes.c10.bo.*;
import com.solvd.lab.v2.automation.constant.C10Constant;
import com.solvd.lab.v2.automation.constant.TimeConstant;
import com.solvd.lab.v2.automation.io.interfaces.Packable;
import com.solvd.lab.v2.automation.util.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

/**
* 1. object streams
* 2. task
* 3. swap strings
* 33. loggers (stdin, stdout, stderr)
* 4. refactoring
* 5. fixes
*/
public class Client1 {

public static void main(String[] args) {
final String HOST = PropertyUtil.getValueByKey(C10Constant.HOSTNAME);
final int PORT = Integer.valueOf(PropertyUtil.getValueByKey(C10Constant.PORT));
final String TOKEN = PropertyUtil.getValueByKey(C10Constant.TOKEN);
String path = "src/main/resources/client1";
Path p = Paths.get(path);
try {
Files.createFile(p);
} catch (IOException e) {
e.printStackTrace();
}

while(true){
connect(HOST, PORT, TOKEN);
}
}

private static void connect(final String host, final int port, final String token) {
Scanner scan = new Scanner(System.in);
System.out.print("you: ");
String msg = scan.nextLine();
Packable pkg = new ConnectMessage(host, port, token, msg);
SerializationUtil.writeObject(pkg, SerializationUtil.getREADER1().getPath());
}

public static Packable getResponse() {
return SerializationUtil.readResponse(SerializationUtil.getREADER_RESPONSE1());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.solvd.lab.v2.automation.classes.c10.bo.ConnectMessage;
import com.solvd.lab.v2.automation.classes.c10.bo.ResponseMessage;
import com.solvd.lab.v2.automation.constant.C10Constant;
import com.solvd.lab.v2.automation.constant.TimeConstant;
import com.solvd.lab.v2.automation.io.interfaces.Packable;
import com.solvd.lab.v2.automation.util.PropertyUtil;
import com.solvd.lab.v2.automation.util.SerializationUtil;
Expand All @@ -11,42 +12,36 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

/**
* 1. object streams
* 2. task
* 3. swap strings
* 33. loggers (stdin, stdout, stderr)
* 4. refactoring
* 5. fixes
*/
public class Client {

public class Client2 {
public static void main(String[] args) {
final String HOST = PropertyUtil.getValueByKey(C10Constant.HOSTNAME);
final int PORT = Integer.valueOf(PropertyUtil.getValueByKey(C10Constant.PORT));
final String TOKEN = PropertyUtil.getValueByKey(C10Constant.TOKEN);

String path = "src/main/resources/client1";
String path = "src/main/resources/client2";
Path p = Paths.get(path);
try {
Files.createFile(p);
} catch (IOException e) {
e.printStackTrace();
}


connect(HOST, PORT, TOKEN);
System.out.println(((ResponseMessage) getResponse()).getResp());
while(true){
connect(HOST, PORT, TOKEN);
}
}

private static void connect(final String host, final int port, final String token) {
String msg = "Conn";
Scanner scan = new Scanner(System.in);
System.out.print("you: ");
String msg = scan.nextLine();
Packable pkg = new ConnectMessage(host, port, token, msg);
SerializationUtil.writeObject(pkg);
SerializationUtil.writeObject(pkg, "src/main/resources/client2");

}

private static Packable getResponse() {
return SerializationUtil.readResponse();
public static Packable getResponse() {
return SerializationUtil.readResponse(SerializationUtil.getREADER_RESPONSE2());
}
}
51 changes: 31 additions & 20 deletions src/main/java/com/solvd/lab/v2/automation/classes/c10/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.solvd.lab.v2.automation.classes.c10.bo.ConnectMessage;
import com.solvd.lab.v2.automation.classes.c10.bo.ResponseMessage;
import com.solvd.lab.v2.automation.constant.TimeConstant;
import com.solvd.lab.v2.automation.io.impl.stream.ObjectReader;
import com.solvd.lab.v2.automation.io.interfaces.Packable;
import com.solvd.lab.v2.automation.util.SerializationUtil;

import javax.print.DocFlavor;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
Expand All @@ -15,37 +18,45 @@ public class Server {

private static final List<String> AVAILABLE_CLIENTS = Arrays.asList("user");
private static final String HOST = "127.0.0.1";
private static final int PORT = 8000;
private static final int PORT1 = 8000;
private static final int PORT2 = 8000;


public static void main(String[] args) {
LOGGER.info(String.format("Listening on %s:%d", HOST, PORT));
LOGGER.info(String.format("Listening on %s:%d:%d", HOST, PORT1, PORT2));
while (true) {

try {
listen();
Thread.sleep(TimeConstant.TIME_TO_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
listen();
}
}

// TODO: filter msgs
private static void listen() {
Packable obj = SerializationUtil.readObject();
if (obj != null) {
ConnectMessage msg = ((ConnectMessage) obj);
if (msg.getHost().equals(HOST) && msg.getPort() == PORT && AVAILABLE_CLIENTS.contains(msg.getToken())) {
LOGGER.info(msg.getMessage());
Packable resp = new ResponseMessage(HOST, PORT, "", "SUCCESS", 200);
sendResponse(resp);
}

Packable obj1 = SerializationUtil.readObject(SerializationUtil.getREADER1());
Packable obj2 = SerializationUtil.readObject(SerializationUtil.getREADER2());
Packable[] obje = {obj1, obj2};

for(Packable obj:obje){
if (obj != null) {
ConnectMessage msg = (ConnectMessage) obj;
if (msg.getHost().equals(HOST) && msg.getPort() == PORT1 && AVAILABLE_CLIENTS.contains(msg.getToken())) {
LOGGER.info(msg.getMessage());
Packable resp = new ResponseMessage(HOST, PORT2, "", msg.getMessage(), 200);
if (obj == obje[0]){
SerializationUtil.writeObject(resp,SerializationUtil.getREADER_RESPONSE2().getPath());
clearBuffer(SerializationUtil.getREADER1());
} else {
SerializationUtil.writeObject(resp,SerializationUtil.getREADER_RESPONSE1().getPath());
clearBuffer(SerializationUtil.getREADER2());
}
}
}
}

}

private static void sendResponse(Packable pkg) {
SerializationUtil.writeResponse(pkg);
public static void clearBuffer(ObjectReader objr){
SerializationUtil.writeObject(null, objr.getPath());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.solvd.lab.v2.automation.constant;

public class TimeConstant {
public static final int TIME_TO_DELAY = 10000;
public static final int TIME_TO_DELAY = 1000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

public class PropertyFileReader extends BaseReader implements Reader {
private Properties props;

public PropertyFileReader(File file) {
super(file);
}

public PropertyFileReader(String path) {
super(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

import java.io.*;

public class ObjectReader extends BaseReader implements StreamReader {
public ObjectReader(File file) {
super(file);
}

public class ObjectReader extends BaseReader implements StreamReader, Packable {
public ObjectReader(String path) {
super(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,57 @@
import com.solvd.lab.v2.automation.io.interfaces.Packable;

public class SerializationUtil {
private static final String DATA_FILE_PATH = System.getProperty("user.dir") + "/src/main/resources/serial";
private static final String DATA_FILE_PATH_RESPONSE = System.getProperty("user.dir") + "/src/main/resources/serial_response";
private static final String DATA_FILE_PATH1 = System.getProperty("user.dir") + "/src/main/resources/client1";
private static final String DATA_FILE_PATH2 = System.getProperty("user.dir") + "/src/main/resources/client2";
private static final String DATA_FILE_PATH_RESPONSE1 = System.getProperty("user.dir") + "/src/main/resources/serial_response1";
private static final String DATA_FILE_PATH_RESPONSE2 = System.getProperty("user.dir") + "/src/main/resources/serial_response2";

private static final ObjectReader READER = new ObjectReader(DATA_FILE_PATH);
private static final ObjectReader READER_RESPONSE = new ObjectReader(DATA_FILE_PATH_RESPONSE);

public static ObjectReader getREADER_RESPONSE1() { return READER_RESPONSE1; }
public static ObjectReader getREADER_RESPONSE2() { return READER_RESPONSE2; }
public static ObjectReader getREADER1() { return READER1; }
public static ObjectReader getREADER2() { return READER2; }

public static void writeObject(Packable obj) {
private static final ObjectReader READER1 = new ObjectReader(DATA_FILE_PATH1);
private static final ObjectReader READER2 = new ObjectReader(DATA_FILE_PATH2);
private static final ObjectReader READER_RESPONSE1 = new ObjectReader(DATA_FILE_PATH_RESPONSE1);
private static final ObjectReader READER_RESPONSE2 = new ObjectReader(DATA_FILE_PATH_RESPONSE2);

public static void writeObject(Packable obj, String path) {
try {
new ObjectWriter().write(DATA_FILE_PATH, obj);
new ObjectWriter().write(path, obj);
} catch (UnableToWriteException e) {
e.printStackTrace();
throw new RuntimeException(String.format("%s is unable to write!", DATA_FILE_PATH));
throw new RuntimeException(String.format("%s is unable to write!", path));
}
}

public static Packable readObject() {
public static Packable readObject(ObjectReader objr) {
try {
return READER.read();
return objr.read();
} catch (UnableToReadException e) {
e.printStackTrace();
throw new RuntimeException(String.format("%s is unable to read!", DATA_FILE_PATH));
throw new RuntimeException(String.format("%s is unable to read!", objr.getPath()));
}

}

public static Packable readResponse() {
public static Packable readResponse(ObjectReader objr) {
try {
return READER_RESPONSE.read();
return objr.read();
} catch (UnableToReadException e) {
e.printStackTrace();
throw new RuntimeException(String.format("%s is unable to read!", DATA_FILE_PATH_RESPONSE));
throw new RuntimeException(String.format("%s is unable to read!", objr.getPath()));
}

}

public static void writeResponse(Packable obj) {
public static void writeResponse(Packable obj, ObjectReader objr) {
try {
new ObjectWriter().write(DATA_FILE_PATH_RESPONSE, obj);
new ObjectWriter().write(objr.getPath(), obj);
} catch (UnableToWriteException e) {
e.printStackTrace();
throw new RuntimeException(String.format("%s is unable to write!", DATA_FILE_PATH_RESPONSE));
throw new RuntimeException(String.format("%s is unable to write!", objr.getPath()));
}
}

}
Binary file modified src/main/resources/serial
Binary file not shown.
Binary file removed src/main/resources/serial_response
Binary file not shown.
File renamed without changes.
Empty file.