55import java .util .Properties ;
66
77import de .sveh .simpleserverclient .annotations .Authorized ;
8- import de .sveh .simpleserverclient .datapackage .DataPackage ;
8+ import de .sveh .simpleserverclient .datapackage .AbstractDataPackage ;
99import de .sveh .simpleserverclient .datapackage .MessageDataPackage ;
1010import de .sveh .simpleserverclient .encoding .AESEncoding ;
1111import de .sveh .simpleserverclient .sender .Sender ;
1515
1616public abstract class Client {
1717
18- private static Client instance ;
18+ private static Client instance ;
1919
20- private String host ;
21- private int port ;
20+ private String host ;
21+ private int port ;
2222
23- private boolean authenticated = false ;
23+ private boolean authenticated = false ;
2424
25- private Socket socket ;
26- private BufferedReader in ;
27- private PrintWriter out ;
25+ private Socket socket ;
26+ private BufferedReader in ;
27+ private PrintWriter out ;
2828
29- private ClientState clientState ;
30- private AESEncoding aesEncoding ;
29+ private ClientState clientState ;
30+ private AESEncoding aesEncoding ;
3131
32- private Properties properties ;
32+ private Properties properties ;
3333
34- public Client (String host , int port ) {
35- createClient (host , port );
36- }
34+ public Client (String host , int port ) {
35+ createClient (host , port );
36+ }
3737
38- 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" );
38+ 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" );
4242
43- createClient (host , port );
44- }
43+ createClient (host , port );
44+ }
4545
46- public Client (String propertyFilename ){
47- boolean read = readProperties (propertyFilename );
48- if (read ){
49- String hostProp = properties .getProperty ("host" );
50- String portProp = properties .getProperty ("port" );
46+ public Client (String propertyFilename ) {
47+ boolean read = readProperties (propertyFilename );
48+ if (read ) {
49+ String hostProp = properties .getProperty ("host" );
50+ String portProp = properties .getProperty ("port" );
5151
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- }
59-
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- }
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+ }
6659
67- }
68- }
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+ }
6966
70- private void createClient ( String host , int port ) {
71- instance = this ;
67+ }
68+ }
7269
73- this . host = host ;
74- this . port = port ;
70+ private void createClient ( String host , int port ) {
71+ instance = this ;
7572
76- init () ;
77- }
73+ this . host = host ;
74+ this . port = port ;
7875
76+ init ();
77+ }
7978
80- private boolean readProperties (String propertyFilename ) {
81- properties = new Properties ();
82- File propertyFile = new File (propertyFilename );
83- if (!propertyFile .exists () || propertyFile .isDirectory ()) return false ;
8479
85- try {
86- properties .load (new FileInputStream (propertyFile ));
87- } catch (IOException e ) {
88- return false ;
89- }
90- return true ;
91- }
80+ private boolean readProperties (String propertyFilename ) {
81+ properties = new Properties ();
82+ File propertyFile = new File (propertyFilename );
83+ if (!propertyFile .exists () || propertyFile .isDirectory ()) return false ;
9284
93- public String getProperty (String key ){
94- return properties .getProperty (key );
95- }
85+ try {
86+ properties .load (new FileInputStream (propertyFile ));
87+ } catch (IOException e ) {
88+ return false ;
89+ }
90+ return true ;
91+ }
9692
93+ public String getProperty (String key ) {
94+ return properties .getProperty (key );
95+ }
9796
98- public abstract void onConnected ();
9997
100- public abstract void onDisconnected ();
98+ public abstract void onConnected ();
10199
102- public abstract String onPasswordRequired ();
100+ public abstract void onDisconnected ();
103101
104- public abstract void onAuthenticated ();
102+ public abstract String onPasswordRequired ();
105103
106- private void init () {
107- clientState = ClientState .DISCONNECTED ;
104+ public abstract void onAuthenticated ();
108105
109- this . aesEncoding = new AESEncoding ();
110- }
106+ private void init () {
107+ clientState = ClientState . DISCONNECTED ;
111108
112- public void connect () {
113- if (clientState != ClientState .DISCONNECTED ) {
114- System .out .println ("Client ist bereits verbunden!" );
115- return ;
116- }
109+ this .aesEncoding = new AESEncoding ();
110+ }
117111
118- new Thread (() -> {
112+ public void connect () {
113+ if (clientState != ClientState .DISCONNECTED ) {
114+ System .out .println ("Client is already connected to a server!" );
115+ return ;
116+ }
119117
118+ new Thread (() -> {
120119 try {
121120 socket = new Socket (host , port );
122121 in = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
@@ -126,69 +125,64 @@ public void connect() {
126125
127126 String line ;
128127 while ((line = in .readLine ()) != null ) {
129- DataPackage dataPackage = DataPackage .fromString (line , getAesEncoding ());
130- if (dataPackage != null ) // ERRORhandling server verlassen?
128+ AbstractDataPackage dataPackage = AbstractDataPackage .fromString (line , getAesEncoding ());
129+ if (dataPackage != null ) // TODO errorhandling
131130 dataPackage .onClient (new Sender (SenderType .SERVER ));
132131 }
133132
134133 } catch (IOException e ) {
135-
134+ e . printStackTrace ();
136135 } finally {
137136 try {
138137 socket .close ();
139138 socket = null ;
140- System .out .println ("Verbindung verloren !" );
141- } catch (IOException e1 ) {
142- e1 .printStackTrace ();
143- } catch (NullPointerException e2 ) {
144- System .out .println ("Konnte keine Verbindung zum Server herstellen !" );
139+ System .out .println ("Connection lost !" );
140+ } catch (IOException e ) {
141+ e .printStackTrace ();
142+ } catch (NullPointerException e ) {
143+ System .out .println ("Could not connect to the server !" );
145144 }
146145
147- new Thread () {
148- @ Override
149- public void run () {
150- onDisconnected ();
151- }
152- }.start ();
146+ new Thread (() -> onDisconnected ()).start ();
153147 }
154148 }).start ();
155149
156- }
157-
158- public void sendDataPackage (DataPackage datapackage ) {
159-
160- if ((datapackage .getClass ().isAnnotationPresent (Authorized .class )) && (!isAuthenticated ())) {
161- return ;
162- }
163-
164- if (this .out != null ) {
165- this .out .println (datapackage .toString (getAesEncoding ()));
166- }
167- }
168-
169- public void sendMessage (String message ) {
170- sendDataPackage (new MessageDataPackage (message ));
171- }
172-
173- /**
174- * @return the authentificated
175- */
176- public boolean isAuthenticated () {
177- return authenticated ;
178- }
179-
180- /**
181- * @param authenticated the authentificated to set
182- */
183- public void setAuthenticated (boolean authenticated ) {
184- this .authenticated = authenticated ;
185- }
186-
187- public AESEncoding getAesEncoding () {
188- return this .aesEncoding ;
189- }
190-
191- public static Client getInstance () {
192- return instance ;
193- }
150+ }
151+
152+ public void sendDataPackage (AbstractDataPackage datapackage ) {
153+
154+ if ((datapackage .getClass ().isAnnotationPresent (Authorized .class )) && (!isAuthenticated ())) {
155+ return ;
156+ }
157+
158+ if (this .out != null ) {
159+ this .out .println (datapackage .toString (getAesEncoding ()));
160+ }
161+ }
162+
163+ public void sendMessage (String message ) {
164+ sendDataPackage (new MessageDataPackage (message ));
165+ }
166+
167+ /**
168+ * @return the authentificated
169+ */
170+ public boolean isAuthenticated () {
171+ return authenticated ;
172+ }
173+
174+ /**
175+ * @param authenticated the authentificated to set
176+ */
177+ public void setAuthenticated (boolean authenticated ) {
178+ this .authenticated = authenticated ;
179+ }
180+
181+ public AESEncoding getAesEncoding () {
182+ return this .aesEncoding ;
183+ }
184+
185+ public static Client getInstance () {
186+ return instance ;
187+ }
194188}
0 commit comments