151 SERVER_DEFAULT_LATENCY_SEND_MS = 5000,
152 SERVER_DEFAULT_LATENCY_SEND_NO_TRIES = 2,
153 SERVER_DEFAULT_LATENCY_RECEIVE_MS = 5000,
154 SERVER_DEFAULT_LATENCY_RECEIVE_NO_TRIES = 2,
155 SERVER_DEFAULT_LATENCY_DELAY_AFTER = 50,
156 SERVER_DEFAULT_IS_BLOCKING = 0
165 : server_(loader.getServer())
171 void MessageProcessor::init(
int port)
177 OTSettings settings(configFolderPath);
180 if (!settings.Load()) {
184 OTSocket::Defaults socketDefaults(
185 SERVER_DEFAULT_LATENCY_SEND_MS, SERVER_DEFAULT_LATENCY_SEND_NO_TRIES,
186 SERVER_DEFAULT_LATENCY_RECEIVE_MS,
187 SERVER_DEFAULT_LATENCY_RECEIVE_NO_TRIES,
188 SERVER_DEFAULT_LATENCY_DELAY_AFTER, SERVER_DEFAULT_IS_BLOCKING);
190 if (!socket_.
Init(socketDefaults, &settings)) {
194 if (!settings.Save()) {
207 bindPath.Format(
"%s%d",
"tcp://*:", port);
209 if (!socket_.
Listen(bindPath)) {
263 bool received = socket_.
Receive(messageString);
267 OTLog::Error(
"server main: Received a message, but of 0 "
268 "length or less. Weird. (Skipping it.)\n");
271 std::string strMsg(messageString.
Get());
273 bool shouldDisconnect = processMessage(strMsg, reply);
275 if (reply.length() <= 0 || shouldDisconnect) {
277 0,
"server main: ERROR: Unfortunately, not every "
279 "legible or worthy of a server response. :-) "
286 bool successSending = socket_.
Send(reply.c_str());
288 if (!successSending) {
290 "while trying to send reply "
291 "back to client! \n\n "
292 "MESSAGE:\n%s\n\nREPLY:\n%s\n\n",
293 strMsg.c_str(), reply.c_str());
306 int64_t elapsed =
static_cast<int64_t
>(endTick - startTick);
314 OTLog::Output(0,
"opentxs server is shutting down gracefully.\n");
320 bool MessageProcessor::processMessage(
const std::string& messageString,
323 if (messageString.size() < 1)
return false;
327 ascMessage.
MemSet(messageString.data(), messageString.size());
336 OTLog::vOutput(2,
"Successfully retrieved envelope from message.\n");
339 OTString envelopeContents;
349 if (!envelopeContents.Exists() ||
350 !message.LoadContractFromString(envelopeContents)) {
352 "contents:\n\n%s\n\n",
353 envelopeContents.Get());
357 OTMessage replyMessage;
358 replyMessage.m_strCommand.Format(
"@%s", message.m_strCommand.Get());
360 replyMessage.m_strNymID = message.m_strNymID;
362 replyMessage.m_strServerID = message.m_strServerID;
364 replyMessage.m_bSuccess =
false;
368 ClientConnection client;
370 OTPseudonym nym(message.m_strNymID);
373 message, replyMessage, &client, &nym);
379 if (!processedUserCmd) {
380 OTString s1(message);
382 OTLog::vOutput(0,
"Unable to process user command: %s\n ********** "
383 "REQUEST:\n\n%s\n\n",
384 message.m_strCommand.Get(), s1.Get());
403 replyMessage.m_bSuccess =
false;
407 replyMessage.SaveContract();
409 OTString s2(replyMessage);
417 message.m_strCommand.Get());
427 if (processedUserCmd || nym.Server_PubKeyExists()) {
432 OTEnvelope recipientEnvelope;
435 client.SealMessageForRecipient(replyMessage, recipientEnvelope);
443 OTASCIIArmor ascReply;
444 if (!recipientEnvelope.GetAsciiArmoredData(ascReply)) {
447 "Unable to GetAsciiArmoredData from sealed "
448 "envelope int oOTASCIIArmor object. (No reply envelope will be "
454 bool val = ascReply.Exists() &&
455 ascReply.WriteArmoredString(output,
"ENVELOPE");
457 if (!val || !output.Exists()) {
459 0,
"Unable to WriteArmoredString from "
460 "OTASCIIArmor object into OTString object. (No reply "
461 "envelope will be sent.)\n");
465 reply.assign(output.Get(), output.GetLength());
470 OTString replyString(replyMessage);
472 if (!replyString.Exists()) {
475 "(No reply message will be sent.)\n");
479 OTASCIIArmor ascReply(replyString);
482 ascReply.Exists() && ascReply.WriteArmoredString(output,
"MESSAGE");
484 if (!val || !output.Exists()) {
486 0,
"Unable to WriteArmoredString from "
487 "OTASCIIArmor object into OTString object. (No reply "
488 "message will be sent.)\n");
492 reply.assign(output.Get(), output.GetLength());
static EXPORT void vError(const char *szError,...)
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
EXPORT double getElapsedTimeInMilliSec()
static EXPORT bool GetConfigFilePath(OTString &strConfigFilePath)
bool IsFlaggedForShutdown() const
EXPORT uint32_t GetLength() const
static int32_t GetHeartbeatNoRequests()
EXPORT bool SetAsciiArmoredData(const OTASCIIArmor &theArmoredText, bool bLineBreaks=true)
EXPORT bool Send(const OTASCIIArmor &ascEnvelope)
EXPORT bool Open(const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr)
static EXPORT void Error(const char *szError)
EXPORT bool Init(const Defaults &defaults)
const OTPseudonym & GetServerNym() const
static int32_t GetHeartbeatMsBetweenBeats()
static EXPORT bool SleepMilliseconds(int64_t lMilliseconds)
EXPORT const char * Get() const
EXPORT bool Receive(OTString &strServerReply)
EXPORT MessageProcessor(ServerLoader &loader)
bool ProcessUserCommand(OTMessage &msgIn, OTMessage &msgOut, ClientConnection *connection, OTPseudonym *nym)
EXPORT bool MemSet(const char *mem, uint32_t size)
static EXPORT void vOutput(int32_t nVerbosity, const char *szOutput,...)