From ad2b0fb6c3f3055567756bef24f7f11329b2b9b6 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Sun, 31 Oct 2021 23:07:14 +0800 Subject: [PATCH] feat: update event mechanism add event type local and global, clients have to use contractID and topic to subscribe local event; allow clients to subscribe topics (will not be recorded) --- contracts/EventPublisher/EventPublisher.yjs | 4 +- .../ws/ContractManagerFrameHandler.java | 66 +++++++++++-------- .../org/bdware/server/ws/EventActions.java | 30 +++++++++ 3 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org/bdware/server/ws/EventActions.java diff --git a/contracts/EventPublisher/EventPublisher.yjs b/contracts/EventPublisher/EventPublisher.yjs index b2e66f5..9eb1254 100644 --- a/contracts/EventPublisher/EventPublisher.yjs +++ b/contracts/EventPublisher/EventPublisher.yjs @@ -7,7 +7,7 @@ contract EventPublisher { * function te: publish an event with topic 'te' and default semantics AT_LEAST_ONCE * function tes: publish an event with topic 'te' and another semantics */ - event te; + event global te; /* * event declaration with semantics * valid semantics: AT_LEASE_ONCE, AT_MOST_ONCE, and ONLY_ONCE @@ -15,7 +15,7 @@ contract EventPublisher { * function tews: publish an event with topic 'tews' and semantics AT_MOST_ONCE * function tewss: publish an event with topic 'tews' and another semantics */ - event AT_MOST_ONCE tews; + event global tews(AT_MOST_ONCE); // publish an event with declared topic and default semantics AT_LEAST_ONCE export function pub1(e) { var content = YancloudUtil.random() + ' EventPublisher.pub1'; diff --git a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java index ae8e166..78a46c8 100644 --- a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java +++ b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java @@ -56,7 +56,9 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler 5) break; diff --git a/src/main/java/org/bdware/server/ws/EventActions.java b/src/main/java/org/bdware/server/ws/EventActions.java new file mode 100644 index 0000000..4b4a2a7 --- /dev/null +++ b/src/main/java/org/bdware/server/ws/EventActions.java @@ -0,0 +1,30 @@ +package org.bdware.server.ws; + +import com.google.gson.JsonObject; +import org.bdware.sc.ContractClient; +import org.bdware.sc.conn.ResultCallback; +import org.bdware.sc.util.HashUtil; +import org.bdware.server.action.Action; +import org.bdware.server.action.CMActions; + +public class EventActions { + @Action(async = true, userPermission = 0) + public void subEvent(JsonObject args, final ResultCallback rcb) { + if (!args.has("topic")) { + rcb.onResult("{\"status\":\"Error\",\"data\":\"no topic arg!\"}"); + return; + } + String topic = args.get("topic").getAsString(); + if (args.has("contractID")) { + String argCID = args.get("contractID").getAsString(); + ContractClient client = CMActions.manager.getClient(argCID); + if (null == client) { + rcb.onResult("{\"status\":\"Error\",\"data\":\"invalid contract ID or Name!\"}"); + return; + } + String contractID = client.getContractID(); + topic = HashUtil.sha3(contractID + topic); + } + CMActions.manager.subEventByClient(topic, rcb.getChannel()); + } +}