-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNotificationSender.cpp
More file actions
191 lines (155 loc) · 5.37 KB
/
NotificationSender.cpp
File metadata and controls
191 lines (155 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ===========================================================================
// NotificationSender.cpp
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
#include <stdexcept>
class SystemOperator
{
private:
std::string m_name;
std::string m_pager;
std::string m_cellPhone;
std::string m_email;
public:
SystemOperator() : m_name{}, m_pager{}, m_cellPhone{}, m_email{} {}
SystemOperator(const std::string& name, const std::string& email, const std::string& cellPhone, const std::string& pager)
: m_name{ name }, m_pager{ pager }, m_cellPhone{ cellPhone }, m_email{ email } {}
void setName(const std::string& name) { m_name = name; }
void setPager(const std::string& pager) { m_pager = pager; }
void setCellPhone(const std::string& cellPhone) { m_cellPhone = cellPhone; }
void setEmail(const std::string& email) { m_email = email; }
const std::string getName() { return m_name; }
const std::string getPager() { return m_pager; }
const std::string getCellPhone() { return m_cellPhone; }
const std::string getEmail() { return m_email; }
};
class NotificationSenderBase
{
protected:
SystemOperator m_operator;
public:
NotificationSenderBase(const SystemOperator& systemOperator)
: m_operator{ systemOperator } {}
protected:
virtual std::string getNotificationHeader() = 0;
virtual std::string getNotificationTrailer() = 0;
virtual std::string getProtocolDetails() = 0;
virtual void sendText(const std::string&) = 0;
public:
void notify(const std::string& text)
{
const std::string& header{ getNotificationHeader() };
sendText(header);
sendText(text);
const std::string& trailer{ getNotificationTrailer() };
sendText(trailer);
const std::string& tmp{ getProtocolDetails() };
std::string details{
tmp +
std::string(" - Length of message: ") +
std::to_string(text.length())
};
sendText(details);
}
};
class MailNotificationSender : public NotificationSenderBase
{
public:
MailNotificationSender(SystemOperator systemOperator)
: NotificationSenderBase{ systemOperator } {}
protected:
virtual std::string getNotificationHeader() override
{
// SMTP: emtpy line between header and body
std::string header{ std::string("EMail Header: Using SMPT Protocol.") };
return header + std::string("\n");
}
virtual std::string getNotificationTrailer() override
{
// SMTP: end of body: single line just containing a '.'
std::string trailer{ "\n.\n" };
return trailer + std::string("<End-of-EMail>") + std::string("\n");
}
virtual std::string getProtocolDetails() override
{
std::string sender{ m_operator.getEmail() };
return std::string("Server: FROM <") + sender + std::string(">");
}
virtual void sendText(const std::string& text) override
{
std::cout << text;
}
};
class SmsNotificationSender : public NotificationSenderBase
{
public:
SmsNotificationSender(SystemOperator systemOperator)
: NotificationSenderBase(systemOperator)
{}
protected:
virtual std::string getNotificationHeader() override
{
// SMS: emtpy line between header and body
std::string header{ std::string("SMS Header: Using Short Message Service Protocol.") };
return header + std::string("\n");
}
virtual std::string getNotificationTrailer() override
{
// SMS: insert single line for better readability
std::string trailer{ "\n" };
return trailer + std::string("<End-of-SMS>") + std::string("\n");
}
virtual std::string getProtocolDetails() override
{
const std::string& sender{ m_operator.getCellPhone() };
return std::string("Server: Originator Address (OA) <") + sender + std::string(">");
}
virtual void sendText(const std::string& text) override
{
if (text.length() > 160) {
throw std::invalid_argument("length of message too long");
}
// content of sms should be converted to 7-bit characters from the GSM character set
std::cout << text;
}
};
class Message
{
private:
std::shared_ptr<NotificationSenderBase> m_sender;
public:
void setSender(std::shared_ptr<NotificationSenderBase> sender) {
m_sender = sender;
}
void postMessage(const std::string& text)
{
m_sender->notify(text);
}
};
void test_notification_sender()
{
SystemOperator systemOperator
{
"Super Operator",
"system@operator.com",
"0049151123456789",
"465565456"
};
std::shared_ptr<NotificationSenderBase> sender1{
std::make_shared<MailNotificationSender>(systemOperator)
};
std::shared_ptr<NotificationSenderBase> sender2{
std::make_shared<SmsNotificationSender>(systemOperator)
};
Message message{};
message.setSender(sender1);
message.postMessage("This is a message being sent as Email ...");
std::cout << std::endl << std::endl;
message.setSender(sender2);
message.postMessage("...and this message is sent as SMS");
}
// ===========================================================================
// End-of-File
// ===========================================================================