| EventStudio 2.5 Sequence diagram based system design and modeling | |
#include <Local_Status_Publisher.h> Collaboration diagram for LocalStatusPublisher:

While developing embedded system, one frequently encounters a situation where many entities are interested in occurrence of a particular event. This introduces a strong coupling between the publisher and subscriber of this event change notification. Thus whenever a new entity needs the information, code for the publisher of the information also needs to be modified to accommodate the new request.
The Publish-Subscribe Pattern solves the tight coupling problem. Here the coupling is removed by the publisher of information supporting a generic interface for subscribers. Entities interested in the information subscribe to the publisher by registering for the information. With this interface, the publisher code does not need to be modified every time a subscriber is introduced.
This class can be used as a helper class when this generic subscribe-publish interface is desired. The following important public methods are supported:
Definition at line 34 of file Local_Status_Publisher.h.
Public Types | |
| enum | OperationStatus |
| Status of an operation. | |
Public Member Functions | |
| void | PublishStatus (int unitId, int status) |
| PublishStatus() notifies subscribers about status change. | |
| OperationStatus | Register (Subscriber *pSubscriber) |
| Register() is invoked by objects inheriting from Subscriber. | |
| OperationStatus | Deregister (Subscriber *pSubscriber) |
| Deregister() removes a subscriber. | |
Private Types | |
| enum | { NOT_FOUND = -1, MAX_SUBSCRIBERS = 100 } |
Private Member Functions | |
| int | Find (Subscriber *pSubscriber) const |
| This method searches for an entry that matches the pointer. | |
Private Attributes | |
| Subscriber * | m_pSubscriber [MAX_SUBSCRIBERS] |
| Subscribers being tracked by the publisher. The entire list should be scanned for non NULL subscriber pointers. | |
|
Definition at line 36 of file Local_Status_Publisher.h. 00036 {
00038 NOT_FOUND = -1,
00039
00041 MAX_SUBSCRIBERS = 100
00042 };
|
|
This method searches for an entry that matches the pointer. It returns the index of the entry. If the entry is not found, it returns NOT_FOUND.
Definition at line 57 of file Local_Status_Publisher.h. 00058 {
00059 int index = NOT_FOUND;
00060 for (int i=0; i < MAX_SUBSCRIBERS; i++)
00061 {
00062 if (m_pSubscriber[i] == pSubscriber)
00063 {
00064 index = i;
00065 break;
00066 }
00067 }
00068 return index;
00069 }
|
| ||||||||||||
PublishStatus() notifies subscribers about status change. Invoke this method whenever you need to update the status to all the subscribers of the status information.
Definition at line 85 of file Local_Status_Publisher.h. 00086 {
00087 for (int i=0; i < MAX_SUBSCRIBERS; i++)
00088 {
00089 // A valid subscriber exists only when the pointer is non NULL
00090 if (m_pSubscriber[i])
00091 {
00092 m_pSubscriber[i]->HandleStatusChange(unitId, status);
00093 }
00094 }
00095 }
|
|
Register() is invoked by objects inheriting from Subscriber. Once registered, the object is notified whenever status change is detected. The method returns FAILURE in the following cases:
Definition at line 108 of file Local_Status_Publisher.h. 00109 {
00110 OperationStatus status = FAILURE;
00111 // First check if the subscriber is already present
00112 // in the list
00113 int index = Find(pSubscriber);
00114
00115 if (index == NOT_FOUND)
00116 {
00117 // Subscriber was not found, so this is not a duplicate request
00118 // Now look for a free entry by finding NULL
00119 index = Find(NULL);
00120 if (index != NOT_FOUND)
00121 {
00122 // A free entry has been found
00123 m_pSubscriber[index] = pSubscriber;
00124 status = SUCCESS;
00125 }
00126 }
00127 return status;
00128 }
|
|
Deregister() removes a subscriber. Returns FAILURE if the subscriber was not registered in the first place.
Definition at line 138 of file Local_Status_Publisher.h. 00139 {
00140 OperationStatus status = FAILURE;
00141 // Search for the entry
00142 int index = Find(pSubscriber);
00143 if (index != NOT_FOUND)
00144 {
00145 // Free the entry by marking as NULL
00146 m_pSubscriber[index] = NULL;
00147 status = SUCCESS;
00148 }
00149 return SUCCESS;
00150 }
|
1.3.4 | |
| Copyright © 2000-2005 EventHelix.com Inc. All Rights Reserved. |