Skip to content
Permalink
Browse files
[CB-4281] Adding echo plugin after breakout
  • Loading branch information
David Kemp authored and mwoghiren committed Jul 22, 2013
1 parent 94f1514 commit f5c0a54d056c6e9b76f59eac2b2908ddbfed3e65
Showing 8 changed files with 302 additions and 0 deletions.
@@ -0,0 +1,5 @@
cordova-plugin-echo
------------------------

This is a plugin implementation of the Echo function to test the bridge. It is currently used by Mobile Spec to test and benchmark the bridge.

@@ -0,0 +1,50 @@
---
license: Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
---

Echo
=========

> The `echo` object provides an interface for testing the bridge. It
> sends any data received back across the bridge unmodifed. This
> allows the bridge modes to be tested and benchmarked.
Methods
-------

- cordova.echo.echo
- cordova.echo.echoAsync
- cordova.echo.echoArrayBuffer
- cordova.echo.echoMultiPart

### cordova.echo.echo

Echos string data in the same thread.

### cordova.echo.echoAsync

Echos string data in a new UI thread.

### cordova.echo.echoArrayBuffer

Echos base64 encoded data in the same thread.

### cordova.echo.echoMultiPart

Echos a multipart message in the same thread.

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="org.apache.cordova.test.echo"
version="0.1.0">
<name>Echo</name>

<js-module src="www/echo.js" name="echo">
<clobbers target="cordova.echo" />
</js-module>

<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Echo" >
<param name="android-package" value="org.apache.cordova.test.Echo"/>
</feature>
</config-file>

<source-file src="src/android/Echo.java" target-dir="src/org/apache/cordova/test" />
</platform>

<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="Echo">
<param name="ios-package" value="CDVEcho"/>
</feature>
</config-file>

<header-file src="src/ios/CDVEcho.h" />
<source-file src="src/ios/CDVEcho.m" />
</platform>

</plugin>
@@ -0,0 +1,74 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package org.apache.cordova.test;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.util.Base64;

/**
* This class exposes methods in Cordova that can be called from JavaScript.
*/
public class Echo extends CordovaPlugin {

/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback context from which we were invoked.
* @return A PluginResult object with a status and message.
*/
@SuppressLint("NewApi")
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("echo")) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, args.getString(0)));
return true;
} else if(action.equals("echoAsync")) {
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, args.optString(0)));
}
});
return true;
} else if(action.equals("echoArrayBuffer")) {
String data = args.optString(0);
byte[] rawData= Base64.decode(data, Base64.DEFAULT);
callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, rawData));
return true;
} else if(action.equals("echoMultiPart")) {
callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, args.getJSONObject(0)));
return true;
}
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
return false;
} catch (JSONException e) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return false;
}
}
}
@@ -0,0 +1,23 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

#import <Cordova/CDVPlugin.h>

@interface CDVEcho : CDVPlugin
@end
@@ -0,0 +1,61 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

#import "CDVEcho.h"
#import <Cordova/CDV.h>

@implementation CDVEcho

- (void)echo:(CDVInvokedUrlCommand*)command
{
id message = [command.arguments objectAtIndex:0];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)echoAsyncHelper:(NSArray*)args
{
[self.commandDelegate sendPluginResult:[args objectAtIndex:0] callbackId:[args objectAtIndex:1]];
}

- (void)echoAsync:(CDVInvokedUrlCommand*)command
{
id message = [command.arguments objectAtIndex:0];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];

[self performSelector:@selector(echoAsyncHelper:) withObject:[NSArray arrayWithObjects:pluginResult, command.callbackId, nil] afterDelay:0];
}

- (void)echoArrayBuffer:(CDVInvokedUrlCommand*)command
{
id message = [command.arguments objectAtIndex:0];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:message];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)echoMultiPart:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:command.arguments];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end
@@ -0,0 +1,53 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

var exec = require('cordova/exec'),
utils = require('cordova/utils');

/**
* Sends the given message through exec() to the Echo plugin, which sends it back to the successCallback.
* @param successCallback invoked with a FileSystem object
* @param errorCallback invoked if error occurs retrieving file system
* @param message The string to be echoed.
* @param forceAsync Whether to force an async return value (for testing native->js bridge).
*/
module.exports = function(successCallback, errorCallback, message, forceAsync) {
var action = 'echo';
var messageIsMultipart = (utils.typeName(message) == "Array");
var args = messageIsMultipart ? message : [message];

if (utils.typeName(message) == 'ArrayBuffer') {
if (forceAsync) {
console.warn('Cannot echo ArrayBuffer with forced async, falling back to sync.');
}
action += 'ArrayBuffer';
} else if (messageIsMultipart) {
if (forceAsync) {
console.warn('Cannot echo MultiPart Array with forced async, falling back to sync.');
}
action += 'MultiPart';
} else if (forceAsync) {
action += 'Async';
}

exec(successCallback, errorCallback, "Echo", action, args);
};

@@ -46,4 +46,5 @@
<dependency id="org.apache.cordova.core.splashscreen" url="." subdir="../cordova-plugin-splashscreen" />
<dependency id="org.apache.cordova.core.vibration" url="." subdir="../cordova-plugin-vibration" />
<dependency id="org.apache.cordova.test.whitelist" url="." subdir="cordova-plugin-whitelist" />
<dependency id="org.apache.cordova.test.echo" url="." subdir="cordova-plugin-echo" />
</plugin>

0 comments on commit f5c0a54

Please sign in to comment.