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

Manwithhonor threads #192

Open
wants to merge 22 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
45 changes: 45 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Compiled class file

*.class


# Log file

*.log


# BlueJ files

*.ctxt


# Mobile Tools for Java
(J2ME)
.mtj.tmp/


# Package Files #

*.jar

*.war

*.ear

*.zip

*.tar.gz

*.rar


# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml

hs_err_pid*

**/*.iml
**/.idea/
**/target/
*.DS_Store
*.iml
/*.iml
125 changes: 121 additions & 4 deletions L7_threads/src/main/java/ru/track/prefork/Client.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,131 @@
package ru.track.prefork;
import ru.track.prefork.Protocol.Message;
import ru.track.prefork.Protocol.Protocol;
import ru.track.prefork.Protocol.MySerializationProtocol;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;


/**
*
*/
public class Client {
private static Protocol<Message> protocol = new MySerializationProtocol<>();
public boolean myError;
static Logger log = LoggerFactory.getLogger(Client.class);
private int port;
private String host;
public Socket socket;
public String exNextLine ="";

public Client(int port, String host) {
public Client( @NotNull String host, int port) {
this.myError = false;
this.port = port;
this.host = host;
this.socket = null;

}


public void workInCycle() {


try {
socket = new Socket(String.valueOf(host), port);
} catch (IOException e) {
System.out.println(e.getMessage());
}
myClientReader myClientReader = new myClientReader();
myClientReader.start();

try (
Scanner myScanner = new Scanner(System.in);
OutputStream out = socket.getOutputStream();
) {
while (true) {
String myNextLine = myScanner.nextLine();
if (myExit(myNextLine))
myError = true;
if (myError) {
break;
}
out.write(myNextLine.getBytes());
out.flush();

}
} catch (Exception e) {
myError = true;
System.out.println(e.getMessage());
}

}


private boolean myExit(String message) {
if (message.equals("exit") )
return true;
return false;

}


private class myClientReader extends Thread {

InputStream myStream;

private myClientReader() {
myStream = null;
}

@Override
public void run() {
myClientReaderFunc();
}

private void myClientReaderFunc() {
try {
while (true) {
myStream = socket.getInputStream();
byte[] myBuffer = new byte[1024];
int amountOfBytes = myStream.read(myBuffer);



if ( myError || !(amountOfBytes >= 0) ) {
if ( amountOfBytes >= 0) {
Message message = protocol.decode(myBuffer);
if ( message.toString().equals("Your connection was terminated, sorry")){
System.out.println(message.toString());
socket.close();
break;
}
}
log.info("Your connection was terminated, sorry");
myError = true;
break;
}
Message message = protocol.decode(myBuffer);
System.out.println(message.toString());

}
} catch (IOException e) {
System.out.println(e.getMessage());
myError = true;
}
finally {
try {
myStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws Exception {
final Client myClient = new Client("localhost", 8000);
myClient.workInCycle();
}

}
30 changes: 30 additions & 0 deletions L7_threads/src/main/java/ru/track/prefork/Protocol/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.track.prefork.Protocol;

import com.sun.mail.iap.ByteArray;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.Arrays;

public class Message implements Serializable{
private long senderId;
private int length;
private String msg;

public Message (byte[] content, long senderId, int length) {
this.senderId = senderId;
this.length = length;
this.msg = new String(content, 0, length);
}

public Message (Message message) {
this.senderId = message.senderId;
this.length = message.length;
this.msg = new String(message.msg);
}

@Override
public String toString() { return msg; }

public long senderId() { return senderId; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.track.prefork.Protocol;

import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.net.ProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MySerializationProtocol<T extends Serializable> implements Protocol<T> {
static final Logger log = LoggerFactory.getLogger(MySerializationProtocol.class);
@Override
public byte[] encode(T msg) throws ProtocolException, IOException {
log.info("encode:" + msg);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutputStream objOut = new ObjectOutputStream(bos)) {
objOut.writeObject(msg);
return bos.toByteArray();
} catch (IOException e) {
throw new ProtocolException("encoding failed");
}
}

@Nullable
@Override
public T decode(byte[] data) throws ProtocolException, IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
try (ObjectInputStream objIn = new ObjectInputStream(bis)){
T message = (T) objIn.readObject();
log.info(String.format("decode \"%s\"", message.toString()));
return message;
} catch (ClassNotFoundException e) {
throw new ProtocolException("decoding failed");
}
}
}
14 changes: 14 additions & 0 deletions L7_threads/src/main/java/ru/track/prefork/Protocol/Protocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.track.prefork.Protocol;

import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.Serializable;
import java.net.ProtocolException;

public interface Protocol<T extends Serializable> {
byte[] encode(T msg) throws ProtocolException, IOException;

@Nullable
T decode(byte[] data) throws ProtocolException, IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.track.prefork.Protocol;

public class ProtocolException extends Exception {
public ProtocolException(String message) {
super(message);
}

public ProtocolException(String message, Throwable cause) {
super(message, cause);
}
}
Loading