Skip to content

Commit

Permalink
updating from buglabs/bugswarm-connector
Browse files Browse the repository at this point in the history
  • Loading branch information
theterg committed Jun 18, 2012
1 parent 93cc706 commit 776fb84
Show file tree
Hide file tree
Showing 105 changed files with 2,511 additions and 37 deletions.
10 changes: 0 additions & 10 deletions buglabs.bug.swarm.client/.classpath

This file was deleted.

1 change: 0 additions & 1 deletion buglabs.bug.swarm.client/META-INF/MANIFEST.MF

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions com.buglabs.bug.swarm.client/.classpath
@@ -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>
Expand All @@ -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>
14 changes: 14 additions & 0 deletions com.buglabs.bug.swarm.client/META-INF/MANIFEST.MF
@@ -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"
4 changes: 4 additions & 0 deletions com.buglabs.bug.swarm.client/build.properties
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
62 changes: 62 additions & 0 deletions com.buglabs.bug.swarm.client/build.xml
@@ -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>
Expand Up @@ -20,6 +20,7 @@ public class SwarmClientFactory {
* 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.
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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());

Expand All @@ -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);
}

/**
Expand Down
@@ -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() {


}

}
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand All @@ -33,45 +36,80 @@ public class SwarmSessionImp implements ISwarmSession, ISwarmMessageListener {
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;
}
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -279,17 +321,20 @@ private Map<String, Object> createFeedRequestMap(String feedName, int interval,
* @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
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit 776fb84

Please sign in to comment.