Permalink
Browse files

patch "502 Bad Gateway" error, add option for debug printing

-When a socket close is detected by the readerThread (as opposed
to the writeOut() method), we forgot to send headers when
automatically reconnecting.  This confused haproxy, causing it to
reply with "502 Bad Gateway".  Fixed.
-added a producersession.enableMessageDebug() option.  Under
normal operation the threads shouldn't print much to the screen
except unexpected errors.  If you want to view raw swarm
messages, just execute enableMessageDebug(true);
  • Loading branch information...
1 parent 0d0c512 commit 83c623c690dc1a5d32c90130936d2d0c9bc8c04f @theterg theterg committed Jul 13, 2012
View
2 SimpleSwarmExample/META-INF/MANIFEST.MF
@@ -7,10 +7,10 @@ Bundle-Version: 1.0.0
Bundle-Vendor: Andrew Tergis
Bundle-Description: An OSGI implementation of JConnolly's swarm sample code
Bug-Bundle-Type: Application
-BUG-API-Version: 2.0.2
Import-Package: org.osgi.framework,
org.osgi.service.log,
org.osgi.util.tracker,
com.buglabs.application,
com.buglabs.util
Require-Bundle: com.buglabs.bug.swarm.client;bundle-version="1.0.0"
+BUG-API-Version: 2.0.2
View
2 SimpleSwarmExample/simpleswarmexample/SimpleSwarmExampleApplication.java
@@ -41,6 +41,7 @@ public void run(Map<Object, Object> services) {
try {
//create a session
producersession = SwarmClientFactory.createProductionSession(hostname, participation_key, resource_id, true, true, swarms);
+ producersession.enableMessageDebug(true);
//add a listener for incoming messages
producersession.addListener(new ISwarmJsonMessageListener() {
@@ -70,6 +71,7 @@ public void messageRecieved(Map<String, ?> payload, String fromSwarm, String fro
//send some messages
HashMap<String, String> map = new HashMap<String, String>();
map.put("message", "Java client hi");
+
try {
while(true){
if (producersession != null && producersession.isConnected()){
View
7 com.buglabs.bug.swarm.client/src/com/buglabs/bug/swarm/client/ISwarmSession.java
@@ -101,4 +101,11 @@
* Close the session.
*/
void close();
+
+ /**
+ * Enable raw printing of swarm messages to system.out.println
+ *
+ * @param value will enable additional debug printing.
+ */
+ public void enableMessageDebug(boolean value);
}
View
2 com.buglabs.bug.swarm.client/src/com/buglabs/bug/swarm/client/SwarmClientFactory.java
@@ -52,6 +52,8 @@ public static ISwarmConfiguration getSwarmConfigurationClient(String hostname, S
* @param hostname of swarm server
* @param apiKey of user
* @param resourceId associated with session
+ * @param keepalive will enable a keepalive thread that will ping the server for you
+ * @param autoreconnect to automatically re-open the socket if the connection fails
* @param swarmIds swarms to connect with
* @return a swarm session
* @throws UnknownHostException if unable to resolve hostname
View
9 ...labs.bug.swarm.client/src/com/buglabs/bug/swarm/client/impl/SwarmParticipationReader.java
@@ -40,6 +40,7 @@
private final BufferedReader reader;
private volatile boolean running = false;
private volatile boolean shuttingDown = false;
+ private boolean debugPrinting = false;
private final String apiKey;
private final List<ISwarmMessageListener> listeners;
private final static ObjectMapper mapper = new ObjectMapper();
@@ -64,6 +65,10 @@ protected SwarmParticipationReader(Socket sock, String apiKey, List<ISwarmMessag
this.reader = new BufferedReader(new InputStreamReader(is, ISwarmClient.SWARM_CHARACTER_ENCODING));
}
+ public void enableMessageDebug(boolean value){
+ debugPrinting = value;
+ }
+
@Override
public void run() {
running = true;
@@ -104,7 +109,9 @@ public void run() {
continue;
}
//uncomment below to print all messages received by the reader
- debugOut(line, false);
+ if (debugPrinting) {
+ debugOut(line, false);
+ }
//Parse the message string into a JsonNode.
JsonNode jmessage = mapper.readTree(line);
View
24 com.buglabs.bug.swarm.client/src/com/buglabs/bug/swarm/client/impl/SwarmSessionImp.java
@@ -44,6 +44,7 @@
private final SessionType type;
private boolean keepalive;
private boolean autoreconnect;
+ private boolean debugPrinting = false;
private long timestamp;
private final Timer keepaliveTimer;
@@ -59,7 +60,8 @@
* @throws UnknownHostException on host resolution error
* @throws IOException on I/O error
*/
- public SwarmSessionImp(String hostname, ISwarmSession.SessionType type, int port, String apiKey, String resourceId, boolean keepalive, boolean autoreconnect, String ... swarmIds) throws UnknownHostException, IOException {
+ public SwarmSessionImp(String hostname, ISwarmSession.SessionType type, int port, String apiKey, String resourceId,
+ boolean keepalive, boolean autoreconnect, String ... swarmIds) throws UnknownHostException, IOException {
this.hostname = hostname;
this.type = type;
this.port = port;
@@ -255,7 +257,9 @@ public void join(String swarmId, String resourceId) throws IOException {
buffer.append(Integer.toHexString(ps.getBytes().length)).append(CRLF);
buffer.append(ps).append(CRLF);
//uncomment to get join messages
- //debugOut(buffer.toString(), true);
+ if (debugPrinting) {
+ debugOut(buffer.toString(), true);
+ }
soutput.write(buffer.toString().getBytes());
soutput.flush();
}
@@ -335,7 +339,9 @@ private void writeOut(String message) throws IOException {
sendHeader();
}
- debugOut(message, true);
+ if (debugPrinting){
+ debugOut(message, true);
+ }
//new framing requires sending a \r\n after each message.
soutput.write(Integer.toHexString(message.length()+CRLF.length()).getBytes());
soutput.write(CRLF.getBytes());
@@ -362,8 +368,8 @@ public void close() {
if (readerThread != null)
readerThread.shuttingDown();
- //Attempt to send presence message.
try {
+ //Attempt to send presence message.
StringBuilder buffer = new StringBuilder(); // StringBuilder(staticHeader.toString());
String ps = generateOutgoingPresenceMessage(false, swarmIds);
@@ -373,6 +379,9 @@ public void close() {
//debugOut(buffer.toString(), true);
soutput.write(buffer.toString().getBytes());
soutput.flush();
+ //Now also send a blank chunk, to actually close the socket...
+ soutput.write(CRLF.getBytes());
+ soutput.flush();
} catch (IOException e) {
}
@@ -407,6 +416,7 @@ public void exceptionOccurred(ExceptionType type, String message) {
if (type == ExceptionType.SERVER_UNEXPECTED_DISCONNECT) {
try {
this.socket = createSocket(hostname, port);
+ sendHeader();
} catch (UnknownHostException e) {
if (keepalive)
System.out.println("["+this.getClass().getSimpleName()+"]: "+"UnknownHostException during reconnect, will retry on next keepalive tick");
@@ -442,4 +452,10 @@ public void cancelRequest(String feedName) throws IOException {
mapper.writeValueAsString(
createFeedRequestMap(feedName, 0, true)));
}
+
+ @Override
+ public void enableMessageDebug(boolean value) {
+ debugPrinting = value;
+ readerThread.enableMessageDebug(value);
+ }
}
View
BIN swarm-libs/com.buglabs.bug.swarm.client-071312.jar
Binary file not shown.

0 comments on commit 83c623c

Please sign in to comment.