Skip to content

Implement backend discovery. #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

package com.google.android.datatransport;

/** Each factory is backed by a single transport backend. */
/**
* Each factory is backed by a single transport backend.
*
* <p>Note: implementations <strong>must</strong> have a no-argument constructor, otherwise the
* runtime won't be able to instantiate them.
*/
public interface TransportFactory {
/**
* Returns a named transport instance that can be used to send values of type T.
Expand Down
16 changes: 13 additions & 3 deletions transport/transport-backend-cct/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.datatransport.backend.cct">
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
<!--<uses-sdk android:minSdkVersion="14"/>-->
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
<!--<uses-sdk android:minSdkVersion="14"/>-->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />

<application>
<service
android:name="com.google.android.datatransport.runtime.backends.TransportBackendDiscovery"
android:exported="false">
<meta-data
android:name="backend:com.google.android.datatransport.cct.CctBackendFactory"
android:value="cct" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018 Google LLC
//
// 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 com.google.android.datatransport.cct;

import android.support.annotation.Keep;
import android.support.annotation.VisibleForTesting;
import com.google.android.datatransport.runtime.backends.BackendFactory;
import com.google.android.datatransport.runtime.backends.CreationContext;
import com.google.android.datatransport.runtime.backends.TransportBackend;

@Keep
public class CctBackendFactory implements BackendFactory {
private static final String URL = mergeStrings("hts/pa.ogepscmlgbth", "tp:/lygolai.o/o/ac");

@Override
public TransportBackend create(CreationContext creationContext) {
return new CctTransportBackend(
URL, creationContext.getWallClock(), creationContext.getMonotonicClock());
}

@VisibleForTesting
static String mergeStrings(String part1, String part2) {
int sizeDiff = part1.length() - part2.length();
if (sizeDiff < 0 || sizeDiff > 1) {
throw new IllegalArgumentException("Invalid input received");
}

StringBuilder url = new StringBuilder(part1.length() + part2.length());

for (int i = 0; i < part1.length(); i++) {
url.append(part1.charAt(i));
if (part2.length() > i) {
url.append(part2.charAt(i));
}
}

return url.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@
package com.google.android.datatransport.cct;

import android.os.Build;
import android.support.annotation.VisibleForTesting;
import com.google.android.datatransport.cct.proto.AndroidClientInfo;
import com.google.android.datatransport.cct.proto.BatchedLogRequest;
import com.google.android.datatransport.cct.proto.ClientInfo;
import com.google.android.datatransport.cct.proto.LogEvent;
import com.google.android.datatransport.cct.proto.LogRequest;
import com.google.android.datatransport.cct.proto.LogResponse;
import com.google.android.datatransport.cct.proto.QosTierConfiguration;
import com.google.android.datatransport.runtime.BackendRequest;
import com.google.android.datatransport.runtime.BackendResponse;
import com.google.android.datatransport.runtime.BackendResponse.Status;
import com.google.android.datatransport.runtime.EventInternal;
import com.google.android.datatransport.runtime.TransportBackend;
import com.google.android.datatransport.runtime.backends.BackendRequest;
import com.google.android.datatransport.runtime.backends.BackendResponse;
import com.google.android.datatransport.runtime.backends.BackendResponse.Status;
import com.google.android.datatransport.runtime.backends.TransportBackend;
import com.google.android.datatransport.runtime.time.Clock;
import com.google.android.datatransport.runtime.time.UptimeClock;
import com.google.android.datatransport.runtime.time.WallTimeClock;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.ByteArrayOutputStream;
Expand All @@ -50,9 +47,9 @@
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;

public class GoogleTransportBackend implements TransportBackend {
final class CctTransportBackend implements TransportBackend {

private static final Logger LOGGER = Logger.getLogger(GoogleTransportBackend.class.getName());
private static final Logger LOGGER = Logger.getLogger(CctTransportBackend.class.getName());
private static final int CONNECTION_TIME_OUT = 30000;
private static final int READ_TIME_OUT = 40000;
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding";
Expand Down Expand Up @@ -80,17 +77,12 @@ private static URL parseUrlOrThrow(String url) {
}
}

@VisibleForTesting
GoogleTransportBackend(String url, Clock wallTimeClock, Clock uptimeClock) {
CctTransportBackend(String url, Clock wallTimeClock, Clock uptimeClock) {
this.endPoint = parseUrlOrThrow(url);
this.uptimeClock = uptimeClock;
this.wallTimeClock = wallTimeClock;
}

public GoogleTransportBackend() {
this("https://play.googleapis.com/log/batch", new WallTimeClock(), new UptimeClock());
}

@Override
public EventInternal decorate(EventInternal eventInternal) {
return eventInternal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018 Google LLC
//
// 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 com.google.android.datatransport.cct;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CctBackendFactoryTest {
@Test
public void mergeStrings_whenPartsAreUnequalLength() {
String part1 = "hts/eapecm";
String part2 = "tp:/xml.o";
assertThat(CctBackendFactory.mergeStrings(part1, part2)).isEqualTo("https://example.com");
}

@Test
public void mergeStrings_whenPartsAreEqualLength() {
String part1 = "hts/eape.o";
String part2 = "tp:/xmlscm";
assertThat(CctBackendFactory.mergeStrings(part1, part2)).isEqualTo("https://examples.com");
}

@Test
public void mergeStrings_whenPart2IsLongerThanPart1() {
String part1 = "135";
String part2 = "2467";
assertThrows(
IllegalArgumentException.class, () -> CctBackendFactory.mergeStrings(part1, part2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.android.datatransport.Priority;
import com.google.android.datatransport.cct.proto.LogResponse;
import com.google.android.datatransport.runtime.BackendRequest;
import com.google.android.datatransport.runtime.BackendResponse;
import com.google.android.datatransport.runtime.BackendResponse.Status;
import com.google.android.datatransport.runtime.EventInternal;
import com.google.android.datatransport.runtime.backends.BackendRequest;
import com.google.android.datatransport.runtime.backends.BackendResponse;
import com.google.android.datatransport.runtime.backends.BackendResponse.Status;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class GoogleTransportBackendTest {
public class CctTransportBackendTest {

private static String TEST_ENDPOINT = "http://localhost:8999/api";
private static GoogleTransportBackend BACKEND =
new GoogleTransportBackend(TEST_ENDPOINT, () -> 3, () -> 1);
private static CctTransportBackend BACKEND =
new CctTransportBackend(TEST_ENDPOINT, () -> 3, () -> 1);
private static String TRANSPORT_NAME = "3";

@Rule public WireMockRule wireMockRule = new WireMockRule(8999);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ dependencies {
//Android compatible version of Apache httpclient.
testImplementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
testImplementation 'org.robolectric:robolectric:4.2'
testImplementation 'junit:junit:4.13-beta-2'

androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.android.support:support-annotations:28.0.0'
Expand Down
3 changes: 2 additions & 1 deletion transport/transport-runtime/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=16.0.0
version=16.0.0
android.enableUnitTestBinaryResources=true
10 changes: 8 additions & 2 deletions transport/transport-runtime/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.datatransport.runtime">
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
<!--<uses-sdk android:minSdkVersion="14"/>-->
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
<!--<uses-sdk android:minSdkVersion="14"/>-->

<application>
<service
android:name=".backends.TransportBackendDiscovery"
android:exported="false" />
</application>
</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class TransportRuntime implements TransportInternal {

private static volatile TransportRuntimeComponent INSTANCE = null;

private final BackendRegistry backendRegistry;
private final Clock eventClock;
private final Clock uptimeClock;
private final Scheduler scheduler;
Expand All @@ -48,13 +47,11 @@ public class TransportRuntime implements TransportInternal {

@Inject
TransportRuntime(
BackendRegistry backendRegistry,
@WallTime Clock eventClock,
@Monotonic Clock uptimeClock,
Scheduler scheduler,
SynchronizationGuard guard,
Uploader uploader) {
this.backendRegistry = backendRegistry;
this.eventClock = eventClock;
this.uptimeClock = uptimeClock;
this.scheduler = scheduler;
Expand Down Expand Up @@ -92,11 +89,6 @@ public static TransportRuntime getInstance() {
return localRef.getTransportRuntime();
}

/** Register a {@link TransportBackend}. */
public void register(String name, TransportBackend backend) {
backendRegistry.register(name, backend);
}

/** Returns a {@link TransportFactory} for a given {@code backendName}. */
public TransportFactory newFactory(String backendName) {
return new TransportFactoryImpl(backendName, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import android.content.Context;
import android.os.Build;
import com.google.android.datatransport.runtime.backends.BackendRegistry;
import com.google.android.datatransport.runtime.backends.CreationContext;
import com.google.android.datatransport.runtime.scheduling.ImmediateScheduler;
import com.google.android.datatransport.runtime.scheduling.Scheduler;
import com.google.android.datatransport.runtime.scheduling.jobscheduling.AlarmManagerScheduler;
Expand All @@ -34,6 +36,7 @@
import dagger.Provides;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.inject.Singleton;

@Module
abstract class TransportRuntimeModule {
Expand Down Expand Up @@ -79,4 +82,11 @@ static Scheduler scheduler(

@Binds
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);

@Provides
@Singleton
static CreationContext creationContext(
@WallTime Clock wallClock, @Monotonic Clock monotonicClock) {
return CreationContext.create(wallClock, monotonicClock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 Google LLC
//
// 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 com.google.android.datatransport.runtime.backends;

/** Used by backend discovery to create {@link TransportBackend}s. */
public interface BackendFactory {
TransportBackend create(CreationContext creationContext);
}
Loading