src/PosixMQ.h
branchv_0
changeset 3 b71fc3b5e56b
parent 1 291bdd97fcff
child 6 65abb0376a0d
equal deleted inserted replaced
2:1eef3d465863 3:b71fc3b5e56b
    25 namespace in {
    25 namespace in {
    26 namespace posixmq {
    26 namespace posixmq {
    27 
    27 
    28 class PosixMQ {
    28 class PosixMQ {
    29 private:
    29 private:
    30 	size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
    30 	const static size_t MSG_SIZE = 8192; // TODO: configurable/dynamic
    31 	std::string queueName;
    31 	std::string queueName;
    32 	mqd_t handle = -2;
    32 	mqd_t handle = -2;
       
    33 	bool unlinkOnClose = false;
    33 
    34 
    34 	PosixMQ(std::string queueName, mqd_t handle) : queueName(queueName), handle(handle) {
    35 	PosixMQ(std::string queueName, mqd_t handle, bool unlinkOnClose) : queueName(queueName), handle(handle), unlinkOnClose(unlinkOnClose) {
    35 	}
    36 	}
    36 
    37 
    37 public:
    38 public:
    38 
    39 
    39 	virtual ~PosixMQ() {
    40 	virtual ~PosixMQ() {
    40 		if (handle >= 0) mq_close(handle);
    41 		if (handle >= 0) mq_close(handle);
       
    42 		if (unlinkOnClose) mq_unlink(queueName.c_str());
    41 	}
    43 	}
    42 
    44 
    43 	static PosixMQ* open(std::string queueName) {
    45 	static PosixMQ* open(std::string queueName, bool unlinkOnClose = false) {
    44 		mqd_t handle = mq_open(queueName.c_str(), O_RDONLY | O_CREAT);
    46 		mqd_t handle = mq_open(queueName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
    45 		if (handle >= 0) return new PosixMQ(queueName, handle);
    47 		if (handle >= 0) return new PosixMQ(queueName, handle, unlinkOnClose);
    46 		else throw std::logic_error("Unable to open PosixMQ: " + queueName + " error: " + strerror(errno));
    48 		else throw std::logic_error("Unable to open PosixMQ: " + queueName + " error: " + strerror(errno));
       
    49 	}
       
    50 
       
    51 	void send(std::string message) {
       
    52 		int result = mq_send(handle, message.c_str(), message.size(), 0);
       
    53 		if (result) throw std::logic_error("Unable to send message to" + queueName + " error: " + strerror(errno));
    47 	}
    54 	}
    48 
    55 
    49 	std::string receive() {
    56 	std::string receive() {
    50 		char buffer[MSG_SIZE + 1];
    57 		char buffer[MSG_SIZE + 1];
    51 		memset(buffer, 0, MSG_SIZE + 1);
    58 		memset(buffer, 0, MSG_SIZE + 1);
    53 
    60 
    54 		if (msgSize >= 0) return std::string(buffer);
    61 		if (msgSize >= 0) return std::string(buffer);
    55 		else throw std::logic_error("Unable to receive PosixMQ message from " + queueName + " error: " + strerror(errno));
    62 		else throw std::logic_error("Unable to receive PosixMQ message from " + queueName + " error: " + strerror(errno));
    56 	}
    63 	}
    57 
    64 
    58 	void unlink() {
       
    59 		mq_unlink(queueName.c_str());
       
    60 	}
       
    61 
       
    62 };
    65 };
    63 
    66 
    64 }
    67 }
    65 }
    68 }
    66 }
    69 }