| 1 | package com.renomad.minum.web; | |
| 2 | ||
| 3 | import com.renomad.minum.logging.ILogger; | |
| 4 | ||
| 5 | import java.io.*; | |
| 6 | import java.net.Socket; | |
| 7 | import java.net.SocketAddress; | |
| 8 | import java.nio.charset.Charset; | |
| 9 | ||
| 10 | /** | |
| 11 | * This wraps Sockets to make them more particular to our use case | |
| 12 | */ | |
| 13 | public final class SocketWrapper implements ISocketWrapper { | |
| 14 | ||
| 15 | private final Socket socket; | |
| 16 | private final String hostName; | |
| 17 | private final BufferedOutputStream bufferedOutputStream; | |
| 18 | private final ILogger logger; | |
| 19 | private final IServer server; | |
| 20 | private final BufferedInputStream bufferedInputStream; | |
| 21 | ||
| 22 | /** | |
| 23 | * Constructor | |
| 24 | * @param socket a socket we intend to wrap with methods applicable to our use cases | |
| 25 | * @param logger not much more to say on this param | |
| 26 | * @param timeoutMillis we'll configure the socket to timeout after this many milliseconds. | |
| 27 | */ | |
| 28 | public SocketWrapper(Socket socket, IServer server, ILogger logger, int timeoutMillis, String hostName) throws IOException { | |
| 29 | this.socket = socket; | |
| 30 | this.hostName = hostName; | |
| 31 | logger.logTrace(() -> String.format("Setting timeout of %d milliseconds on socket %s", timeoutMillis, socket)); | |
| 32 | ||
| 33 |
1
1. <init> : removed call to java/net/Socket::setSoTimeout → KILLED |
this.socket.setSoTimeout(timeoutMillis); |
| 34 | this.bufferedInputStream = new BufferedInputStream(socket.getInputStream()); | |
| 35 | this.bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream()); | |
| 36 | ||
| 37 | this.logger = logger; | |
| 38 | this.server = server; | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public void send(String msg) throws IOException { | |
| 43 |
1
1. send : removed call to java/io/BufferedOutputStream::write → TIMED_OUT |
bufferedOutputStream.write(msg.getBytes(Charset.defaultCharset())); |
| 44 | } | |
| 45 | ||
| 46 | @Override | |
| 47 | public void send(byte[] bodyContents) throws IOException { | |
| 48 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
bufferedOutputStream.write(bodyContents); |
| 49 | } | |
| 50 | ||
| 51 | @Override | |
| 52 | public void send(byte[] bodyContents, int off, int len) throws IOException { | |
| 53 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
bufferedOutputStream.write(bodyContents, off, len); |
| 54 | } | |
| 55 | ||
| 56 | @Override | |
| 57 | public void send(int b) throws IOException { | |
| 58 |
1
1. send : removed call to java/io/BufferedOutputStream::write → KILLED |
bufferedOutputStream.write(b); |
| 59 | } | |
| 60 | ||
| 61 | @Override | |
| 62 | public void sendHttpLine(String msg) throws IOException { | |
| 63 | logger.logTrace(() -> String.format("%s sending: \"%s\"", this, msg)); | |
| 64 |
1
1. sendHttpLine : removed call to com/renomad/minum/web/SocketWrapper::send → KILLED |
send(msg + WebEngine.HTTP_CRLF); |
| 65 | } | |
| 66 | ||
| 67 | @Override | |
| 68 | public int getLocalPort() { | |
| 69 |
1
1. getLocalPort : replaced int return with 0 for com/renomad/minum/web/SocketWrapper::getLocalPort → KILLED |
return socket.getLocalPort(); |
| 70 | } | |
| 71 | ||
| 72 | @Override | |
| 73 | public SocketAddress getRemoteAddrWithPort() { | |
| 74 |
1
1. getRemoteAddrWithPort : replaced return value with null for com/renomad/minum/web/SocketWrapper::getRemoteAddrWithPort → KILLED |
return socket.getRemoteSocketAddress(); |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | public String getRemoteAddr() { | |
| 79 |
1
1. getRemoteAddr : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getRemoteAddr → TIMED_OUT |
return socket.getInetAddress().getHostAddress(); |
| 80 | } | |
| 81 | ||
| 82 | @Override | |
| 83 | public HttpServerType getServerType() { | |
| 84 |
1
1. getServerType : replaced return value with null for com/renomad/minum/web/SocketWrapper::getServerType → KILLED |
return server.getServerType(); |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public void close() throws IOException { | |
| 89 | logger.logTrace(() -> "close called on " + this); | |
| 90 |
1
1. close : removed call to java/net/Socket::close → TIMED_OUT |
socket.close(); |
| 91 |
2
1. close : negated conditional → TIMED_OUT 2. close : removed call to com/renomad/minum/web/IServer::removeMyRecord → TIMED_OUT |
if (server != null) server.removeMyRecord(this); |
| 92 | } | |
| 93 | ||
| 94 | @Override | |
| 95 | public InputStream getInputStream() { | |
| 96 |
1
1. getInputStream : replaced return value with null for com/renomad/minum/web/SocketWrapper::getInputStream → KILLED |
return bufferedInputStream; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Note that since we are indicating just the remote address | |
| 101 | * as the unique value, in cases like tests where we are operating as | |
| 102 | * sometimes server or client, you might see the server as the remote. | |
| 103 | */ | |
| 104 | @Override | |
| 105 | public String toString() { | |
| 106 |
1
1. toString : replaced return value with "" for com/renomad/minum/web/SocketWrapper::toString → KILLED |
return "(SocketWrapper for remote address: " + this.getRemoteAddrWithPort().toString() + ")"; |
| 107 | } | |
| 108 | ||
| 109 | @Override | |
| 110 | public String getHostName() { | |
| 111 |
1
1. getHostName : replaced return value with "" for com/renomad/minum/web/SocketWrapper::getHostName → KILLED |
return hostName; |
| 112 | } | |
| 113 | ||
| 114 | @Override | |
| 115 | public void flush() throws IOException { | |
| 116 |
1
1. flush : removed call to java/io/BufferedOutputStream::flush → KILLED |
this.bufferedOutputStream.flush(); |
| 117 | } | |
| 118 | } | |
Mutations | ||
| 33 |
1.1 |
|
| 43 |
1.1 |
|
| 48 |
1.1 |
|
| 53 |
1.1 |
|
| 58 |
1.1 |
|
| 64 |
1.1 |
|
| 69 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 84 |
1.1 |
|
| 90 |
1.1 |
|
| 91 |
1.1 2.2 |
|
| 96 |
1.1 |
|
| 106 |
1.1 |
|
| 111 |
1.1 |
|
| 116 |
1.1 |