Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
75
android/plugins/common/BufferingSonarPlugin.java
Normal file
75
android/plugins/common/BufferingSonarPlugin.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.common;
|
||||
|
||||
import com.facebook.sonar.core.SonarConnection;
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.core.SonarPlugin;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Sonar plugin that keeps events in a buffer until a connection is available.
|
||||
*
|
||||
* <p>In order to send data to the {@link SonarConnection}, use {@link #send(String, SonarObject)}
|
||||
* instead of {@link SonarConnection#send(String, SonarObject)}.
|
||||
*/
|
||||
public abstract class BufferingSonarPlugin implements SonarPlugin {
|
||||
|
||||
private static final int BUFFER_SIZE = 500;
|
||||
|
||||
private @Nullable RingBuffer<CachedSonarEvent> mEventQueue;
|
||||
private @Nullable SonarConnection mConnection;
|
||||
|
||||
@Override
|
||||
public synchronized void onConnect(SonarConnection connection) {
|
||||
mConnection = connection;
|
||||
|
||||
sendBufferedEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void onDisconnect() {
|
||||
mConnection = null;
|
||||
}
|
||||
|
||||
public synchronized SonarConnection getConnection() {
|
||||
return mConnection;
|
||||
}
|
||||
|
||||
public synchronized boolean isConnected() {
|
||||
return mConnection != null;
|
||||
}
|
||||
|
||||
public synchronized void send(String method, SonarObject sonarObject) {
|
||||
if (mEventQueue == null) {
|
||||
mEventQueue = new RingBuffer<>(BUFFER_SIZE);
|
||||
}
|
||||
mEventQueue.enqueue(new CachedSonarEvent(method, sonarObject));
|
||||
if (mConnection != null) {
|
||||
mConnection.send(method, sonarObject);
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void sendBufferedEvents() {
|
||||
if (mEventQueue != null && mConnection != null) {
|
||||
for (CachedSonarEvent cachedSonarEvent : mEventQueue.asList()) {
|
||||
mConnection.send(cachedSonarEvent.method, cachedSonarEvent.sonarObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class CachedSonarEvent {
|
||||
final String method;
|
||||
final SonarObject sonarObject;
|
||||
|
||||
private CachedSonarEvent(String method, SonarObject sonarObject) {
|
||||
this.method = method;
|
||||
this.sonarObject = sonarObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
40
android/plugins/common/MainThreadSonarReceiver.java
Normal file
40
android/plugins/common/MainThreadSonarReceiver.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.common;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import com.facebook.sonar.core.ErrorReportingRunnable;
|
||||
import com.facebook.sonar.core.SonarConnection;
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.core.SonarReceiver;
|
||||
import com.facebook.sonar.core.SonarResponder;
|
||||
|
||||
public abstract class MainThreadSonarReceiver implements SonarReceiver {
|
||||
|
||||
public MainThreadSonarReceiver(SonarConnection connection) {
|
||||
this.mConnection = connection;
|
||||
}
|
||||
|
||||
private final SonarConnection mConnection;
|
||||
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@Override
|
||||
public final void onReceive(final SonarObject params, final SonarResponder responder) {
|
||||
mHandler.post(
|
||||
new ErrorReportingRunnable(mConnection) {
|
||||
@Override
|
||||
public void runOrThrow() throws Exception {
|
||||
onReceiveOnMainThread(params, responder);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public abstract void onReceiveOnMainThread(SonarObject params, SonarResponder responder)
|
||||
throws Exception;
|
||||
}
|
||||
31
android/plugins/common/RingBuffer.java
Normal file
31
android/plugins/common/RingBuffer.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.common;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
final class RingBuffer<T> {
|
||||
final int mBufferSize;
|
||||
final List<T> mBuffer = new LinkedList<>();
|
||||
|
||||
RingBuffer(int bufferSize) {
|
||||
mBufferSize = bufferSize;
|
||||
}
|
||||
|
||||
void enqueue(T item) {
|
||||
if (mBuffer.size() >= mBufferSize) {
|
||||
mBuffer.remove(0);
|
||||
}
|
||||
mBuffer.add(item);
|
||||
}
|
||||
|
||||
List<T> asList() {
|
||||
return mBuffer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user