Adding start of new Mail Service.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7723 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2014-12-02 13:22:48 +00:00
parent 6d32524837
commit 6588eafb99
14 changed files with 1321 additions and 1 deletions

View file

@ -0,0 +1,254 @@
/*
* libretroshare/src/services directmailservice.cc
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "services/mail/directmailservice.h"
#include "util/rsdebug.h"
//#define MSG_DEBUG 1
const int directmailzone = 54319;
using namespace Rs::Mail;
DirectMailService::DirectMailService(p3ServiceControl *sc)
:MailTransport(DEFAULT_MAX_MESSAGE_SIZE, RS_SERVICE_TYPE_DIRECT_MAIL),
p3Service(), mServiceCtrl(sc), mMsgMtx("DirectMailService")
{
mSerialiser = new RsMailTransportSerialiser(RS_SERVICE_TYPE_DIRECT_MAIL);
addSerialType(mSerialiser);
}
const std::string MSGDIRECT_APP_NAME = "msgdirect";
const uint16_t MSGDIRECT_APP_MAJOR_VERSION = 1;
const uint16_t MSGDIRECT_APP_MINOR_VERSION = 0;
const uint16_t MSGDIRECT_MIN_MAJOR_VERSION = 1;
const uint16_t MSGDIRECT_MIN_MINOR_VERSION = 0;
RsServiceInfo DirectMailService::getServiceInfo()
{
return RsServiceInfo(RS_SERVICE_TYPE_DIRECT_MAIL,
MSGDIRECT_APP_NAME,
MSGDIRECT_APP_MAJOR_VERSION,
MSGDIRECT_APP_MINOR_VERSION,
MSGDIRECT_MIN_MAJOR_VERSION,
MSGDIRECT_MIN_MINOR_VERSION);
}
bool DirectMailService::CanSend(const RsTlvMailAddress &addr)
{
/* we can send if MessageAddress is a peerID,
* and the peer ID is a friend.
*/
if (addr.mAddressType == ADDRESS_TYPE_PEER_ID)
{
return true;
}
return false;
}
bool DirectMailService::sendMail(RsMailChunkItem *item)
{
// set the right serviceId.
item->setPacketService(RS_SERVICE_TYPE_DIRECT_MAIL);
if (!attemptToSend(item))
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
mOutgoingMsgs.push_back(item);
}
return true;
}
RsMailChunkItem *DirectMailService::recvMail()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingMsgs.empty())
{
RsMailChunkItem *item = mIncomingMsgs.front();
mIncomingMsgs.pop_front();
return item;
}
return NULL;
}
RsPeerId convertAddressToPeerId(RsMailAckItem *item)
{
RsPeerId id;
return id;
}
bool DirectMailService::sendMailAck(RsMailAckItem *item)
{
// set the right serviceId.
item->setPacketService(RS_SERVICE_TYPE_DIRECT_MAIL);
item->PeerId(convertAddressToPeerId(item));
if (!attemptToSend(item))
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
mOutgoingMsgs.push_back(item);
}
return true;
}
RsMailAckItem *DirectMailService::recvMailAck()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingAckMsgs.empty())
{
RsMailAckItem *item = mIncomingAckMsgs.front();
mIncomingMsgs.pop_front();
return item;
}
return NULL;
}
int DirectMailService::tick()
{
rslog(RSL_DEBUG_BASIC, directmailzone,
"DirectMailService::tick()");
incomingMsgs();
return 0;
}
int DirectMailService::status()
{
rslog(RSL_DEBUG_BASIC, directmailzone,
"DirectMailService::status()");
return 1;
}
void DirectMailService::statusChange(const std::list<pqiServicePeer> &plist)
{
/* should do it properly! */
/* only do this when a new peer is connected */
bool newPeers = false;
std::list<pqiServicePeer>::const_iterator it;
for(it = plist.begin(); it != plist.end(); it++)
{
if (it->actions & RS_SERVICE_PEER_CONNECTED)
{
newPeers = true;
}
}
if (newPeers)
{
checkOutgoingMessages();
}
}
int DirectMailService::incomingMsgs()
{
RsItem *item;
int i = 0;
while((item = recvItem()) != NULL)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
RsMailChunkItem *mi = dynamic_cast<RsMailChunkItem *>(item);
RsMailAckItem *mai = dynamic_cast<RsMailAckItem *>(item);
if (mi)
{
mIncomingMsgs.push_back(mi);
}
else if (mai)
{
mIncomingAckMsgs.push_back(mai);
}
else
{
std::cerr << "DirectMailService::incomingMsgs() Unknown incoming Msg";
std::cerr << std::endl;
delete item;
}
++i;
}
return i;
}
// This is a Local copy of an Outgoing message - either SentBox or Draft.
bool DirectMailService::attemptToSend(RsItem *mi)
{
RsPeerId pid = mi->PeerId();
if (mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid))
{
sendItem(mi);
return true;
}
return false;
}
void DirectMailService::checkOutgoingMessages()
{
/* iterate through the outgoing queue and attempt to send.
*/
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
std::list<RsItem *>::iterator mit;
for(mit = mOutgoingMsgs.begin(); mit != mOutgoingMsgs.end(); mit++)
{
if (attemptToSend(*mit))
{
/* actually sent the message */
mit = mOutgoingMsgs.erase(mit);
}
else
{
mit++;
}
}
}
bool DirectMailService::haveIncomingMail()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingMsgs.empty())
{
return true;
}
if (!mIncomingAckMsgs.empty())
{
return true;
}
return false;
}

View file

@ -0,0 +1,92 @@
/*
* libretroshare/src/services/mail directmailservice.h
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef DIRECT_MAIL_SERVICE_HEADER
#define DIRECT_MAIL_SERVICE_HEADER
#include "services/mail/mailtransport.h"
#include "services/p3service.h"
#include "pqi/pqiservicemonitor.h"
#include "util/rsthreads.h"
namespace Rs
{
namespace Mail
{
class DirectMailService: public MailTransport, public p3Service, public pqiServiceMonitor
{
public:
DirectMailService(p3ServiceControl *sc);
virtual RsServiceInfo getServiceInfo();
/******* MailTransport Interface ********************************************************/
virtual bool CanSend(const RsTlvMailAddress &addr);
virtual bool haveIncomingMail();
virtual bool sendMail(RsMailChunkItem *msg);
virtual RsMailChunkItem *recvMail();
virtual bool sendMailAck(RsMailAckItem *ack);
virtual RsMailAckItem *recvMailAck();
/******* MsgTransport Interface ********************************************************/
int tick();
int status();
/*** Overloaded from pqiMonitor ***/
virtual void statusChange(const std::list<pqiServicePeer> &plist);
/*** Overloaded from pqiMonitor ***/
private:
int incomingMsgs();
void checkOutgoingMessages();
bool attemptToSend(RsItem *item);
p3ServiceControl *mServiceCtrl;
/* Mutex Required for stuff below */
RsMutex mMsgMtx;
RsMailTransportSerialiser *mSerialiser ;
/* outgoing messages */
std::list<RsItem *> mOutgoingMsgs;
/* incoming msgs */
std::list<RsMailChunkItem *> mIncomingMsgs;
std::list<RsMailAckItem *> mIncomingAckMsgs;
};
} // namspace Msg
} // namspace Rs
#endif // DIRECT_MESSAGE_SERVICE_HEADER

View file

@ -0,0 +1,56 @@
/*
* libretroshare/src/services/mail mailtransport.cc
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "services/mail/mailtransport.h"
using namespace Rs::Mail;
MailTransport::MailTransport(uint32_t max_size, uint16_t transport_type)
:mEnabled(true), mMaxSize(max_size), mTransportType(transport_type)
{
return;
}
bool MailTransport::isEnabled()
{
return mEnabled;
}
void MailTransport::setEnabled(bool enabled)
{
mEnabled = enabled;
}
uint32_t MailTransport::getMaxSize()
{
return mMaxSize;
}
uint16_t MailTransport::getTransportType()
{
return mTransportType;
}

View file

@ -0,0 +1,77 @@
#pragma once
#ifndef RS_MAIL_TRANSPORT_HEADER
#define RS_MAIL_TRANSPORT_HEADER
/*
* libretroshare/src/services/mail mailtransport.h
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rsmailtransportitems.h"
namespace Rs
{
namespace Mail
{
const uint32_t DEFAULT_MAX_MESSAGE_SIZE = (64 * 1024);
// To decide how this will work!
const uint32_t ADDRESS_TYPE_PEER_ID = 0x001;
const uint32_t ADDRESS_TYPE_GXS_ID = 0x002;
const uint32_t ADDRESS_TYPE_EMAIL_ID = 0x003;
class MailTransport
{
public:
MailTransport(uint32_t max_size, uint16_t transport_type);
virtual ~MailTransport();
virtual bool CanSend(const RsTlvMailAddress &addr) = 0;
virtual bool haveIncomingMail() = 0;
virtual bool sendMail(RsMailChunkItem *msg) = 0;
virtual RsMailChunkItem *recvMail() = 0;
virtual bool sendMailAck(RsMailAckItem *ack) = 0;
virtual RsMailAckItem *recvMailAck() = 0;
virtual bool isEnabled();
virtual void setEnabled(bool enabled);
uint32_t getMaxSize();
uint16_t getTransportType();
private:
bool mEnabled;
uint32_t mMaxSize;
uint16_t mTransportType;
};
} // namespace Mail
} // namespace Rs
#endif