Click here to Skip to main content
15,896,453 members
Articles / Desktop Programming / MFC

SAFMQ Store and Forward Message Queue

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
16 Jan 20064 min read 84.2K   1.8K   33  
An OpenSource cross-compilable/cross-platform message queue server like MSMQ or MQSeries.
/*
 Copyright 2005 Matthew J. Battey

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software distributed
	under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
	CONDITIONS OF ANY KIND, either express or implied. See the License for the
	specific language governing permissions and limitations under the License.




This software implements a platform independent Store and Forward Message Queue.
*/
#include "main.h"
#include "SystemDelivery.h"
#include "lib/MQFactory.h"
#include "lib/MQConnection.h"
#include "url/urldecode.h"
#include "comdefs.h"
#include <iostream>

using namespace std;
using namespace safmq;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

std::queue<SystemMessage>	SystemDelivery::que;
Mutex						SystemDelivery::qmtx;
Signal						SystemDelivery::qsignal;

SystemDelivery::SystemDelivery()
{
	// TODO: fetch the login id's
	ttl_user = system_user;
	ttl_passwd = "";
}

SystemDelivery::~SystemDelivery()
{

}

void SystemDelivery::Enqueue(SystemMessage &msg)
{
	MutexLock	lock(&qmtx);
	que.push(msg);
}

void* SystemDelivery::run()
{
	while (!m_bStop) {
		qsignal.WaitFor(500);
		if (!m_bStop) 
			sendMessages();
	}
	return 0;
}

void SystemDelivery::stop()
{
	Thread::stop();
	qsignal.Set();
}

void SystemDelivery::sendMessages()
{
	qmtx.Lock();
	bool	empty = que.empty();
	qmtx.Unlock();

	std::string cert = pcfg->getParam(SSL_CLIENT_CERT_PARAM,"");
	std::string key = pcfg->getParam(SSL_CLIENT_KEY_PARAM,"");

	SystemMessage	msg;
	while (!empty) {
		qmtx.Lock();
		msg = que.front();
		que.pop();
		empty = que.empty();
		qmtx.Unlock();
		try {
			SAFMQ_WSTRING		qname;
			msg.getResponseQueueName(qname);

			try {
				URL::urldecode	url(qname);
				SAFMQ_WSTRING	resource = url.getResource();

				SAFMQ_WSTRING	url_nuevo;


				if (url.getProtocol().length())
					url_nuevo = url.getProtocol() + ":";
				url_nuevo += "//";
					
				url_nuevo += url.getDestination();
				if (url.getPort().length())
					url_nuevo += ":" + url.getPort();

				cout << "System Message Delivery:" << msg.getLabel() << endl;
				cout << "Destination: " << url_nuevo << endl;
				cout << "ttl_user:" << ttl_user << endl;
				cout << "ttl_passwd: " << ttl_passwd << endl;

				SystemConnection			*con = (SystemConnection*)MQFactory::BuildConnection(url_nuevo, ttl_user.c_str(), ttl_passwd.c_str(),cert,key);
				MQConnection::QueueHandle	que;
				
				if (resource.length() && resource[0] == '/')
					resource.erase(0,1);

				if (resource.length()) {
					ErrorCode ec = con->OpenQueue(resource,que);
					if (ec == EC_NOERROR) {
						ec = con->Enqueue(que,msg);
					}
					// TODO: Log the error or something.
					if (ec != EC_NOERROR)
						cout << "SystemDelivery error code: " << EC_Decode(ec) << endl;
				}
		
				delete con;
			} catch (URL::urlexception& e) {
				cout << "Error urlexception: " << e.what() << endl;
			}
		} catch (ErrorCode c) {
			cout << "Error Code Thrown: " << c << endl;
		} catch (MQFactoryException e) {
			cout << "MQFactorException: " << e.what() << endl;
		} catch (tcpsocket::SocketException e) {
			cout << "tcpsocket::SocketException: " << e.what() << endl;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
A programmer for 20 years and professionaly employed for 12, I am currently Cheif Engineer for Pharmacy Chare Professionals, Inc., located in Omaha, NE.

My experience is in the area of OO Design, Application, and programming, technical team leadership, RDBMS applications, ISAM applications, Image Processing, Mathematical image generation, Client-Server business applications, eBusiness applications, XML & EDI B2B communications, Java application development, C/C++ application development, CFML/ASP/VB development, on systems like Win2K/NT/98/95, Linux, Irix, Solaris, and MacOS.

Comments and Discussions