Adding a message object.
This commit is contained in:
		@@ -18,6 +18,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "neocomm/message.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <list>
 | 
					#include <list>
 | 
				
			||||||
#include <opendht.h>
 | 
					#include <opendht.h>
 | 
				
			||||||
@@ -30,7 +32,6 @@ namespace neocomm {
 | 
				
			|||||||
struct channel {
 | 
					struct channel {
 | 
				
			||||||
	dht::InfoHash key;  //!< The sha1 hash of the channel name.
 | 
						dht::InfoHash key;  //!< The sha1 hash of the channel name.
 | 
				
			||||||
	std::future<size_t> token;  //!< Token used to leave channel.
 | 
						std::future<size_t> token;  //!< Token used to leave channel.
 | 
				
			||||||
	std::list<std::string> messages;  //!< Messages received by channel.
 | 
						std::list<struct message> messages;  //!< Messages received by channel.
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										31
									
								
								include/neocomm/message.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								include/neocomm/message.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
 | 
				
			||||||
 | 
					 * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 | 
					 * it under the terms of the GNU Affero General Public License as
 | 
				
			||||||
 | 
					 * published by the Free Software Foundation, either version 3 of the
 | 
				
			||||||
 | 
					 * License, or (at your option) any later version.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This program 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 Affero General Public License for more details.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * You should have received a copy of the GNU Affero General Public License
 | 
				
			||||||
 | 
					 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <opendht.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace neocomm {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct message {
 | 
				
			||||||
 | 
						std::string alias;
 | 
				
			||||||
 | 
						std::string msg;
 | 
				
			||||||
 | 
						MSGPACK_DEFINE(alias, msg)
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -34,8 +34,10 @@ public:
 | 
				
			|||||||
	 * @brief Initalize the node.
 | 
						 * @brief Initalize the node.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param port Local port to bind to.
 | 
						 * @param port Local port to bind to.
 | 
				
			||||||
 | 
						 * @param alias Set the alias of the user.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	node(const unsigned short port = 31133);
 | 
						node(const unsigned short port = 31133,
 | 
				
			||||||
 | 
								const std::string &alias = "unknown");
 | 
				
			||||||
	~node();
 | 
						~node();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -74,6 +76,24 @@ public:
 | 
				
			|||||||
	 * @param name Name of the channel.
 | 
						 * @param name Name of the channel.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	void leave_channel(const std::string &name);
 | 
						void leave_channel(const std::string &name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @brief Get the current alias for the user.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @return The current alias of the user.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						inline std::string get_alias() const {
 | 
				
			||||||
 | 
							return alias;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @brief Set the alias of the user.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param alias The new alias of the user.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						inline void set_alias(const std::string &alias) {
 | 
				
			||||||
 | 
							this->alias = alias;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	inline struct channel *get_channel(
 | 
						inline struct channel *get_channel(
 | 
				
			||||||
			const std::string &name) {
 | 
								const std::string &name) {
 | 
				
			||||||
@@ -84,6 +104,7 @@ private:
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		return nullptr;
 | 
							return nullptr;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						std::string alias;
 | 
				
			||||||
	dht::DhtRunner network;
 | 
						dht::DhtRunner network;
 | 
				
			||||||
	std::map<std::string, struct channel> channels;
 | 
						std::map<std::string, struct channel> channels;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,10 +17,12 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "neocomm/node.hpp"
 | 
					#include "neocomm/node.hpp"
 | 
				
			||||||
 | 
					#include "neocomm/message.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace neocomm;
 | 
					using namespace neocomm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
node::node(unsigned short port) {
 | 
					node::node(unsigned short port, const std::string &alias) :
 | 
				
			||||||
 | 
							alias(alias) {
 | 
				
			||||||
	// TODO: see about preserving an identity
 | 
						// TODO: see about preserving an identity
 | 
				
			||||||
	network.run(port, dht::crypto::generateIdentity(), true);
 | 
						network.run(port, dht::crypto::generateIdentity(), true);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -33,9 +35,9 @@ void node::join_channel(const std::string &name) {
 | 
				
			|||||||
	if(get_channel(name))
 | 
						if(get_channel(name))
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	channels[name].key = dht::InfoHash::get(name);
 | 
						channels[name].key = dht::InfoHash::get(name);
 | 
				
			||||||
	channels[name].token = network.listen<std::string>(
 | 
						channels[name].token = network.listen<struct message>(
 | 
				
			||||||
			channels[name].key,
 | 
								channels[name].key,
 | 
				
			||||||
			[&](std::string &&msg) {
 | 
								[&](struct message &&msg) {
 | 
				
			||||||
				channels[name].messages.push_back(msg);
 | 
									channels[name].messages.push_back(msg);
 | 
				
			||||||
				return true;
 | 
									return true;
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user