I have the following pseudocode as part of a C++ project:
class BasicSender
{
public:
virtual void confirmation(int id) = 0;
int sendMessage(string x)
{
Message x = createMessageFromString(x);
if(! canSendMessage(m))
{
MessageStore::storeMessageForLater(this, m); // just store it
}
// *** HERE ***
return m.messageId;
}
};
void threadFunction()
{
while(1)
{
while(MessageStore::hasMessagesToBeSent())
{
StoredMessage m = MessageStore.getNextUnsentMessage();
if(m.basicSender.sendStoredMessage(m.message))
{
m.confirmation(m.message.messageId);
}
}
}
}
and the usage of it:
class ConcreteSender : public BasicSender
{
virtual void confirmation(int id)
{
cout << "Yippe " << id << " is sent";
}
};
int main() {
ConcreteSender a;
int ID = a.sendMessage("test");
... other stuff
}
And what I try to achieve:
is it even remotely possible to call the confirmation
for succesfully delivered messages method right after sendMessage
finished (ie: it returned to the caller and the caller (in main
) has the ID
value). If I put the method call at the place *** HERE ***
the virtual method will get called before sendMessage
finished and the user will be confused that the received a confirmation for a message ID he had no clue about. Yes, I have methods which can check if a message was sent or not, and yes, I can create a thread which from time to time pulls the MessageStore
to see if the message was delivered or not. But I am more specifically interested if it is possible to somehow chain in the two function calls, in case of success: confirmation
called right after sendMessage
returns. If yes, how? (Please note, I cannot modify the signature of the functions, third party library :( )