Skip to content

Commit

Permalink
Code review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Sep 7, 2013
1 parent 16d3d68 commit 2411f01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
17 changes: 7 additions & 10 deletions src/main/java/org/littleshoot/proxy/impl/ConnectionFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ ConnectionFlow then(ConnectionFlowStep step) {
return this;
}

Object getConnectLock() {
return connectLock;
}

/**
* While we're in the process of connecting, any messages read by the
* {@link ProxyToServerConnection} are passed to this method, which passes
Expand Down Expand Up @@ -201,6 +197,13 @@ public void operationComplete(Future future)
});
}

/**
* Like {@link #fail(Throwable)} but with no cause.
*/
void fail() {
fail(null);
}

/**
* Once we've finished recording our connection and written our initial
* request, we can notify anyone who is waiting on the connection that it's
Expand All @@ -210,10 +213,4 @@ private void notifyThreadsWaitingForConnection() {
connectLock.notifyAll();
}

/**
* Like {@link #fail(Throwable)} but with no cause.
*/
void fail() {
fail(null);
}
}
21 changes: 15 additions & 6 deletions src/main/java/org/littleshoot/proxy/impl/NetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,28 @@ private static InetAddress getLocalHostViaUdp() throws UnknownHostException {
}
}

/**
* Go through our network interfaces and find the first bound address for an
* up interface that's in the IPv4 address space and is not the loopback
* address.
*
* @return
*/
public static InetAddress firstLocalNonLoopbackIPv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/littleshoot/proxy/impl/ProxyConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ private void readHTTP(HttpObject httpObject) {
case NEGOTIATING_CONNECT:
LOG.debug("Attempted to read from connection that's in the process of negotiating an HTTP CONNECT. This is probably the LastHttpContent of a chunked CONNECT.");
break;
case AWAITING_CONNECT_OK:
LOG.warn("AWAITING_CONNECT_OK should have been handled by ProxyToServerConnection.read()");
break;
case HANDSHAKING:
LOG.warn(
"Attempted to read from connection that's in the process of handshaking. This shouldn't happen.",
Expand All @@ -182,11 +185,7 @@ private void readHTTP(HttpObject httpObject) {
case DISCONNECTED:
LOG.info("Ignoring message since the connection is closed or about to close");
break;
case AWAITING_CONNECT_OK:
LOG.warn("AWAITING_CONNECT_OK should have been handled by ProxyToServerConnection.read()");
break;
}

become(nextState);
}

Expand Down Expand Up @@ -253,11 +252,12 @@ void doWrite(Object msg) {
* @param httpObject
*/
protected void writeHttp(HttpObject httpObject) {
writeToChannel(httpObject);

if (ProxyUtils.isLastChunk(httpObject)) {
channel.write(httpObject);
LOG.debug("Writing an empty buffer to signal the end of our chunked transfer");
writeToChannel(Unpooled.EMPTY_BUFFER);
} else {
writeToChannel(httpObject);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ void write(Object msg) {
msg);
}
} else {
synchronized (connectionFlow.getConnectLock()) {
synchronized (connectLock) {
if (isConnecting()) {
LOG.debug("Attempted to write while still in the process of connecting. Wait for connecting to finish.");
try {
connectionFlow.getConnectLock().wait(30000);
connectLock.wait(30000);
} catch (InterruptedException ie) {
LOG.warn("Interrupted while waiting for connect monitor");
}
Expand Down

0 comments on commit 2411f01

Please sign in to comment.