-
Notifications
You must be signed in to change notification settings - Fork 4
Add a PEP scenario #58
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
313a843
Use the latest escalus
chrzaszcz f1bf08f
Support a static domain variant in dynamic_domains.erl
chrzaszcz 5856dd7
Extend amoc_xmpp_presence with caps and subscriptions
chrzaszcz 6922bcd
Use the 'make_jid' helper in scenarios
chrzaszcz 26c3e8f
Add a helper module for PEP
chrzaszcz fbcb3ff
Add a typical publish+notify scenario
chrzaszcz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| -module(amoc_xmpp_pep). | ||
|
|
||
| -moduledoc """ | ||
| Building blocks for PEP (XEP-0163: Personal Eventing Protocol) scenarios: | ||
| - Determining the names of nodes to publish (see nodes_per_user) | ||
| - Item publishing (auto-create makes separate node creation unnecessary) | ||
| - Counting incoming item notifications and measuring TTD | ||
| """. | ||
|
|
||
| -include_lib("escalus/include/escalus_xmlns.hrl"). | ||
| -include_lib("exml/include/exml.hrl"). | ||
| -include_lib("kernel/include/logger.hrl"). | ||
|
|
||
| -export([init/0, | ||
| publish_item/2, | ||
| nodes_to_publish/0, | ||
| received_handler_spec/0]). | ||
|
|
||
| -define(V(X), (fun amoc_config_validation:X/1)). | ||
|
|
||
| -required_variable(#{name => nodes_per_user, default_value => 1, | ||
| verification => ?V(nonnegative_integer), | ||
| description => "Number of PEP nodes each user publishes to"}). | ||
|
|
||
| -spec init() -> ok. | ||
| init() -> | ||
| amoc_metrics:init(counters, pubsub_items_published), | ||
| amoc_metrics:init(counters, pubsub_notifications_received), | ||
| amoc_metrics:init(counters, timeouts), | ||
| amoc_metrics:init(times, pubsub_item_ttd), | ||
| amoc_metrics:init(times, response), | ||
| ok. | ||
|
|
||
| -spec nodes_to_publish() -> [binary()]. | ||
| nodes_to_publish() -> | ||
| [node_name(NodeId) || NodeId <- lists:seq(1, cfg(nodes_per_user))]. | ||
|
|
||
| -spec received_handler_spec() -> [amoc_xmpp_handlers:handler_spec()]. | ||
| received_handler_spec() -> | ||
| [{fun is_pep_notification/1, | ||
| fun record_pep_notification/3}]. | ||
|
|
||
| -spec publish_item(escalus:client(), escalus_pubsub_stanza:pubsub_node_name()) -> ok. | ||
| publish_item(Client, Node) -> | ||
| Id = escalus_stanza:id(), | ||
| Req = escalus_pubsub_stanza:publish(item_content(Client), Id, Node), | ||
| Pred = fun(Stanza) -> escalus_pred:is_iq_result(Req, Stanza) end, | ||
| Resp = amoc_xmpp:send_request_and_get_response(Client, Req, Pred, response, 10000), | ||
| ?LOG_DEBUG("~s got PEP publish response ~p", [escalus_client:username(Client), Resp]), | ||
| amoc_metrics:update_counter(pubsub_items_published). | ||
|
|
||
| -spec item_content(escalus:client()) -> exml:element(). | ||
| item_content(Client) -> | ||
| #xmlel{ | ||
| name = ~"entry", | ||
| attrs = #{~"timestamp" => integer_to_binary(os:system_time(microsecond)), | ||
| ~"jid" => escalus_client:short_jid(Client)}, | ||
| children = [#xmlcdata{content = cfg(message_body)}]}. | ||
|
|
||
| -spec node_name(pos_integer()) -> binary(). | ||
| node_name(NodeId) -> | ||
| iolist_to_binary([~"test-node-", integer_to_binary(NodeId)]). | ||
|
|
||
| -spec is_pep_notification(exml:element()) -> boolean(). | ||
| is_pep_notification(Stanza = #xmlel{name = ~"message"}) -> | ||
| EventXmlns = exml_query:path(Stanza, [{element, ~"event"}, {attr, ~"xmlns"}]), | ||
| Items = exml_query:path(Stanza, [{element, ~"event"}, {element, ~"items"}]), | ||
| EventXmlns =:= ?NS_PUBSUB_EVENT andalso Items =/= undefined; | ||
| is_pep_notification(_) -> | ||
| false. | ||
|
|
||
| -spec record_pep_notification(escalus:client(), exml:element(), | ||
| escalus_connection:metadata()) -> ok. | ||
| record_pep_notification(Client, Stanza, Metadata) -> | ||
| ?LOG_DEBUG("~s received PEP notification ~p", [escalus_client:username(Client), Stanza]), | ||
| amoc_metrics:update_counter(pubsub_notifications_received), | ||
| update_pubsub_item_ttd(Stanza, Metadata). | ||
|
|
||
| -spec update_pubsub_item_ttd(exml:element(), escalus_connection:metadata()) -> ok. | ||
| update_pubsub_item_ttd(Stanza, #{recv_timestamp := RecvTimestamp}) -> | ||
| Timestamp = exml_query:path(Stanza, [{element, ~"event"}, | ||
| {element, ~"items"}, | ||
| {element, ~"item"}, | ||
| {element, ~"entry"}, | ||
| {attr, ~"timestamp"}]), | ||
| amoc_metrics:update_time(pubsub_item_ttd, RecvTimestamp - binary_to_integer(Timestamp)). | ||
|
|
||
| cfg(Name) -> amoc_config:get(Name). | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.