Skip to content

Commit

Permalink
refine demos
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Oct 17, 2011
1 parent bd93fc0 commit 3c7f400
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class SimplePubSubActivity extends Activity {
private static TextView mStatusline;
private static Button mStart;

private final AutobahnConnection mConnection = new AutobahnConnection();

private void alert(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
Expand Down Expand Up @@ -95,12 +97,14 @@ private static class MyEvent1 {

@Override
public String toString() {
return "{name: " + name + ", created: " + created + ", num: " + num + ", rand: " + rand + ", flag:" + flag + "}";
return "{name: " + name +
", created: " + created +
", num: " + num +
", rand: " + rand +
", flag:" + flag + "}";
}
}

private final AutobahnConnection mConnection = new AutobahnConnection();

private void test() {

final String wsuri = "ws://" + mHostname.getText() + ":" + mPort.getText();
Expand Down
2 changes: 1 addition & 1 deletion Demo/SimpleRpc/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="de.tavendo.autobahn.simplerpc.MainActivity"
<activity android:name=".SimpleRpcActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
14 changes: 11 additions & 3 deletions Demo/SimpleRpc/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:weightSum="1" android:background="#028ec9" android:padding="12dip">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="24dip" android:textStyle="bold" android:gravity="center" android:paddingBottom="16dip" android:text="Autobahn WebSockets Simple RPC"></TextView>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/lightgrey" android:text="Autobahn Server Hostname / Port"></TextView>
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
<EditText android:id="@+id/hostname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/darkgrey"></EditText>
<EditText android:id="@+id/port" android:layout_width="match_parent" android:inputType="number" android:layout_height="wrap_content" android:layout_weight="3" android:textColor="@color/darkgrey">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<Button android:layout_width="120dip" android:layout_gravity="center" android:layout_height="80dip" android:layout_margin="16dip" android:id="@+id/start" android:bufferType="editable" android:text="Button"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:textSize="18dip" android:textColor="@color/lightgrey" android:id="@+id/statusline" android:text="Ready."/>
</LinearLayout>
6 changes: 6 additions & 0 deletions Demo/SimpleRpc/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="autobahn">#028ec9</color>
<color name="lightgrey">#cccccc</color>
<color name="darkgrey">#333333</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/******************************************************************************
*
* Copyright 2011 Tavendo GmbH
*
* Licensed 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 de.tavendo.autobahn.simplerpc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import de.tavendo.autobahn.Autobahn;
import de.tavendo.autobahn.AutobahnConnection;

public class SimpleRpcActivity extends Activity {

@SuppressWarnings("unused")
private static final String TAG = "de.tavendo.autobahn.simplerpc";

private static final String PREFS_NAME = "AutobahnAndroidSimpleRpc";

private SharedPreferences mSettings;

private static EditText mHostname;
private static EditText mPort;
private static TextView mStatusline;
private static Button mStart;

private void alert(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

private void loadPrefs() {

mHostname.setText(mSettings.getString("hostname", ""));
mPort.setText(mSettings.getString("port", "9000"));
}

private void savePrefs() {

SharedPreferences.Editor editor = mSettings.edit();
editor.putString("hostname", mHostname.getText().toString());
editor.putString("port", mPort.getText().toString());
editor.commit();
}

private void setButtonConnect() {
mStart.setText("Connect");
mStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
test();
}
});
}

private void setButtonDisconnect() {
mStart.setText("Disconnect");
mStart.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mConnection.disconnect();
}
});
}

private final AutobahnConnection mConnection = new AutobahnConnection();

private void test() {

final String wsuri = "ws://" + mHostname.getText() + ":" + mPort.getText();

mStatusline.setText("Connecting to\n" + wsuri + " ..");

setButtonDisconnect();

// we establish a connection by giving the WebSockets URL of the server
// and the handler for open/close events
mConnection.connect(wsuri, new Autobahn.SessionHandler() {

@Override
public void onOpen() {

// The connection was successfully established. we set the status
// and save the host/port as Android application preference for next time.
mStatusline.setText("Connected to\n" + wsuri);
savePrefs();

// We establish a prefix to use for writing URIs using shorthand CURIE notation.
mConnection.prefix("calc", "http://example.com/simple/calc#");

testRpc();
}

@Override
public void onClose(int code, String reason) {

// The connection was closed. Set the status line, show a message box,
// and set the button to allow to connect again.
mStatusline.setText("Connection closed.");
alert(reason);
setButtonConnect();
}
});
}

private void testRpc() {

ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < 10; ++i) nums.add(i);

mConnection.call("calc:asum", Integer.class, new Autobahn.CallHandler() {

@Override
public void onResult(Object result) {
int res = (Integer) result;
alert("calc:asum result = " + res);
}

@Override
public void onError(String errorId, String errorInfo) {
alert("calc:asum RPC error - " + errorInfo);
}
}, nums);

mConnection.call("calc:add", Integer.class, new Autobahn.CallHandler() {

@Override
public void onResult(Object result) {
int res = (Integer) result;
alert("calc:add result = " + res);
}

@Override
public void onError(String errorId, String errorInfo) {
alert("calc:add RPC error - " + errorInfo);
}
}, 23, 55);
}

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mHostname = (EditText) findViewById(R.id.hostname);
mPort = (EditText) findViewById(R.id.port);
mStatusline = (TextView) findViewById(R.id.statusline);
mStart = (Button) findViewById(R.id.start);

mSettings = getSharedPreferences(PREFS_NAME, 0);
loadPrefs();

setButtonConnect();
}
}

0 comments on commit 3c7f400

Please sign in to comment.