Coverage Summary for Class: SocketWrapper (it.polimi.ingsw.Network)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
SocketWrapper |
0%
(0/1)
|
0%
(0/6)
|
0%
(0/2)
|
0%
(0/21)
|
1 package it.polimi.ingsw.Network;
2
3 import it.polimi.ingsw.Logger;
4 import it.polimi.ingsw.Server.Messages.Message;
5
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.net.InetAddress;
10 import java.net.Socket;
11 import java.net.SocketException;
12 import java.util.Objects;
13
14 /**
15 * Wrapper around a {@link Socket} transfering {@link Message}s, removes most of the overhead of handling the connection.
16 */
17 public class SocketWrapper {
18 private final Socket sock;
19 private final ObjectInputStream input;
20 private final ObjectOutputStream output;
21
22 /**
23 * Create the SocketWrapper around a standard socket
24 *
25 * @param socket the socket to wrap around
26 * @throws IOException if the socket/wrapper has issues opening its streams
27 */
28 public SocketWrapper(Socket socket) throws IOException {
29 this.sock = socket;
30 // get output writer
31 this.output = new ObjectOutputStream(this.sock.getOutputStream());
32 // get the input reader
33 this.input = new ObjectInputStream(socket.getInputStream());
34 }
35
36 /**
37 * Blocks until a message is read
38 *
39 * @return the read {@link Message}
40 * @throws SocketException if there's an error while reading from the object stream, the socket will close and this exception
41 * is thrown
42 */
43 public Message awaitMessage() throws IOException {
44 try {
45 return (Message) input.readObject();
46 } catch (Exception e) {
47 Logger.info("the object stream from socket generated an exception and the SocketWrapper will now be closed." +
48 "The exception was: " + e.getClass());
49 this.close();
50 Logger.info("closed SocketWrapper");
51 throw new SocketException("SocketWrapper is closed");
52 }
53 }
54
55 /**
56 * Attempts to close the socket, if the socket is already closed then it does nothing
57 *
58 * @throws IOException if any exception happens during closing of the socket
59 */
60 public void close() throws IOException {
61 if (!this.sock.isClosed()) {
62 this.input.close();
63 this.output.flush();
64 this.output.close();
65 this.sock.close();
66 }
67 }
68
69 /**
70 * Check to see if the socket is closed
71 *
72 * @return true if the socket is closed
73 */
74 public boolean isClosed() {
75 return sock.isClosed();
76 }
77
78 /**
79 * Sends a message to the socket endpoint
80 *
81 * @param message the message to send, required not null
82 * @throws IOException if any error happens during the sending of the message
83 */
84 public synchronized void sendMessage(Message message) throws IOException {
85 Objects.requireNonNull(message);
86 this.output.writeObject(message);
87 this.output.flush();
88 }
89
90 /**
91 * Get the address this socket is bound to
92 *
93 * @return the address this socket is connected to.
94 */
95 public InetAddress getInetAddress() {
96 return sock.getInetAddress();
97 }
98 }