Skip to content
Browse files

added initial implentation of Listener

  • Loading branch information...
1 parent 102eaac commit 08d376ffbedfd8271208b08c6a92bfbf44243ecb @mschoch mschoch committed Jan 31, 2012
View
9 TouchDB-Android-Listener/.classpath
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="gen"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+ <classpathentry kind="lib" path="libs/webserver.jar"/>
+ <classpathentry kind="output" path="bin/classes"/>
+</classpath>
View
17 TouchDB-Android-Listener/.gitignore
@@ -0,0 +1,17 @@
+# built application files
+*.apk
+*.ap_
+
+# files for the dex VM
+*.dex
+
+# Java class files
+*.class
+
+# generated files
+/bin/
+/gen/
+
+# Local configuration file (sdk path, etc)
+local.properties
+build.properties
View
33 TouchDB-Android-Listener/.project
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>TouchDB-Android-Listener</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
View
14 TouchDB-Android-Listener/AndroidManifest.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.couchbase.touchdb.listener"
+ android:versionCode="1"
+ android:versionName="1.0" >
+
+ <uses-sdk android:minSdkVersion="8" />
+
+ <application
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name" >
+ </application>
+
+</manifest>
View
BIN TouchDB-Android-Listener/libs/servlet.jar
Binary file not shown.
View
BIN TouchDB-Android-Listener/libs/webserver.jar
Binary file not shown.
View
40 TouchDB-Android-Listener/proguard.cfg
@@ -0,0 +1,40 @@
+-optimizationpasses 5
+-dontusemixedcaseclassnames
+-dontskipnonpubliclibraryclasses
+-dontpreverify
+-verbose
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
+
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+-keep public class * extends android.app.backup.BackupAgentHelper
+-keep public class * extends android.preference.Preference
+-keep public class com.android.vending.licensing.ILicensingService
+
+-keepclasseswithmembernames class * {
+ native <methods>;
+}
+
+-keepclasseswithmembers class * {
+ public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+-keepclasseswithmembers class * {
+ public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers class * extends android.app.Activity {
+ public void *(android.view.View);
+}
+
+-keepclassmembers enum * {
+ public static **[] values();
+ public static ** valueOf(java.lang.String);
+}
+
+-keep class * implements android.os.Parcelable {
+ public static final android.os.Parcelable$Creator *;
+}
View
13 TouchDB-Android-Listener/project.properties
@@ -0,0 +1,13 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-8
+android.library=true
+android.library.reference.1=../TouchDB-Android
View
BIN TouchDB-Android-Listener/res/drawable-hdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN TouchDB-Android-Listener/res/drawable-ldpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN TouchDB-Android-Listener/res/drawable-mdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
12 TouchDB-Android-Listener/res/layout/main.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:orientation="vertical" >
+
+ <TextView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="@string/hello" />
+
+</LinearLayout>
View
7 TouchDB-Android-Listener/res/values/strings.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+ <string name="hello">Hello World!</string>
+ <string name="app_name">TouchDB-Android-Listener</string>
+
+</resources>
View
47 TouchDB-Android-Listener/src/com/couchbase/touchdb/listener/TDHTTPServer.java
@@ -0,0 +1,47 @@
+package com.couchbase.touchdb.listener;
+
+import java.util.Properties;
+
+import Acme.Serve.Serve;
+
+import com.couchbase.touchdb.TDServer;
+
+@SuppressWarnings("serial")
+public class TDHTTPServer extends Serve {
+
+ public static final String TDSERVER_KEY = "TDServer";
+
+ private Properties props;
+ private TDServer server;
+ private TDListener listener;
+
+ public TDHTTPServer() {
+ props = new Properties();
+ }
+
+ public void setServer(TDServer server) {
+ this.server = server;
+ }
+
+ public void setListener(TDListener listener) {
+ this.listener = listener;
+ }
+
+ public void setPort(int port) {
+ props.put("port", port);
+ }
+
+ @Override
+ public int serve() {
+ //pass our custom properties in
+ this.arguments = props;
+
+ //pass in the tdserver to the servlet
+ TDHTTPServlet servlet = new TDHTTPServlet();
+ servlet.setServer(server);
+
+ this.addServlet("/", servlet);
+ return super.serve();
+ }
+
+}
View
97 TouchDB-Android-Listener/src/com/couchbase/touchdb/listener/TDHTTPServlet.java
@@ -0,0 +1,97 @@
+package com.couchbase.touchdb.listener;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.couchbase.touchdb.TDBody;
+import com.couchbase.touchdb.TDServer;
+import com.couchbase.touchdb.router.TDRouter;
+import com.couchbase.touchdb.router.TDURLConnection;
+
+@SuppressWarnings("serial")
+public class TDHTTPServlet extends HttpServlet {
+
+ private TDServer server;
+
+ public void setServer(TDServer server) {
+ this.server = server;
+ }
+
+ @Override
+ public void service(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
+
+
+ synchronized (server) {
+
+ //set path
+ URL url = new URL("touchdb://" + request.getRequestURI());
+ TDURLConnection conn = (TDURLConnection)url.openConnection();
+ conn.setDoOutput(true);
+
+ //set the method
+ conn.setRequestMethod(request.getMethod());
+
+ //set the headers
+ Enumeration<String> headerNames = request.getHeaderNames();
+ while(headerNames.hasMoreElements()) {
+ String headerName = headerNames.nextElement();
+ conn.setRequestProperty(headerName, request.getHeader(headerName));
+ }
+
+ //set the body
+ InputStream is = request.getInputStream();
+ //fixme dont think i should have to call available here
+ //but its blocking on get requests otherwise
+ if(is != null && is.available() > 0) {
+ conn.setDoInput(true);
+ OutputStream os = conn.getOutputStream();
+ byte[] buffer = new byte[1024];
+ int lenRead = is.read(buffer, 0, 1024);
+ while(lenRead > 0) {
+ os.write(buffer, 0, lenRead);
+ lenRead = is.read(buffer, 0, 1024);
+ }
+ is.close();
+ os.close();
+ }
+
+ TDRouter router = new TDRouter(server, conn);
+ router.start();
+
+ //set the response code
+ response.setStatus(conn.getResponseCode());
+
+ //add the resonse headers
+ Map<String, List<String>> headers = conn.getHeaderFields();
+ if(headers != null) {
+ for (String headerName : headers.keySet()) {
+ for (String headerValue : headers.get(headerName)) {
+ response.addHeader(headerName, headerValue);
+ }
+ }
+ }
+
+ //write the response body
+ OutputStream os = response.getOutputStream();
+ TDBody body = conn.getResponseBody();
+ if(body != null) {
+ byte[] json = body.getJson();
+ os.write(json);
+ }
+ os.close();
+ }
+
+ }
+
+}
View
41 TouchDB-Android-Listener/src/com/couchbase/touchdb/listener/TDListener.java
@@ -0,0 +1,41 @@
+package com.couchbase.touchdb.listener;
+
+import java.net.URL;
+
+import com.couchbase.touchdb.TDServer;
+import com.couchbase.touchdb.router.TDURLStreamHandlerFactor;
+
+public class TDListener implements Runnable {
+
+ private Thread thread;
+ private TDServer server;
+ private TDHTTPServer httpServer;
+
+ //static inializer to ensure that touchdb:// URLs are handled properly
+ {
+ URL.setURLStreamHandlerFactory(new TDURLStreamHandlerFactor());
+ }
+
+ public TDListener(TDServer server, int port) {
+ this.server = server;
+ this.httpServer = new TDHTTPServer();
+ this.httpServer.setServer(server);
+ this.httpServer.setListener(this);
+ this.httpServer.setPort(port);
+ }
+
+ @Override
+ public void run() {
+ httpServer.serve();
+ }
+
+ public void start() {
+ thread = new Thread(this);
+ thread.start();
+ }
+
+ public void stop() {
+ httpServer.notifyStop();
+ }
+
+}

2 comments on commit 08d376f

@ddumont

Can't we do this kind of message passing with either intents or content uris? Do we really need to use servers and sockets for this?

@mschoch
couchbaselabs member
mschoch commented on 08d376f Feb 1, 2012

The point of the listener is that it implements HTTP. This is essential for the operation of some applications. It was packaged as a separate Android library because not all applications will require it. If you application does not use CouchApp style applications, or require peer-to-peer replication, you can safely ignore this module.

Please sign in to comment.
Something went wrong with that request. Please try again.