Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tmsgcore.h"
Toshihiro Shimizu 890ddd
#include <qlocalserver.h></qlocalserver.h>
Toshihiro Shimizu 890ddd
#include <qlocalsocket.h></qlocalsocket.h>
Toshihiro Shimizu 890ddd
#include <qstringlist></qstringlist>
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qtcpserver></qtcpserver>
Toshihiro Shimizu 890ddd
#include <qtcpsocket></qtcpsocket>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TMsgCore *TMsgCore::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static TMsgCore *theInstance = 0;
Toshihiro Shimizu 890ddd
	if (!theInstance)
Toshihiro Shimizu 890ddd
		theInstance = new TMsgCore();
Toshihiro Shimizu 890ddd
	return theInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TMsgCore::TMsgCore()
Toshihiro Shimizu 890ddd
	: m_tcpServer(0), m_clientSocket(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TMsgCore::OnNewConnection() //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QTcpSocket *socket;
Toshihiro Shimizu 890ddd
	if (m_tcpServer)
Toshihiro Shimizu 890ddd
		socket = m_tcpServer->nextPendingConnection();
Toshihiro Shimizu 890ddd
	assert(socket);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool ret = connect(socket, SIGNAL(readyRead()), SLOT(OnReadyRead()));
Toshihiro Shimizu 890ddd
	ret = ret && connect(socket, SIGNAL(disconnected()), SLOT(OnDisconnected()));
Toshihiro Shimizu 890ddd
	assert(ret);
Toshihiro Shimizu 890ddd
	m_sockets.insert(socket);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TMSG_PORT 10545
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TMsgCore::openConnection() //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//QHostAddress host = (remoteConnection) ? QHostAddress::AnyQHostAddress::LocalHost : ;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_tcpServer != 0 && m_tcpServer->serverAddress() == QHostAddress::Any)
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	if (m_tcpServer != 0)
Toshihiro Shimizu 890ddd
		delete m_tcpServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_tcpServer = new QTcpServer();
Toshihiro Shimizu 890ddd
	bool ret = connect(m_tcpServer, SIGNAL(newConnection()), SLOT(OnNewConnection()));
Toshihiro Shimizu 890ddd
	//bool listen = m_tcpServer->listen(QString("tmsg")+QString::number(QCoreApplication::applicationPid()));
Toshihiro Shimizu 890ddd
	bool listen = m_tcpServer->listen(QHostAddress::Any, TMSG_PORT); //QString("tmsg")+QString::number(QCoreApplication::applicationPid()));
Toshihiro Shimizu 890ddd
	if (!listen) {
Toshihiro Shimizu 890ddd
		QString err = m_tcpServer->errorString();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(ret && listen);
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TMsgCore::getConnectionName() //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_tcpServer);
Toshihiro Shimizu 890ddd
	QString serverName = "pippo"; //m_tcpServer->serverName();
Toshihiro Shimizu 890ddd
	return serverName;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TMsgCore::OnDisconnected() //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::set<qtcpsocket *="">::iterator it = m_sockets.begin();</qtcpsocket>
Toshihiro Shimizu 890ddd
	while (it != m_sockets.end()) {
Toshihiro Shimizu 890ddd
		if ((*it)->state() != QTcpSocket::ConnectedState)
Toshihiro Shimizu 890ddd
			m_sockets.erase(it++);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			it++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//if (m_socketIn)
Toshihiro Shimizu 890ddd
	//delete m_socketIn;
Toshihiro Shimizu 890ddd
	//m_socketIn = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TMsgCore::OnReadyRead() //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::set<qtcpsocket *="">::iterator it = m_sockets.begin();</qtcpsocket>
Toshihiro Shimizu 890ddd
	for (; it != m_sockets.end(); it++) //devo scorrerli tutti perche' non so da quale socket viene il messaggio...
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if ((*it)->state() == QTcpSocket::ConnectedState && (*it)->bytesAvailable() > 0)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (it != m_sockets.end()) {
Toshihiro Shimizu 890ddd
		readFromSocket(*it);
Toshihiro Shimizu 890ddd
		OnReadyRead();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TMsgCore::readFromSocket(QTcpSocket *socket) //server side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static char data[1024];
Toshihiro Shimizu 890ddd
	static QString prevMessage;
Toshihiro Shimizu 890ddd
	QString str;
Toshihiro Shimizu 890ddd
	int byteread;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while ((byteread = socket->read(data, 1023)) != 0) {
Toshihiro Shimizu 890ddd
		data[byteread] = '\0';
Toshihiro Shimizu 890ddd
		str += QString(data);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	QString message = prevMessage + str;
Toshihiro Shimizu 890ddd
	prevMessage = QString();
Toshihiro Shimizu 890ddd
	if (message.isEmpty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lastEnd = message.lastIndexOf("#END");
Toshihiro Shimizu 890ddd
	int lastbegin = message.lastIndexOf("#TMSG");
Toshihiro Shimizu 890ddd
	if (lastbegin == -1 && lastEnd == -1) {
Toshihiro Shimizu 890ddd
		prevMessage = message;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	} else if (lastbegin != -1 && lastEnd != -1 && lastEnd < lastbegin) {
Toshihiro Shimizu 890ddd
		prevMessage = message.right(message.size() - lastbegin);
Toshihiro Shimizu 890ddd
		message.chop(lastbegin);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QStringList messages = message.split("#TMSG", QString::SkipEmptyParts);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < messages.size(); i++) {
Toshihiro Shimizu 890ddd
		QString str = messages.at(i).simplified();
Toshihiro Shimizu 890ddd
		str.chop(4); //rimuovo i "#END" alla fine
Toshihiro Shimizu 890ddd
		if (str.startsWith("ERROR"))
Toshihiro Shimizu 890ddd
			DVGui::MsgBox(CRITICAL, str.right(str.size() - 5));
Toshihiro Shimizu 890ddd
		else if (str.startsWith("WARNING"))
Toshihiro Shimizu 890ddd
			DVGui::MsgBox(WARNING, str.right(str.size() - 7));
Toshihiro Shimizu 890ddd
		else if (str.startsWith("INFO"))
Toshihiro Shimizu 890ddd
			DVGui::MsgBox(INFORMATION, str.right(str.size() - 4));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TMsgCore::~TMsgCore()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_tcpServer == 0 && m_clientSocket != 0) //client side
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//m_socketIn->waitForBytesWritten (-1);
Toshihiro Shimizu 890ddd
		//   m_clientSocket->disconnectFromServer();
Toshihiro Shimizu 890ddd
		// m_clientSocket->waitForDisconnected();
Toshihiro Shimizu 890ddd
		delete m_clientSocket;
Toshihiro Shimizu 890ddd
		m_clientSocket = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TMsgCore::send(MsgType type, const QString &message) //client side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (receivers(SIGNAL(sendMessage(int, const QString &))) == 0) {
Toshihiro Shimizu 890ddd
		if (m_clientSocket == 0 || m_clientSocket->state() != QTcpSocket::ConnectedState)
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QString socketMessage = (type == CRITICAL ? "#TMSG ERROR " : (type == WARNING ? "#TMSG WARNING " : "#TMSG INFO ")) + message + " #END\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050000
Toshihiro Shimizu 890ddd
		m_clientSocket->write(socketMessage.toLatin1());
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		m_clientSocket->write(socketMessage.toAscii());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		m_clientSocket->flush();
Toshihiro Shimizu 890ddd
		//m_clientSocket->waitForBytesWritten (1000);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		Q_EMIT sendMessage(type, message);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TMsgCore::connectTo(const QString &address) //client side
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_clientSocket = new QTcpSocket();
Toshihiro Shimizu 890ddd
	m_clientSocket->connectToHost(address == "" ? QHostAddress::LocalHost : QHostAddress(address), TMSG_PORT);
Toshihiro Shimizu 890ddd
	//m_clientSocket->connectToServer (connectionName, QIODevice::ReadWrite | QIODevice::Text);
Toshihiro Shimizu 890ddd
	bool ret = m_clientSocket->waitForConnected(1000);
Toshihiro Shimizu 890ddd
	if (!ret) {
Toshihiro Shimizu 890ddd
		// std::cout << "cannot connect to "<< address.toStdString() << std::endl;
Toshihiro Shimizu 890ddd
		//std::cout << "error "<< m_clientSocket->errorString().toStdString() << std::endl;
Toshihiro Shimizu 890ddd
		int err = m_clientSocket->error();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void DVGui::MsgBox(MsgType type, const QString &text)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TMsgCore::instance()->send(type, text);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void DVGui::error(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	MsgBox(DVGui::CRITICAL, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void DVGui::warning(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	MsgBox(DVGui::WARNING, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void DVGui::info(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	MsgBox(DVGui::INFORMATION, msg);
Toshihiro Shimizu 890ddd
}