Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a ready-to-go example of John's code snippet
This has been pre-configured for resources and swarms on the test acct
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<classpath> | |||
<classpathentry kind="src" path=""/> | |||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | |||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> | |||
<classpathentry kind="con" path="com.buglabs.dragonfly.jdt.BugClasspathContainerInitializer"/> | |||
<classpathentry kind="output" path=""/> | |||
</classpath> |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<projectDescription> | |||
<name>SimpleSwarmExample</name> | |||
<comment></comment> | |||
<projects> | |||
</projects> | |||
<buildSpec> | |||
<buildCommand> | |||
<name>org.eclipse.jdt.core.javabuilder</name> | |||
<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>com.buglabs.dragonfly.BugApplicationNature</nature> | |||
<nature>org.eclipse.pde.PluginNature</nature> | |||
<nature>org.eclipse.jdt.core.javanature</nature> | |||
</natures> | |||
</projectDescription> |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
eclipse.preferences.version=1 | |||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | |||
org.eclipse.jdt.core.compiler.compliance=1.6 | |||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning | |||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning | |||
org.eclipse.jdt.core.compiler.source=1.6 |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
Manifest-Version: 1.0 | |||
Bundle-ManifestVersion: 2 | |||
Bundle-Name: SimpleSwarmExample | |||
Bundle-Activator: simpleswarmexample.Activator | |||
Bundle-SymbolicName: SimpleSwarmExample | |||
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" |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
/** | |||
* Generated by Dragonfly SDK. | |||
*/ | |||
package simpleswarmexample; | |||
|
|||
|
|||
import org.osgi.service.log.LogService; | |||
|
|||
import org.osgi.framework.BundleActivator; | |||
import org.osgi.framework.BundleContext; | |||
import org.osgi.util.tracker.ServiceTracker; | |||
|
|||
import com.buglabs.application.ServiceTrackerHelper; | |||
|
|||
|
|||
/** | |||
* BundleActivator for SimpleSwarmExample. The OSGi entry point to the application. | |||
* | |||
*/ | |||
public class Activator implements BundleActivator { | |||
/** | |||
* OSGi services the application depends on. | |||
*/ | |||
private static final String [] services = { | |||
LogService.class.getName(), | |||
}; | |||
private ServiceTracker serviceTracker; | |||
|
|||
/* | |||
* (non-Javadoc) | |||
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) | |||
*/ | |||
public void start(BundleContext context) throws Exception { | |||
//Begin tracking services, and when all services are available, create thread and call ManagedRunnable.run(). | |||
serviceTracker = ServiceTrackerHelper.openServiceTracker(context, services, new SimpleSwarmExampleApplication()); | |||
|
|||
} | |||
|
|||
/* | |||
* (non-Javadoc) | |||
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) | |||
*/ | |||
public void stop(BundleContext context) throws Exception { | |||
|
|||
//Will cause the ManagedRunnable.shutdown() to be called. | |||
serviceTracker.close(); | |||
} | |||
|
|||
|
|||
} |
101 changes: 101 additions & 0 deletions
101
SimpleSwarmExample/simpleswarmexample/SimpleSwarmExampleApplication.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,101 @@ | |||
package simpleswarmexample; | |||
|
|||
import java.io.IOException; | |||
import java.net.UnknownHostException; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|||
import org.osgi.service.log.LogService; | |||
|
|||
import com.buglabs.application.ServiceTrackerHelper.ManagedRunnable; | |||
import com.buglabs.bug.swarm.client.ISwarmJsonMessageListener; | |||
import com.buglabs.bug.swarm.client.ISwarmSession; | |||
import com.buglabs.bug.swarm.client.SwarmClientFactory; | |||
import com.sun.xml.internal.ws.api.model.ExceptionType; | |||
/** | |||
* This class represents the running application when all service dependencies are fulfilled. | |||
* | |||
* The run() method will be called with a map containing all the services specified in ServiceTrackerHelper.openServiceTracker(). | |||
* The application will run in a separate thread than the caller of start() in the Activator. See | |||
* ManagedInlineRunnable for a thread-less application. | |||
* | |||
* By default, the application will only be started when all service dependencies are fulfilled. For | |||
* finer grained service binding logic, see ServiceTrackerHelper.openServiceTracker(BundleContext context, String[] services, Filter filter, ServiceTrackerCustomizer customizer) | |||
*/ | |||
public class SimpleSwarmExampleApplication implements ManagedRunnable { | |||
|
|||
String hostname = "api.bugswarm.net"; | |||
String participation_key = "63a0e565521670b1cf32040bf179e2f904146e81"; | |||
String resource_id = "6dc1eca12cf6461515fb863a925ef5bfa8278c48"; | |||
String[] swarms = new String[]{ "6c67d6493656d32698ec9f820feca33c38d9abf6" }; | |||
private LogService ls; | |||
private Thread myThread; | |||
private ISwarmSession producersession; | |||
|
|||
@Override | |||
public void run(Map<Object, Object> services) { | |||
ls = (LogService) services.get(LogService.class.getName()); | |||
ilog("starting..."); | |||
myThread = Thread.currentThread(); | |||
|
|||
try { | |||
//create a session | |||
producersession = SwarmClientFactory.createProductionSession(hostname, participation_key, resource_id, false, false, swarms); | |||
|
|||
//add a listener for incoming messages | |||
producersession.addListener(new ISwarmJsonMessageListener() { | |||
@Override | |||
public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) { | |||
//print out messages regarding what resources are present in our swarm | |||
System.out.println("fromswarm: "+fromSwarm+" fromresource: "+fromResource+ "isavailable "+isAvailable); | |||
} | |||
|
|||
@Override | |||
public void exceptionOccurred(ExceptionType type, String message) { | |||
//handle some authentication or connection issues | |||
System.out.println("exception occured: "+type+ " message "+ message); | |||
} | |||
|
|||
@Override | |||
public void messageRecieved(Map<String, ?> payload, String fromSwarm, String fromResource, boolean isPublic) { | |||
//print messages as they're received from the swarm | |||
for (Map.Entry<String, ?> entry : payload.entrySet()) | |||
{ | |||
System.out.println(entry.getKey() + "/" + entry.getValue()); | |||
} | |||
} | |||
}); | |||
ilog("Connected, sending messages"); | |||
|
|||
//send some messages | |||
HashMap<String, String> map = new HashMap<String, String>(); | |||
map.put("message", "Java client hi"); | |||
try { | |||
while(true){ | |||
producersession.send(map); | |||
Thread.sleep(1000); | |||
} | |||
} catch (InterruptedException e){ | |||
producersession.close(); | |||
return; | |||
} | |||
} catch (UnknownHostException e) { | |||
e.printStackTrace(); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
|
|||
} | |||
|
|||
@Override | |||
public void shutdown() { | |||
// TODO Add shutdown code here if necessary. | |||
myThread.interrupt(); | |||
} | |||
|
|||
//Wrappers for the log service (to standardize logged messages) | |||
void ilog(String message){ ls.log(LogService.LOG_INFO, "["+this.getClass().getPackage().getName()+"] "+message); } | |||
void dlog(String message){ ls.log(LogService.LOG_DEBUG, "["+this.getClass().getPackage().getName()+"] "+message); } | |||
void elog(String message){ ls.log(LogService.LOG_ERROR, "["+this.getClass().getPackage().getName()+"] "+message); } | |||
void wlog(String message){ ls.log(LogService.LOG_WARNING, "["+this.getClass().getPackage().getName()+"] "+message); } | |||
} |