Skip to content

Commit e608ae7

Browse files
Updated Diffusion Examples to 6.11.2
1 parent bb22e20 commit e608ae7

File tree

268 files changed

+4557
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+4557
-31
lines changed

android/pubsub/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Android example showing basic use of the Diffusion API
2+
3+
This is an Android application that demonstrates how
4+
to create, update, and subscribe to a Diffusion topic.
5+
6+
7+
8+
## Building the example
9+
10+
11+
You can build the example from the command line using gradle, but we recommend importing
12+
the example into Android Studio as a project. The following has been tested with Android
13+
Studio 3.2.1.
14+
15+
1. Copy the `diffusion-android.jar` to the `app/libs` directory.
16+
17+
2. Start Android Studio. Use the
18+
`File/New/Import Project...` menu option, and select the `examples\android\pubsub` directory.
19+
20+
3. Use the `Build/Make Project` menu option.
21+
22+
## Running the example
23+
24+
25+
1. Start the Diffusion server.
26+
27+
2. Use Android Studio (`Run/Run "app"`) to deploy the example to the Android emulator. Create a new virtual device if one doesn't already exist, using API level 19 or higher.
28+
29+
3. The example doesn't use Android UI features. To see the output, use Android Studio to examine the log.

android/pubsub/app/build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
defaultConfig {
6+
multiDexEnabled true
7+
applicationId "com.pushtechnology.examples"
8+
minSdkVersion 19
9+
targetSdkVersion 27
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
20+
compileOptions {
21+
coreLibraryDesugaringEnabled true
22+
sourceCompatibility JavaVersion.VERSION_1_8
23+
targetCompatibility JavaVersion.VERSION_1_8
24+
}
25+
}
26+
27+
dependencies {
28+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
29+
implementation 'org.slf4j:slf4j-api:1.7.21'
30+
implementation fileTree(dir: 'libs', include: ['*.jar'])
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.pushtechnology.diffusion.examples">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:supportsRtl="true">
12+
<activity android:name=".PubSubExample">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2017, 2019 Push Technology Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*******************************************************************************/
15+
16+
package com.pushtechnology.diffusion.examples;
17+
18+
import java.util.concurrent.Executors;
19+
import java.util.concurrent.ScheduledExecutorService;
20+
import java.util.concurrent.TimeUnit;
21+
import java.util.concurrent.atomic.AtomicLong;
22+
import java.util.Locale;
23+
24+
import com.pushtechnology.diffusion.client.Diffusion;
25+
import com.pushtechnology.diffusion.client.features.Topics;
26+
import com.pushtechnology.diffusion.client.features.control.topics.TopicControl;
27+
import com.pushtechnology.diffusion.client.session.Session;
28+
import com.pushtechnology.diffusion.client.session.SessionFactory;
29+
import com.pushtechnology.diffusion.client.topics.details.TopicSpecification;
30+
import com.pushtechnology.diffusion.client.topics.details.TopicType;
31+
32+
import android.app.Activity;
33+
import android.os.Bundle;
34+
import android.util.Log;
35+
import android.widget.TextView;
36+
37+
import java8.util.concurrent.CompletableFuture;
38+
39+
/**
40+
* Android example showing basic use of the Diffusion API.
41+
*
42+
* <p>
43+
* Start a Diffusion server and the Android emulator on the same machine, then
44+
* build and deploy this example to the emulator.
45+
*
46+
* @author DiffusionData Limited
47+
* @since 6.2
48+
*/
49+
public class PubSubExample extends Activity {
50+
51+
private final ScheduledExecutorService executor =
52+
Executors.newSingleThreadScheduledExecutor();
53+
54+
@Override
55+
protected void onCreate(Bundle savedInstanceState) {
56+
super.onCreate(savedInstanceState);
57+
setContentView(R.layout.activity_example);
58+
59+
startSession();
60+
}
61+
62+
private void startSession() {
63+
64+
// Configure a session factory that connects to Diffusion using the "admin" account.
65+
// The principal name, password, and server port match the sample configuration
66+
// in the standard Diffusion installation.
67+
// The 10.0.2.2 IP address provided as the server host is the address of the machine
68+
// running the Diffusion Android emulator.
69+
70+
final SessionFactory sessionFactory = Diffusion.sessions()
71+
.principal("admin")
72+
.password("password")
73+
.serverHost("10.0.2.2")
74+
.serverPort(8080);
75+
76+
// For compatibility with Android SDK's earlier than API level 24, Diffusion uses a backport
77+
// of JDK 8 classes such as CompletableFuture. Note the java8 package name in the import
78+
// statement above.
79+
final CompletableFuture<Session> future =
80+
// Use the asynchronous openAsync method() to connect in a background thread.
81+
// Android prohibits blocking network operations in the main UI thread.
82+
sessionFactory.openAsync();
83+
84+
future.whenComplete((session, ex) -> {
85+
if (ex != null) {
86+
Log.e("diffusion", "Failed to connect to Diffusion server, is it running? Will retry.", ex);
87+
executor.schedule(this::startSession, 10, TimeUnit.SECONDS);
88+
} else {
89+
example(session);
90+
}
91+
});
92+
}
93+
94+
private void example(Session session) {
95+
96+
final Topics topics = session.feature(Topics.class);
97+
98+
// Ask the server to subscribe to a topic called "counter". It doesn't matter that this
99+
// is done before a value stream is added, nor that the topic may not yet exist. The
100+
// server remembers the session has selected the counter topic, and re-evaluates
101+
// subscriptions as topics are added and removed.
102+
topics.subscribe("counter");
103+
104+
// Add the int64 "counter" topic.
105+
session.feature(TopicControl.class).addTopic("counter", TopicType.INT64)
106+
.whenComplete((result, ex) -> {
107+
if (ex != null) {
108+
// This can fail because there is an incompatible topic called "counter";
109+
// if the session has insufficient permissions to create the topic; or if
110+
// the session is closed.
111+
Log.w("diffusion", "Failed to create topic", ex);
112+
}
113+
else {
114+
// Result will be either AddTopicResult.CREATED or AddTopicResult.EXISTS,
115+
// depending on whether the topic already exists on the server.
116+
Log.i("diffusion", "Created topic with result " + result);
117+
}
118+
});
119+
120+
final AtomicLong i = new AtomicLong(0);
121+
122+
// Schedule a recurring task that increments the counter and updates the topic.
123+
executor.scheduleAtFixedRate(
124+
() -> topics.set("counter", Long.class, i.getAndIncrement()),
125+
1, 1, TimeUnit.SECONDS);
126+
127+
/// Add a value stream to dispatch the topic events locally.
128+
topics.addStream("counter", Long.class, new Topics.ValueStream.Default<Long>() {
129+
@Override
130+
public void onSubscription(String topicPath, TopicSpecification specification) {
131+
Log.i("diffusion", "Subscribed to: " + topicPath);
132+
}
133+
134+
@Override
135+
public void onValue(
136+
String topicPath,
137+
TopicSpecification specification,
138+
Long oldValue,
139+
Long newValue) {
140+
141+
runOnUiThread(new Runnable() {
142+
@Override
143+
public void run() {
144+
TextView valueView = (TextView)findViewById(R.id.value);
145+
valueView.setText(String.format(Locale.getDefault(),
146+
"Subscribed to 'counter' topic: %d", newValue));
147+
}
148+
});
149+
}
150+
});
151+
}
152+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<TextView
7+
android:id="@+id/value"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:text="Hello World!" />
11+
12+
</RelativeLayout>
3.09 KB
Loading
1.86 KB
Loading
4.35 KB
Loading
7.77 KB
Loading

0 commit comments

Comments
 (0)