Skip to content
Open
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
95 changes: 95 additions & 0 deletions privmx-endpoint/src/main/cpp/modules/StreamApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,99 @@ Java_com_simplito_java_privmx_1endpoint_modules_stream_StreamApi_deinit(
e.what()
);
}
}

extern "C"
JNIEXPORT jobject JNICALL
Java_com_simplito_java_privmx_1endpoint_modules_stream_StreamApi_subscribeFor(
JNIEnv *env,
jobject thiz,
jobject subscription_queries
) {
JniContextUtils ctx(env);
if (ctx.nullCheck(subscription_queries, "Subscription queries")) {
return nullptr;
}

jobject result;
ctx.callResultEndpointApi<jobject>(
&result,
[&ctx, &thiz, &subscription_queries]() -> jobject {

std::vector<std::string> queries = jArrayToVector<std::string>(
ctx,
ctx.jObject2jArray(subscription_queries),
jobject2string
);

std::vector<std::string> subscription_ids_c =
getStreamApi(ctx, thiz)->subscribeFor(queries);

return vectorTojArray(
ctx,
subscription_ids_c,
string2jobject
);
}
);
if (ctx->ExceptionCheck()) {
return nullptr;
}
return result;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_simplito_java_privmx_1endpoint_modules_stream_StreamApi_buildSubscriptionQuery(
JNIEnv *env,
jobject thiz,
jlong event_type,
jlong selector_type,
jstring selector_id
) {
JniContextUtils ctx(env);
if (ctx.nullCheck(selector_id, "SelectorID")) {
return nullptr;
}

jstring result = nullptr;
ctx.callResultEndpointApi<jstring>(
&result,
[&ctx, &thiz, &event_type, &selector_type, &selector_id]() {
std::string query_result_c = getStreamApi(ctx, thiz)->buildSubscriptionQuery(
static_cast<stream::EventType>(event_type),
static_cast<stream::EventSelectorType>(selector_type),
ctx.jString2string(selector_id)
);
return ctx->NewStringUTF(query_result_c.c_str());
}
);
if (ctx->ExceptionCheck()) {
return nullptr;
}
return result;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_simplito_java_privmx_1endpoint_modules_stream_StreamApi_unsubscribeFrom(
JNIEnv *env,
jobject thiz,
jobject subscription_ids
) {
JniContextUtils ctx(env);
if (ctx.nullCheck(subscription_ids, "Subscription IDs")) {
return;
}

ctx.callVoidEndpointApi([&ctx, &thiz, &subscription_ids]() {
auto subscription_ids_arr = ctx.jObject2jArray(subscription_ids);
auto subscription_ids_c = jArrayToVector<std::string>(
ctx,
subscription_ids_arr,
jobject2string
);

getStreamApi(ctx, thiz)->unsubscribeFrom(subscription_ids_c);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.simplito.java.privmx_endpoint.model.events.eventSelectorTypes;

public enum StreamEventSelectorType implements EventSelectorType{
CONTEXT_ID,
STREAMROOM_ID,
STREAM_ID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.simplito.java.privmx_endpoint.model.events.eventTypes;

public enum StreamEventType implements EventType{
STREAMROOM_CREATE,
STREAMROOM_UPDATE,
STREAMROOM_DELETE,
STREAM_JOIN,
STREAM_LEAVE,
STREAM_PUBLISH,
STREAM_UNPUBLISH
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.simplito.java.privmx_endpoint.modules.stream;

import com.simplito.java.privmx_endpoint.LibLoader;
import com.simplito.java.privmx_endpoint.model.events.eventSelectorTypes.StreamEventSelectorType;
import com.simplito.java.privmx_endpoint.model.events.eventTypes.StreamEventType;
import com.simplito.java.privmx_endpoint.modules.core.Connection;
import com.simplito.java.privmx_endpoint.modules.event.EventApi;

import java.util.List;
import java.util.Objects;

public class StreamApi implements AutoCloseable {
Expand All @@ -23,6 +26,20 @@ public StreamApi(Connection connection, EventApi eventApi) throws IllegalStateEx
this.api = init(connection, eventApi);
}

public native List<String> subscribeFor(List<String> subscriptionQueries);

public String buildSubscriptionQuery(StreamEventType eventType, StreamEventSelectorType selectorType, String selectorId) {
return buildSubscriptionQuery(
eventType.ordinal(),
selectorType.ordinal(),
selectorId
);
}

private native String buildSubscriptionQuery(long eventType, long selectorType, String selectorId);

public native void unsubscribeFrom(List<String> subscriptionIds);

@Override
public void close() throws Exception {
deinit();
Expand Down