-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d578a91
commit e2784a4
Showing
9 changed files
with
136 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
57 changes: 54 additions & 3 deletions
57
src/main/java/be/bagofwords/application/SocketRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,63 @@ | ||
package be.bagofwords.application; | ||
|
||
import be.bagofwords.util.SafeThread; | ||
import be.bagofwords.util.SocketConnection; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
/** | ||
* Created by koen on 01.11.16. | ||
*/ | ||
public interface SocketRequestHandler { | ||
public abstract class SocketRequestHandler extends SafeThread { | ||
|
||
protected SocketConnection connection; | ||
private SocketServer socketServer; | ||
private long startTime; | ||
|
||
public SocketRequestHandler(String name, SocketConnection connection) { | ||
super(name, true); | ||
this.connection = connection; | ||
} | ||
|
||
public void setSocketServer(SocketServer socketServer) { | ||
this.socketServer = socketServer; | ||
} | ||
|
||
public long getStartTime() { | ||
return startTime; | ||
} | ||
|
||
@Override | ||
protected void runInt() throws Exception { | ||
try { | ||
startTime = System.currentTimeMillis(); | ||
handleRequests(); | ||
} catch (Exception ex) { | ||
if (isUnexpectedError(ex)) { | ||
reportUnexpectedError(ex); | ||
} | ||
} | ||
IOUtils.closeQuietly(connection); | ||
socketServer.removeHandler(this); | ||
} | ||
|
||
protected boolean isUnexpectedError(Exception ex) { | ||
if (ex.getMessage() != null && ex.getMessage().contains("Connection reset")) { | ||
return false; | ||
} | ||
for (StackTraceElement el : ex.getStackTrace()) { | ||
if (el.getMethodName().equals("readNextAction")) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public abstract void handleRequests() throws Exception; | ||
|
||
void handleRequests() throws Exception; | ||
public abstract void reportUnexpectedError(Exception ex); | ||
|
||
void reportUnexpectedError(Exception ex); | ||
public long getTotalNumberOfRequests() { | ||
return -1; //Should be overridden in subclasses | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/be/bagofwords/application/SocketServerContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package be.bagofwords.application; | ||
|
||
public class SocketServerContextFactory extends MinimalApplicationContextFactory { | ||
|
||
@Override | ||
public void wireApplicationContext(ApplicationContext context) { | ||
super.wireApplicationContext(context); | ||
SocketServer server = new SocketServer(Integer.parseInt(context.getConfig("socket_port"))); | ||
context.registerBean(server); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters