@@ -2,33 +2,58 @@ pragma solidity ^0.5.0;
22
33contract MessageReceiver {
44
5+ // Each receiver manages messages for only one topic
56 string public topicName;
7+ // Message key to message mapping for look-up
68 mapping (bytes32 => Message) public messages;
9+ // Message key stored in an array for ordering/indexing
710 bytes32 [] public messageList;
811
9- event MessageReceived (address msgFrom , bytes32 msgKey );
12+ // Event to notify listening/interested parties
13+ event MessageReceived (string topicName , address msgFrom , bytes32 msgKey );
1014
1115 struct Message {
1216 address sender;
1317 string message;
1418 }
1519
16- constructor (string memory _topicName ) public {
17- topicName = _topicName;
20+ /**
21+ * @notice Create a new MessageReceiver for a specific topic
22+ *
23+ * @param topic string The topic name for the messages
24+ */
25+ constructor (string memory topic ) public {
26+ topicName = topic;
1827 }
1928
29+ /**
30+ * @notice Send a new message to the "bus". Emits a MessageReceived event.
31+ *
32+ * @param key bytes32 The key for your message, should be unique
33+ * @param newMessage string The message contents
34+ */
2035 function sendMessage (bytes32 key , string memory newMessage ) public {
2136 messageList.push (key);
2237 messages[key].sender = msg .sender ;
2338 messages[key].message = newMessage;
2439
25- emit MessageReceived (msg .sender , key);
40+ emit MessageReceived (topicName, msg .sender , key);
2641 }
2742
43+ /**
44+ * @notice Gets the count of the messages sent to this topic
45+ *
46+ * @return uint256 Count of messages
47+ */
2848 function getMessageCount () public view returns (uint256 ) {
2949 return messageList.length ;
3050 }
3151
52+ /**
53+ * @notice Gets a message given a key
54+ *
55+ * @return address, string Returns the sender address and message contents
56+ */
3257 function getMessage (bytes32 key ) public view returns (address , string memory ) {
3358 return (messages[key].sender, messages[key].message);
3459 }
0 commit comments