2
2
3
3
import java .io .*;
4
4
import java .net .Socket ;
5
- import java .util .Properties ;
6
5
7
6
import de .sveh .simpleserverclient .annotations .Authorized ;
7
+ import de .sveh .simpleserverclient .config .IConfiguration ;
8
+ import de .sveh .simpleserverclient .config .PropertyConfiguration ;
8
9
import de .sveh .simpleserverclient .datapackage .AbstractDataPackage ;
9
10
import de .sveh .simpleserverclient .datapackage .MessageDataPackage ;
10
11
import de .sveh .simpleserverclient .encoding .AESEncoding ;
@@ -29,42 +30,40 @@ public abstract class Client {
29
30
private ClientState clientState ;
30
31
private AESEncoding aesEncoding ;
31
32
32
- private Properties properties ;
33
+ private IConfiguration properties ;
33
34
34
35
public Client (String host , int port ) {
35
36
createClient (host , port );
36
37
}
37
38
38
39
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 );
42
41
43
42
createClient (host , port );
44
43
}
45
44
46
45
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 );
59
47
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" );
66
50
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 ;
67
57
}
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
+
68
67
}
69
68
70
69
private void createClient (String host , int port ) {
@@ -76,25 +75,10 @@ private void createClient(String host, int port) {
76
75
init ();
77
76
}
78
77
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
-
93
78
public String getProperty (String key ) {
94
79
return properties .getProperty (key );
95
80
}
96
81
97
-
98
82
public abstract void onConnected ();
99
83
100
84
public abstract void onDisconnected ();
@@ -121,7 +105,7 @@ public void connect() {
121
105
in = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
122
106
out = new PrintWriter (socket .getOutputStream (), true );
123
107
124
- new Thread (() -> onConnected () ).start ();
108
+ new Thread (this :: onConnected ).start ();
125
109
126
110
String line ;
127
111
while ((line = in .readLine ()) != null ) {
@@ -143,7 +127,7 @@ public void connect() {
143
127
System .out .println ("Could not connect to the server!" );
144
128
}
145
129
146
- new Thread (() -> onDisconnected () ).start ();
130
+ new Thread (this :: onDisconnected ).start ();
147
131
}
148
132
}).start ();
149
133
0 commit comments