14
14
import java .net .ServerSocket ;
15
15
import java .net .Socket ;
16
16
import java .util .logging .Level ;
17
- import static serverApp .App .LOGGER ;
17
+ import static serverApp .App .logger ;
18
18
19
19
20
20
@ RequiredArgsConstructor
@@ -24,20 +24,12 @@ public class Server {
24
24
private ServerSocket serverSocket ;
25
25
26
26
27
- public void run () throws IOException {
27
+ public void run () throws IOException , ClassNotFoundException {
28
28
boolean status = true ;
29
29
openServerSocket ();
30
30
while (status ) {
31
- try {
32
- Socket clientSocket = connectToClient ();
33
- status = processClientRequest (clientSocket );
34
- } catch (ClassNotFoundException e ) {
35
- LOGGER .log (Level .SEVERE , e .getMessage ());
36
- status = false ;
37
- } catch (IOException e ) {
38
- LOGGER .log (Level .WARNING , e .getMessage ());
39
- status = false ;
40
- }
31
+ Socket clientSocket = connectToClient ();
32
+ status = processClientRequest (clientSocket );
41
33
}
42
34
saveIfExit ();
43
35
}
@@ -46,21 +38,21 @@ private void openServerSocket() throws IOException {
46
38
try {
47
39
serverSocket = new ServerSocket (port );
48
40
} catch (IOException e ) {
49
- throw new IOException ( "Failed to open ServerSocket." );
41
+ logger . log ( Level . WARNING , "Failed to open ServerSocket." );
50
42
}
51
43
}
52
44
53
- private void saveIfExit () throws IOException {
45
+ private void saveIfExit () throws IOException , ClassNotFoundException {
54
46
try {
55
47
collectionManager .saveCollection ();
56
- LOGGER .log (Level .INFO , "The collection was saved" );
48
+ logger .log (Level .INFO , "The collection was saved" );
57
49
} catch (FileNotFoundException e ) {
58
- LOGGER .log (Level .SEVERE , "This file wasn't found" );
50
+ logger .log (Level .SEVERE , "This file wasn't found" );
59
51
} catch (SecurityException e ) {
60
- LOGGER .log (Level .SEVERE , "Write access to the file is denied" );
52
+ logger .log (Level .SEVERE , "Write access to the file is denied" );
61
53
} catch (IOException e ) {
62
54
e .printStackTrace ();
63
- LOGGER .log (Level .SEVERE , "Some I/O errors occur: " + e .getMessage ());
55
+ logger .log (Level .SEVERE , "Some I/O errors occur: " + e .getMessage ());
64
56
}
65
57
exit ();
66
58
run ();
@@ -71,16 +63,16 @@ private void exit() {
71
63
if (serverSocket != null ) {
72
64
serverSocket .close ();
73
65
}
74
- LOGGER .log (Level .INFO , "The connection with the client closed." );
66
+ logger .log (Level .INFO , "The connection with the client closed." );
75
67
76
68
} catch (IOException e ) {
77
- LOGGER .log (Level .INFO , "Error when completing the connection with the client." );
69
+ logger .log (Level .INFO , "Error when completing the connection with the client." );
78
70
}
79
71
}
80
72
81
73
private Socket connectToClient () throws IOException {
82
74
Socket clientSocket = serverSocket .accept ();
83
- LOGGER .log (Level .INFO , "The connection with the client has been successfully established" );
75
+ logger .log (Level .INFO , "The connection with the client has been successfully established" );
84
76
return clientSocket ;
85
77
}
86
78
@@ -102,9 +94,11 @@ private boolean processClientRequest(Socket clientSocket) throws IOException, Cl
102
94
clientWriter .flush ();
103
95
} while (true );
104
96
} catch (IOException e ) {
105
- throw new IOException ("Disconnection from the client." );
97
+ logger .log (Level .WARNING , "Disconnection from the client." );
98
+ return false ;
106
99
} catch (ClassNotFoundException e ) {
107
- throw new ClassNotFoundException ("An error occurred while reading the received data!" );
100
+ logger .log (Level .SEVERE ,"An error occurred while reading the received data!" );
101
+ return false ;
108
102
}
109
103
}
110
104
}
0 commit comments