Permalink
Browse files

updating from buglabs/bugswarm-connector

  • Loading branch information...
1 parent 93cc706 commit 776fb843a9d0b7dd14647de3bf61a1f0018270cc @theterg theterg committed Jun 18, 2012
Showing with 2,511 additions and 37 deletions.
  1. +0 −10 buglabs.bug.swarm.client/.classpath
  2. +0 −1 buglabs.bug.swarm.client/META-INF/MANIFEST.MF
  3. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmBinaryUploadClient.class
  4. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmClient.class
  5. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmConfiguration.class
  6. BIN ...abs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmInviteClient$InvitationResponse.class
  7. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmInviteClient$InvitationState.class
  8. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmInviteClient.class
  9. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmJsonMessageListener.class
  10. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmKeysClient$KeyType.class
  11. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmKeysClient.class
  12. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmMessageListener$ExceptionType.class
  13. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmMessageListener.class
  14. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmResourcesClient$MemberType.class
  15. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmResourcesClient.class
  16. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmSession$SessionType.class
  17. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmSession.class
  18. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/ISwarmStringMessageListener.class
  19. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/IUserResourceClient.class
  20. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/SwarmClientFactory.class
  21. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/SwarmWSResponse$1.class
  22. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/SwarmWSResponse.class
  23. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/AbstractSwarmWSClient$1.class
  24. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/AbstractSwarmWSClient$2.class
  25. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/AbstractSwarmWSClient.class
  26. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmBinaryUploadWSClient.class
  27. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmInviteWSClient.class
  28. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmKeysWSClient$1.class
  29. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmKeysWSClient$2.class
  30. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmKeysWSClient$3.class
  31. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmKeysWSClient.class
  32. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmParticipationReader.class
  33. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmResourceWSClient$1.class
  34. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmResourceWSClient$2.class
  35. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmResourceWSClient.class
  36. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmSessionImp.class
  37. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/SwarmWSClient.class
  38. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/impl/UserResourceWSClient.class
  39. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/Configuration$Protocol.class
  40. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/Configuration.class
  41. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/Invitation$1.class
  42. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/Invitation$2.class
  43. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/Invitation.class
  44. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/ModelBase$1.class
  45. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/ModelBase.class
  46. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/ResourcePosition.class
  47. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/SwarmKey.class
  48. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/SwarmModel$1.class
  49. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/SwarmModel$2.class
  50. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/SwarmModel.class
  51. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/SwarmResourceModel.class
  52. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/UserResourceModel$1.class
  53. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/UserResourceModel$2.class
  54. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/model/UserResourceModel.class
  55. BIN buglabs.bug.swarm.client/bin/com/buglabs/bug/swarm/client/test/SwarmConfigKeys.class
  56. +9 −0 com.buglabs.bug.swarm.client/.classpath
  57. +12 −1 {buglabs.bug.swarm.client → com.buglabs.bug.swarm.client}/.project
  58. +14 −0 com.buglabs.bug.swarm.client/META-INF/MANIFEST.MF
  59. +4 −0 com.buglabs.bug.swarm.client/build.properties
  60. +62 −0 com.buglabs.bug.swarm.client/build.xml
  61. 0 ...nt → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmBinaryUploadClient.java
  62. 0 ...g.swarm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmClient.java
  63. 0 ....client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmConfiguration.java
  64. 0 ...m.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmInviteClient.java
  65. 0 ...t → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmJsonMessageListener.java
  66. 0 ...arm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmKeysClient.java
  67. 0 ...lient → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmMessageListener.java
  68. 0 ...lient → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmResourcesClient.java
  69. 0 ....swarm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmSession.java
  70. 0 ...→ com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/ISwarmStringMessageListener.java
  71. 0 ....client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/IUserResourceClient.java
  72. +7 −6 ...m.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/SwarmClientFactory.java
  73. 0 ...warm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/SwarmWSResponse.java
  74. 0 ... → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/AbstractSwarmWSClient.java
  75. +15 −0 com.buglabs.bug.swarm.client/src/com/buglabs/bug/swarm/client/impl/KeepAliveThread.java
  76. 0 ...om.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmBinaryUploadWSClient.java
  77. 0 ...nt → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmInviteWSClient.java
  78. 0 ...ient → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmKeysWSClient.java
  79. +3 −3 ...com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmParticipationReader.java
  80. 0 ... → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmResourceWSClient.java
  81. +57 −12 ...client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmSessionImp.java
  82. ...m.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/SwarmWSClient.java
  83. ...t → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/impl/UserResourceWSClient.java
  84. 0 ....client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/Configuration.java
  85. ...arm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/Invitation.java
  86. 0 ...warm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/ModelBase.java
  87. 0 ...ient → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/ResourcePosition.java
  88. 0 ...swarm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/SwarmKey.java
  89. 0 ...arm.client → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/SwarmModel.java
  90. 0 ...nt → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/SwarmResourceModel.java
  91. 0 ...ent → com.buglabs.bug.swarm.client}/src/com/buglabs/bug/swarm/client/model/UserResourceModel.java
  92. +204 −0 com.buglabs.bug.swarm.client/test/Example.html
  93. +1 −0 com.buglabs.bug.swarm.client/test/README
  94. +77 −0 com.buglabs.bug.swarm.client/test/com/buglabs/bug/swarm/client/example/Example.java
  95. +180 −0 com.buglabs.bug.swarm.client/test/com/buglabs/bug/swarm/client/test/AccountConfig.java
  96. 0 ...t/src → com.buglabs.bug.swarm.client/test}/com/buglabs/bug/swarm/client/test/SwarmConfigKeys.java
  97. +170 −0 ...labs.bug.swarm.client/test/com/buglabs/bug/swarm/client/test/TwoParticipantsOneSwarmTestCase.java
  98. +122 −0 ....bug.swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmAPIKeyWSAPITests.java
  99. +283 −0 ....swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmInvitationWSAPITests.java
  100. +333 −0 ...ug.swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmResourceWSAPITests.java
  101. +223 −0 ...uglabs.bug.swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmWSAPITests.java
  102. +126 −0 ...bug.swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/UserResourceWSAPITests.java
  103. +104 −0 com.buglabs.bug.swarm.client/test/com/buglabs/bug/swarm/client/test/participation/FeedTests.java
  104. +329 −0 ....bug.swarm.client/test/com/buglabs/bug/swarm/client/test/participation/ParticipationAPITests.java
  105. +172 −0 ...bug.swarm.client/test/com/buglabs/bug/swarm/client/test/participation/SessionManagementTests.java
@@ -1,10 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="lib" path="../swarm-libs/com.buglabs.bug.swarm.client-052412.jar"/>
- <classpathentry kind="lib" path="../swarm-libs/jackson-core-asl-1.9.2.jar"/>
- <classpathentry kind="lib" path="../swarm-libs/jackson-mapper-asl-1.9.2.jar"/>
- <classpathentry kind="lib" path="../swarm-libs/org.touge.restclient.jar"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
@@ -1 +0,0 @@
-Manifest-Version: 1.0
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="test"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
- <name>buglabs.bug.swarm.client</name>
+ <name>com.buglabs.bug.swarm.client</name>
<comment></comment>
<projects>
</projects>
@@ -10,8 +10,19 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,14 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: BUGswarm ReST Client
+Bundle-SymbolicName: com.buglabs.bug.swarm.client
+Bundle-Version: 1.0.0.qualifier
+Bundle-Vendor: Bug Labs, Inc.
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Import-Package:
+ junit.framework;resolution:=optional,
+ org.codehaus.jackson,
+ org.codehaus.jackson.map,
+ org.touge.restclient;version="1.0"
+Export-Package: com.buglabs.bug.swarm.client;version="0.3.0",
+ com.buglabs.bug.swarm.client.model;version="0.3.0"
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<project name="com.buglabs.bug.swarm.client" basedir=".">
+ <!-- PROJECT CONSTANTS -->
+
+ <target name="build.jars" description="Package the java class files into a jar.">
+ <echo message="## PROJECT: ${ant.project.name} ## TARGET: build.jars" />
+
+ <property name="dist.jar.name" value="${ant.project.name}" />
+ <property name="manifest.location" location="${basedir}/META-INF/MANIFEST.MF"/>
+
+ <fail unless="distDirectory" message="distDirectory must be defined." />
+ <fail unless="buildDirectory" message="buildDirectory must be defined." />
+
+ <jar destfile="${distDirectory}/${dist.jar.name}.jar"
+ basedir="${buildDirectory}"
+ manifest="${manifest.location}">
+ <fileset dir="${buildDirectory}" excludes="**/*.java,build/**,**/.settings/**,**/build.xml/**,**/build.properties/**,**/.classpath,**/.project,src/**,test/**,junit-reports/**,.git/**,**.gitignore,**/.checkstyle,**/README.*,**/build*.sh,checkstyle-reports/**" />
+ <fileset dir="${basedir}" includes="images/**,includes/**,javascripts/**,stylesheets/**,templates/**" />
+ </jar>
+ </target>
+
+ <!-- target: test Run JUnit tests -->
+ <target name="test">
+ <echo message="## PROJECT: ${ant.project.name} ## TARGET: test" />
+ <fail unless="report.dir" message="Ant variable report.dir must be defined." />
+ <echo message="Generating test result output in ${report.dir}..." />
+ <junit printsummary="yes">
+ <classpath refid="bin.classpath" />
+ <sysproperty key="report.misc" value="${report.misc}" />
+ <formatter type="xml" />
+ <batchtest todir="${report.dir}">
+ <fileset dir="${report.src}">
+ <include name="**/*Test*.java" />
+ <exclude name="**/*OSGiTest*.java" />
+ </fileset>
+ </batchtest>
+ </junit>
+ </target>
+
+ <!-- PROJECT IMPORTS -->
+ <exec executable="/usr/bin/wget">
+ <arg line="-P ${base.build.dir}" />
+ <arg line="--no-check-certificate" />
+ <arg line="-nc" />
+ <arg line="https://github.com/downloads/buglabs/bug-osgi/common-osgi.xml" />
+ </exec>
+ <property name="common.ant.file" location="${base.build.dir}/common-osgi.xml" />
+ <import file="${common.ant.file}" />
+
+ <!-- JUnit tests -->
+ <property environment="env" />
+ <property name="report.misc" value="${env.TEST_HOST}" />
+ <echo message="report.misc is ${report.misc}">
+ </echo>
+ <property name="report.dir" location="junit-reports" />
+ <property name="report.src" location="test/" />
+
+ <!-- Checkstyle -->
+ <property name="checkstyle.config" location="${base.build.dir}/toolbox/checkstyle/buglabs_checks.xml" />
+ <property name="checkstyle.dir" location="checkstyle-reports" />
+ <property name="checkstyle.src" location="src/" />
+</project>
@@ -20,6 +20,7 @@
* Default port of swarm server participation API.
*/
private static final int DEFAULT_SWARM_SERVER_PORT = 80;
+ private static boolean keepalive;
private SwarmClientFactory() {
//Static utility class.
@@ -56,9 +57,9 @@ public static ISwarmConfiguration getSwarmConfigurationClient(String hostname, S
* @throws UnknownHostException if unable to resolve hostname
* @throws IOException on I/O error
*/
- public static ISwarmSession createProductionSession(String hostname, String apiKey, String resourceId, String ... swarmIds) throws UnknownHostException, IOException {
+ public static ISwarmSession createProductionSession(String hostname, String apiKey, String resourceId, boolean keepalive, boolean autoreconnect, String ... swarmIds) throws UnknownHostException, IOException {
- return createSession(hostname, SessionType.PRODUCTION, apiKey, resourceId, swarmIds);
+ return createSession(hostname, SessionType.PRODUCTION, apiKey, resourceId, keepalive, autoreconnect, swarmIds);
}
/**
@@ -72,12 +73,12 @@ public static ISwarmSession createProductionSession(String hostname, String apiK
* @throws UnknownHostException if unable to resolve hostname
* @throws IOException on I/O error
*/
- public static ISwarmSession createConsumptionSession(String hostname, String apiKey, String resourceId, String ... swarmIds) throws UnknownHostException, IOException {
+ public static ISwarmSession createConsumptionSession(String hostname, String apiKey, String resourceId, boolean keepalive, boolean autoreconnect, String ... swarmIds) throws UnknownHostException, IOException {
- return createSession(hostname, SessionType.CONSUMPTION, apiKey, resourceId, swarmIds);
+ return createSession(hostname, SessionType.CONSUMPTION, apiKey, resourceId, keepalive, autoreconnect, swarmIds);
}
- private static ISwarmSession createSession(String hostname, SessionType type, String apiKey, String resourceId, String ... swarmIds) throws UnknownHostException, IOException {
+ private static ISwarmSession createSession(String hostname, SessionType type, String apiKey, String resourceId, boolean keepalive, boolean autoreconnect, String ... swarmIds) throws UnknownHostException, IOException {
if (hostname.toLowerCase().startsWith("http://"))
hostname = hostname.substring("http://".length());
@@ -87,7 +88,7 @@ private static ISwarmSession createSession(String hostname, SessionType type, St
port = Integer.parseInt(hostname.split(":")[1]);
hostname = hostname.split(":")[0];
}
- return new SwarmSessionImp(hostname, type, port, apiKey, resourceId, swarmIds);
+ return new SwarmSessionImp(hostname, type, port, apiKey, resourceId, keepalive, autoreconnect, swarmIds);
}
/**
@@ -0,0 +1,15 @@
+package com.buglabs.bug.swarm.client.impl;
+
+import java.net.Socket;
+
+public class KeepAliveThread implements Runnable{
+ public KeepAliveThread(Socket connection){
+
+ }
+ @Override
+ public void run() {
+
+
+ }
+
+}
@@ -14,8 +14,8 @@
import com.buglabs.bug.swarm.client.ISwarmClient;
import com.buglabs.bug.swarm.client.ISwarmJsonMessageListener;
import com.buglabs.bug.swarm.client.ISwarmMessageListener;
-import com.buglabs.bug.swarm.client.ISwarmStringMessageListener;
import com.buglabs.bug.swarm.client.ISwarmMessageListener.ExceptionType;
+import com.buglabs.bug.swarm.client.ISwarmStringMessageListener;
/**
* Reads from the HTTP-like input stream for a swarm participation session and sends events to listeners when they occur.
@@ -79,8 +79,8 @@ public void run() {
listener.exceptionOccurred(ExceptionType.SERVER_MESSAGE_PARSE_ERROR, "Unparsable message: " + line);
continue;
}
-
- debugOut(line, false);
+ //uncomment below to print all messages received by the reader
+ //debugOut(line, false);
//Parse the message string into a JsonNode.
JsonNode jmessage = mapper.readTree(line);
@@ -5,10 +5,13 @@
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
+import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import org.codehaus.jackson.JsonGenerationException;
@@ -33,45 +36,80 @@
private final List<ISwarmMessageListener> listeners;
private SwarmParticipationReader readerThread;
private final static ObjectMapper mapper = new ObjectMapper();
+ protected static final long MAX_INTERVAL = 60000; //timeout
private final String[] swarmIds;
private final String resourceId;
private final int port;
- private final SessionType type;
+ private final SessionType type;
+ private boolean keepalive;
+ private boolean autoreconnect;
+ private long timestamp;
/**
* @param hostname host of server
* @param type type of session to create
* @param port port on server
* @param apiKey api key
* @param resourceId resource id
+ * @param autoreconnect
+ * @param keepalive
* @param swarmIds list of swarms to join
* @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, 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;
this.apiKey = apiKey;
this.resourceId = resourceId;
+ this.keepalive = keepalive;
+ this.autoreconnect = autoreconnect;
this.swarmIds = swarmIds;
this.listeners = new CopyOnWriteArrayList<ISwarmMessageListener>();
this.listeners.add(this);
this.socket = createSocket(hostname, port);
- sendHeader();
+ sendHeader();
+ if (keepalive)
+ createKeepAliveThread();
+ }
+
+ //every 60 seconds, see if a message has been sent by comparing
+ //the local timestamp to the global. if they're the same
+ //no message has been sent, send a \n, otherwise just keep truckin
+ private void createKeepAliveThread() {
+ final Timer timer = new Timer();
+ timer.schedule(new TimerTask() {
+ private long localtimestamp = timestamp;
+
+ public void run() {
+ if (localtimestamp==timestamp){
+ try {
+ writeOut("\n");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ localtimestamp = timestamp;
+
+
+ }
+ }, 0, 60000);
}
private Socket createSocket(String hostname, int port) throws UnknownHostException, IOException {
Socket socket = new Socket(hostname, port);
+ socket.setSoTimeout(60000);
this.soutput = socket.getOutputStream();
if (readerThread != null)
readerThread.interrupt();
this.readerThread = new SwarmParticipationReader(socket.getInputStream(), apiKey, listeners);
this.readerThread.start();
+ //sendHeader();
return socket;
}
@@ -112,7 +150,11 @@ private void sendHeader() throws IOException {
//need to write one chunk into the stream before the platform will send data do us
//I chose \ because it's the keepalive as well
//see https://github.com/buglabs/bugswarm-connector/issues/30
- writeOut("\n");
+ soutput.write(Integer.toHexString("\n".length()).getBytes());
+ soutput.write(CRLF.getBytes());
+ soutput.write("\n".getBytes());
+ soutput.write(CRLF.getBytes());
+ soutput.flush();
}
/**
@@ -209,8 +251,8 @@ public void join(String swarmId, String resourceId) throws IOException {
buffer.append(Integer.toHexString(ps.getBytes().length)).append(CRLF);
buffer.append(ps).append(CRLF);
-
- debugOut(buffer.toString(), true);
+ //uncomment to get join messages
+ //debugOut(buffer.toString(), true);
soutput.write(buffer.toString().getBytes());
soutput.flush();
}
@@ -279,17 +321,20 @@ private void debugOut(String message, boolean out) {
* @throws IOException on socket I/O error
*/
private void writeOut(String message) throws IOException {
- if (!isConnected()) {
+ if (!isConnected() && autoreconnect) {
this.socket = createSocket(hostname, port);
+ sendHeader();
}
- debugOut(message, true);
-
- soutput.write(Integer.toHexString(message.length()).getBytes());
+ //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());
+ message = message+CRLF;
soutput.write(message.getBytes());
soutput.write(CRLF.getBytes());
soutput.flush();
+ timestamp = (new Date()).getTime();
}
@Override
@@ -314,8 +359,8 @@ public void close() {
buffer.append(Integer.toHexString(ps.getBytes().length)).append(CRLF);
buffer.append(ps).append(CRLF);
-
- debugOut(buffer.toString(), true);
+ //uncomment to get close messages
+ //debugOut(buffer.toString(), true);
soutput.write(buffer.toString().getBytes());
soutput.flush();
} catch (IOException e) {
@@ -11,12 +11,12 @@
import com.buglabs.bug.swarm.client.ISwarmClient;
import com.buglabs.bug.swarm.client.ISwarmConfiguration;
import com.buglabs.bug.swarm.client.ISwarmInviteClient;
+import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationResponse;
import com.buglabs.bug.swarm.client.ISwarmKeysClient;
import com.buglabs.bug.swarm.client.ISwarmResourcesClient;
+import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
import com.buglabs.bug.swarm.client.IUserResourceClient;
import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationResponse;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
import com.buglabs.bug.swarm.client.model.Invitation;
import com.buglabs.bug.swarm.client.model.ModelBase;
import com.buglabs.bug.swarm.client.model.SwarmModel;
Oops, something went wrong.

0 comments on commit 776fb84

Please sign in to comment.