-
Notifications
You must be signed in to change notification settings - Fork 61
add event subscribe module and implement subscribe function #27
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/main/java/org/fisco/bcos/sdk/eventsub/EventSubscribeImp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright 2014-2020 [fisco-dev] | ||
| * | ||
| * 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 org.fisco.bcos.sdk.eventsub; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import java.util.List; | ||
| import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.fisco.bcos.sdk.channel.Channel; | ||
| import org.fisco.bcos.sdk.channel.ResponseCallback; | ||
| import org.fisco.bcos.sdk.eventsub.filter.EventLogFilter; | ||
| import org.fisco.bcos.sdk.eventsub.filter.EventLogFilterStatus; | ||
| import org.fisco.bcos.sdk.eventsub.filter.EventLogResponse; | ||
| import org.fisco.bcos.sdk.eventsub.filter.EventPushMsgHandler; | ||
| import org.fisco.bcos.sdk.eventsub.filter.EventSubNodeRespStatus; | ||
| import org.fisco.bcos.sdk.eventsub.filter.FilterManager; | ||
| import org.fisco.bcos.sdk.eventsub.filter.ScheduleTimeConfig; | ||
| import org.fisco.bcos.sdk.model.Message; | ||
| import org.fisco.bcos.sdk.model.MsgType; | ||
| import org.fisco.bcos.sdk.model.Response; | ||
| import org.fisco.bcos.sdk.utils.ObjectMapperFactory; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class EventSubscribeImp implements EventSubscribe { | ||
| private static final Logger logger = LoggerFactory.getLogger(EventSubscribeImp.class); | ||
| private Channel ch; | ||
| private String groupId; | ||
| private FilterManager filterManager; | ||
| private EventPushMsgHandler msgHander; | ||
| private boolean running = false; | ||
| ScheduledThreadPoolExecutor resendSchedule = new ScheduledThreadPoolExecutor(1); | ||
|
|
||
| public EventSubscribeImp(Channel ch, String groupId) { | ||
| this.ch = ch; | ||
| this.groupId = groupId; | ||
| filterManager = new FilterManager(); | ||
| msgHander = new EventPushMsgHandler(filterManager); | ||
| ch.addMessageHandler(MsgType.EVENT_LOG_PUSH, msgHander); | ||
| } | ||
|
|
||
| @Override | ||
| public void subscribeEvent(EventLogParams params, EventCallback callback) { | ||
| if (!params.valid()) { | ||
| callback.onReceiveLog(EventSubNodeRespStatus.INVALID_PARAMS.getStatus(), null); | ||
| return; | ||
| } | ||
| EventLogFilter filter = new EventLogFilter(); | ||
| filter.setRegisterID(EventSubscribe.newSeq()); | ||
| filter.setParams(params); | ||
| filter.setCallback(callback); | ||
| filterManager.addFilter(filter); | ||
| sendFilter(filter); | ||
| } | ||
|
|
||
| @Override | ||
| public void unsubscribeEvent(String filterID) { | ||
| // Todo need node support | ||
| } | ||
|
|
||
| @Override | ||
| public List<EventLogFilter> getAllSubscribedEvent() { | ||
| return filterManager.getAllSubscribedEvent(); | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| if (running) { | ||
| return; | ||
| } | ||
| running = true; | ||
| resendSchedule.scheduleAtFixedRate( | ||
| () -> { | ||
| resendWaitingFilters(); | ||
| }, | ||
| 0, | ||
| ScheduleTimeConfig.resendFrequency, | ||
| TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| resendSchedule.shutdown(); | ||
| } | ||
|
|
||
| private void resendWaitingFilters() { | ||
| List<EventLogFilter> filters = filterManager.getWaitingReqFilters(); | ||
| for (EventLogFilter filter : filters) { | ||
| sendFilter(filter); | ||
| } | ||
| logger.info("Resend waiting filters, size: {}", filters.size()); | ||
| } | ||
|
|
||
| private void sendFilter(EventLogFilter filter) { | ||
| Message msg = new Message(); | ||
| msg.setSeq(EventSubscribe.newSeq()); | ||
| msg.setType((short) MsgType.CLIENT_REGISTER_EVENT_LOG.ordinal()); | ||
| msg.setResult(0); | ||
| try { | ||
| String content = filter.getNewParamJsonString(groupId); | ||
| msg.setData(content.getBytes()); | ||
| } catch (JsonProcessingException e) { | ||
| logger.error( | ||
| "send filter error, registerID: {},filterID : {}, error: {}", | ||
| filter.getRegisterID(), | ||
| filter.getFilterID(), | ||
| e.getMessage()); | ||
| logger.error( | ||
| "remove bad filter , registerID: {},filterID : {}", | ||
| filter.getRegisterID(), | ||
| filter.getFilterID()); | ||
| filterManager.removeFilter(filter.getRegisterID()); | ||
| } | ||
|
|
||
| filterManager.addCallback(filter.getFilterID(), filter.getCallback()); | ||
|
|
||
| // Todo check send to group function. this function may not in channel module. | ||
| ch.asyncSendToGroup( | ||
| msg, | ||
| groupId, | ||
| new RegisterEventSubRespCallback( | ||
| filterManager, filter, filter.getFilterID(), filter.getRegisterID())); | ||
| } | ||
|
|
||
| class RegisterEventSubRespCallback extends ResponseCallback { | ||
| FilterManager filterManager; | ||
| EventLogFilter filter; | ||
| String filterID; | ||
| String registerID; | ||
|
|
||
| public RegisterEventSubRespCallback( | ||
| FilterManager filterManager, | ||
| EventLogFilter filter, | ||
| String filterID, | ||
| String registerID) { | ||
| this.filterManager = filterManager; | ||
| this.filter = filter; | ||
| this.filterID = filterID; | ||
| this.registerID = registerID; | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(Response response) { | ||
| logger.info( | ||
| " event filter callback response, registerID: {}, filterID: {}, seq: {}, error code: {}, content: {}", | ||
| registerID, | ||
| filterID, | ||
| response.getMessageID(), | ||
| response.getErrorCode(), | ||
| response.getContent()); | ||
| try { | ||
| if (0 == response.getErrorCode()) { | ||
| EventLogResponse resp = | ||
| ObjectMapperFactory.getObjectMapper() | ||
| .readValue(response.getContent(), EventLogResponse.class); | ||
| if (resp.getResult() == 0) { | ||
| // node give an "OK" response, event log will be pushed soon | ||
| filterManager.updateFilterStatus( | ||
| filter, EventLogFilterStatus.EVENT_LOG_PUSHING, null); | ||
| } else { | ||
| // node give a bad response, will not push event log, trigger callback | ||
| filter.getCallback().onReceiveLog(resp.getResult(), null); | ||
| filterManager.removeFilter(registerID); | ||
| filterManager.removeCallback(filterID); | ||
| } | ||
| } else { | ||
| filterManager.updateFilterStatus( | ||
| filter, EventLogFilterStatus.WAITING_REQUEST, null); | ||
| filterManager.removeCallback(filterID); | ||
| } | ||
| } catch (Exception e) { | ||
| logger.error( | ||
| " event filter response message exception, filterID: {}, registerID: {}, exception message: {}", | ||
| filterID, | ||
| registerID, | ||
| e.getMessage()); | ||
| filter.getCallback() | ||
| .onReceiveLog(EventSubNodeRespStatus.OTHER_ERROR.getStatus(), null); | ||
| filterManager.removeFilter(registerID); | ||
| filterManager.removeCallback(filterID); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该是filterId吧,和groupId一样保持命名风格