-
-
Save payne911/d71fbc4067ed13865ca51377ec302cc7 to your computer and use it in GitHub Desktop.
HDLC Simulator
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
package devoir.client; | |
import devoir.hdlc.Trame; | |
import devoir.hdlc.specific.ATrame; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
public abstract class Client { | |
private Socket clientSocket; | |
private PrintWriter out; | |
private BufferedReader in; | |
private EnumClient type; | |
public Client(EnumClient type) { | |
this.type = type; | |
} | |
public String getType() { | |
return this.type.getType(); | |
} | |
public void startConnection(String ip, int port) { | |
try { | |
System.out.println(getType() + " has connected to IP " + ip | |
+ " on port " + port); | |
clientSocket = new Socket(ip, port); | |
out = new PrintWriter(clientSocket.getOutputStream(), true); | |
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
} catch (IOException e) { | |
// todo: close connection here in case of error? | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Using "System.out.println()" will print in the ClientMain shell. | |
* | |
* Using "out.println()" will print in the ServerMain shell. The Server | |
* will receive that info through it's "in.readLine()" method. | |
* @param trame | |
* @return The response | |
*/ | |
public String sendFrame(Trame trame) { | |
// outward-bound frame | |
frameStamp(true, trame); | |
out.println(trame); | |
// collecting the response | |
String response = null; | |
try { | |
response = in.readLine(); | |
// todo: translate into a Trame and return that | |
// todo: next 2 lines are temporary, for testing purposes: will be removed eventually | |
ATrame aTrame = new ATrame(trame.getNumAsInt()); | |
frameStamp(false, aTrame); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
/** | |
* Identify messages in the shell to let user know what's happening | |
* behind the scene. | |
* @param isGoingOut 'true' if frame is sent to 'out' | |
* 'false' if frame is receive through 'in' | |
* @param trame used to extract relevant information "(Type,Num)" | |
*/ | |
public void frameStamp(boolean isGoingOut, Trame trame) { | |
// direction | |
String direction = isGoingOut ? "OUT" : "IN "; | |
// timestamp | |
String now = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()).toString(); | |
String hours = now.substring(11,24); | |
// frame type | |
String frame = "(" + trame.getType() + "," + trame.getNumAsInt() + ")"; | |
// print the actual "frame stamp" | |
System.out.println("[ " + direction + " @ " + hours + " ] " + frame + " " + trame.toString()); | |
} | |
public void stopConnection() { | |
// lazy try-catch :D | |
try { | |
in.close(); | |
out.close(); | |
clientSocket.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment