22
33import java .io .*;
44import java .net .Socket ;
5- import java .util .Properties ;
65
76import de .sveh .simpleserverclient .annotations .Authorized ;
7+ import de .sveh .simpleserverclient .config .IConfiguration ;
8+ import de .sveh .simpleserverclient .config .PropertyConfiguration ;
89import de .sveh .simpleserverclient .datapackage .AbstractDataPackage ;
910import de .sveh .simpleserverclient .datapackage .MessageDataPackage ;
1011import de .sveh .simpleserverclient .encoding .AESEncoding ;
@@ -29,42 +30,40 @@ public abstract class Client {
2930 private ClientState clientState ;
3031 private AESEncoding aesEncoding ;
3132
32- private Properties properties ;
33+ private IConfiguration properties ;
3334
3435 public Client (String host , int port ) {
3536 createClient (host , port );
3637 }
3738
3839 public Client (String host , int port , String propertyFilename ) {
39- boolean read = readProperties (propertyFilename );
40- if (!read )
41- ILogger .log (LogType .ERROR , "Could not read the property file" );
40+ properties = new PropertyConfiguration (propertyFilename );
4241
4342 createClient (host , port );
4443 }
4544
4645 public Client (String propertyFilename ) {
47- boolean read = readProperties (propertyFilename );
48- if (read ) {
49- String hostProp = properties .getProperty ("host" );
50- String portProp = properties .getProperty ("port" );
51-
52- if (hostProp == null ) {
53- ILogger .log (LogType .ERROR , "No host property found" );
54- return ;
55- } else if (portProp == null ) {
56- ILogger .log (LogType .ERROR , "No port property found" );
57- return ;
58- }
46+ properties = new PropertyConfiguration (propertyFilename );
5947
60- try {
61- int portNo = Integer .parseInt (portProp );
62- createClient (hostProp , portNo );
63- } catch (Exception e ) {
64- ILogger .log (LogType .ERROR , "Port property is not a number" );
65- }
48+ String hostProp = properties .getProperty ("host" );
49+ String portProp = properties .getProperty ("port" );
6650
51+ if (hostProp == null ) {
52+ ILogger .log (LogType .ERROR , "No host property found" );
53+ return ;
54+ } else if (portProp == null ) {
55+ ILogger .log (LogType .ERROR , "No port property found" );
56+ return ;
6757 }
58+
59+ try {
60+ int portNo = Integer .parseInt (portProp );
61+ createClient (hostProp , portNo );
62+ } catch (Exception e ) {
63+ ILogger .log (LogType .ERROR , "Port property is not a number" );
64+ }
65+
66+
6867 }
6968
7069 private void createClient (String host , int port ) {
@@ -76,25 +75,10 @@ private void createClient(String host, int port) {
7675 init ();
7776 }
7877
79-
80- private boolean readProperties (String propertyFilename ) {
81- properties = new Properties ();
82- File propertyFile = new File (propertyFilename );
83- if (!propertyFile .exists () || propertyFile .isDirectory ()) return false ;
84-
85- try {
86- properties .load (new FileInputStream (propertyFile ));
87- } catch (IOException e ) {
88- return false ;
89- }
90- return true ;
91- }
92-
9378 public String getProperty (String key ) {
9479 return properties .getProperty (key );
9580 }
9681
97-
9882 public abstract void onConnected ();
9983
10084 public abstract void onDisconnected ();
@@ -121,7 +105,7 @@ public void connect() {
121105 in = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
122106 out = new PrintWriter (socket .getOutputStream (), true );
123107
124- new Thread (() -> onConnected () ).start ();
108+ new Thread (this :: onConnected ).start ();
125109
126110 String line ;
127111 while ((line = in .readLine ()) != null ) {
@@ -143,7 +127,7 @@ public void connect() {
143127 System .out .println ("Could not connect to the server!" );
144128 }
145129
146- new Thread (() -> onDisconnected () ).start ();
130+ new Thread (this :: onDisconnected ).start ();
147131 }
148132 }).start ();
149133
0 commit comments