[Jderobot-admin] jderobot-r1191 - in trunk/src/stable: components components/namingService interfaces/slice/jderobot

rocapal en jderobot.org rocapal en jderobot.org
Lun Mar 17 09:21:05 CET 2014


Author: rocapal
Date: 2014-03-17 09:21:05 +0100 (Mon, 17 Mar 2014)
New Revision: 1191

Added:
   trunk/src/stable/components/namingService/
   trunk/src/stable/components/namingService/CMakeLists.txt
   trunk/src/stable/components/namingService/NamingServiceJdeRobot.cpp
   trunk/src/stable/components/namingService/NamingServiceJdeRobot.h
   trunk/src/stable/components/namingService/main.cpp
   trunk/src/stable/components/namingService/namingService.cfg
   trunk/src/stable/interfaces/slice/jderobot/namingService.ice
Log:
#195 first version of naming service


Added: trunk/src/stable/components/namingService/CMakeLists.txt
===================================================================
--- trunk/src/stable/components/namingService/CMakeLists.txt	                        (rev 0)
+++ trunk/src/stable/components/namingService/CMakeLists.txt	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,21 @@
+
+SET( SOURCE_FILES main.cpp NamingServiceJdeRobot.h NamingServiceJdeRobot.cpp)
+
+add_definitions(-DGLADE_DIR="${gladedir}")
+
+include_directories(
+    ${INTERFACES_CPP_DIR}
+    ${LIBS_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}    
+)
+
+add_executable (namingService ${SOURCE_FILES})
+
+TARGET_LINK_LIBRARIES(namingService
+    ${CMAKE_THREAD_LIBS_INIT}
+    ${gsl_LIBRARIES}    
+    ${ZeroCIce_LIBRARIES}
+    ${Boost_LIBRARIES}    
+    JderobotInterfaces
+    logger
+)

Added: trunk/src/stable/components/namingService/NamingServiceJdeRobot.cpp
===================================================================
--- trunk/src/stable/components/namingService/NamingServiceJdeRobot.cpp	                        (rev 0)
+++ trunk/src/stable/components/namingService/NamingServiceJdeRobot.cpp	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,192 @@
+/*
+ *  Copyright (C) 2014 JdeRobot developers
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ *  Authors : Roberto Calvo Palomino <rocapal [at] gsyc [dot] urjc [dot] es>
+ *
+ */
+
+#include "NamingServiceJdeRobot.h"
+
+namespace NamingService
+{
+
+const std::string serializeFile = "TableNamingService.jde";
+
+NamingServiceJdeRobot::NamingServiceJdeRobot(std::string& propertyPrefix): mNodes(new jderobot::NodeContainer())
+{
+	load();
+}
+
+void NamingServiceJdeRobot::bind (const jderobot::NamingNodePtr& node, const Ice::Current&)
+{
+	if (exists(node))
+	{
+		jderobot::Logger::getInstance()->warning("Name already bind " + node->name + " replace it ...");
+		remove(node);
+	}
+
+	mNodes->nodes.push_back(node);
+
+	jderobot::Logger::getInstance()->info("Bind:: " + node->name + " - " + node->interfaceName + " - " + node->protocol + " - " +
+			node->ip + ":" + boost::lexical_cast<std::string>(node->port) );
+
+	save();
+}
+
+void NamingServiceJdeRobot::unbind (const jderobot::NamingNodePtr& node, const Ice::Current& )
+{
+	if (! exists(node))
+	{
+		jderobot::Logger::getInstance()->error("Name not exist to unbind " + node->name);
+		jderobot::NameNotExistException ex ("Name not exist to unbind " + node->name);
+		throw ex;
+	}
+
+	if (remove(node))
+	{
+		jderobot::Logger::getInstance()->info("unbind:: " + node->name + " - " + node->interfaceName + " - " + node->protocol + " - " +
+				node->ip + ":" + boost::lexical_cast<std::string>(node->port) );
+
+		save();
+	}
+	else
+		jderobot::Logger::getInstance()->error("unbind:: there was an error with unbind node" );
+
+}
+
+jderobot::NodeContainerPtr NamingServiceJdeRobot::resolveByName (const std::string& name, const Ice::Current&)
+{
+
+	jderobot::NodeContainerPtr result = new jderobot::NodeContainer();
+
+	for (std::vector<jderobot::NamingNodePtr>::iterator it = mNodes->nodes.begin(); it != mNodes->nodes.end(); it++)
+	{
+		if (it->_ptr->name.compare(name)==0)
+			result->nodes.push_back(mNodes->nodes[it - mNodes->nodes.begin()]);
+	}
+
+	if (result->nodes.size() == 0)
+	{
+		jderobot::Logger::getInstance()->error("Name not exist to resolveByName " + name);
+		jderobot::NameNotExistException ex ("Name not exist to resolveByName " + name);
+		throw ex;
+	}
+
+	return result;
+}
+
+jderobot::NodeContainerPtr NamingServiceJdeRobot::resolveByInterface (const std::string& interface, const Ice::Current&)
+{
+	jderobot::NodeContainerPtr result = new jderobot::NodeContainer();
+
+	for (std::vector<jderobot::NamingNodePtr>::iterator it = mNodes->nodes.begin(); it != mNodes->nodes.end(); it++)
+	{
+		if (it->_ptr->interfaceName.compare(interface) == 0)
+			result->nodes.push_back(mNodes->nodes[it - mNodes->nodes.begin()]);
+	}
+
+	if (result->nodes.size() == 0)
+	{
+		jderobot::Logger::getInstance()->error("Interface not exist to resolveByInterface " + interface);
+		jderobot::InterfaceNotExistException ex ("Interface not exist to resolveByInterface " + interface);
+		throw ex;
+	}
+
+	return result;
+}
+
+bool NamingServiceJdeRobot::exists (const jderobot::NamingNodePtr& node)
+{
+	for (std::vector<jderobot::NamingNodePtr>::iterator it = mNodes->nodes.begin(); it != mNodes->nodes.end(); it++)
+	{
+		if (node->name.compare(it->_ptr->name)==0)
+			return true;
+	}
+	return false;
+}
+
+bool NamingServiceJdeRobot::remove (const jderobot::NamingNodePtr& node)
+{
+	for (std::vector<jderobot::NamingNodePtr>::iterator it = mNodes->nodes.begin(); it != mNodes->nodes.end(); it++)
+	{
+			if (node->name.compare(it->_ptr->name)==0)
+		{
+			mNodes->nodes.erase(it);
+			return true;
+		}
+	}
+	return false;
+}
+
+bool NamingServiceJdeRobot::save ()
+{
+	std::ofstream out(serializeFile.c_str());
+
+	for (std::vector<jderobot::NamingNodePtr>::iterator it = mNodes->nodes.begin(); it != mNodes->nodes.end(); it++)
+	{
+		std::stringstream ss;
+		ss << it->_ptr->name << "&" << it->_ptr->interfaceName << "&" << it->_ptr->ip << "&" << it->_ptr->port << "&" << it->_ptr->protocol << std::endl;
+		out.write(ss.str().c_str(), ss.str().length());
+	}
+	out.close();
+	return true;
+}
+
+bool NamingServiceJdeRobot::load ()
+{
+	std::ifstream in(serializeFile.c_str());
+
+	jderobot::Logger::getInstance()->info ("Trying to get persistent data from " + serializeFile);
+	std::string line;
+	while (std::getline(in, line))
+	{
+		std::vector<std::string> elems;
+		split(line, '&', elems);
+
+		if (elems.size() != 5)
+		{
+			jderobot::Logger::getInstance()->warning("Restore error line (not process): " + line);
+			continue;
+		}
+
+		jderobot::NamingNodePtr node = new jderobot::NamingNode();
+		node->name = elems[0];
+		node->interfaceName = elems[1];
+		node->ip = elems[2];
+		node->port = boost::lexical_cast<int>(elems[3]);
+		node->protocol = elems[4];
+
+		mNodes->nodes.push_back(node);
+
+		jderobot::Logger::getInstance()->info("Restore node: " + node->name + " - " + node->interfaceName + " - " + node->protocol + " - " +
+					node->ip + ":" + boost::lexical_cast<std::string>(node->port) );
+
+	}
+
+}
+
+std::vector<std::string>& NamingServiceJdeRobot::split(const std::string &s, char delim, std::vector<std::string> &elems) {
+
+    std::stringstream ss(s);
+    std::string item;
+    while (std::getline(ss, item, delim)) {
+        elems.push_back(item);
+    }
+    return elems;
+}
+
+
+}

Added: trunk/src/stable/components/namingService/NamingServiceJdeRobot.h
===================================================================
--- trunk/src/stable/components/namingService/NamingServiceJdeRobot.h	                        (rev 0)
+++ trunk/src/stable/components/namingService/NamingServiceJdeRobot.h	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,99 @@
+/*
+ *  Copyright (C) 2014 JdeRobot developers
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ *  Authors : Roberto Calvo Palomino <rocapal [at] gsyc [dot] urjc [dot] es>
+ *
+ */
+
+#include <jderobot/namingService.h>
+#include <log/Logger.h>
+#include <boost/lexical_cast.hpp>
+#include <fstream>
+#include <vector>
+#include <string>
+
+namespace NamingService
+{
+
+	/**
+	 * NamingServiceJdeRobot class implements a simple and efficient naming service
+	 * for components. Allow (de)serializer of the naming table.
+	 */
+
+	class NamingServiceJdeRobot: virtual public jderobot::NamingService
+	{
+
+	public:
+
+		/**
+		* \brief Constructor
+		*
+		* @param propertyPrefix Prefix to get specific config of ICE file
+		 */
+		NamingServiceJdeRobot(std::string& propertyPrefix);
+
+		/**
+		 * \brief Bind the new node in the table. If already exists a node with the same name,
+		 * it's replace by the new.
+		 *
+		 * @param node The node to bind
+		 * @param current the Ice data
+		 *
+		 */
+		virtual void bind (const jderobot::NamingNodePtr& node, const Ice::Current& current);
+
+		/**
+		 * \brief Unbind a node of the table.
+		 *
+		 * @param node The node to unbind
+		 * @param current the Ice data
+		 *
+		 */
+		virtual void unbind (const jderobot::NamingNodePtr& node, const Ice::Current& current);
+
+		/**
+		 * \brief Resolve a petition given a name of component
+		 *
+		 * @param name The name of component to search
+		 * @param current the Ice data
+		 *
+		 */
+		virtual jderobot::NodeContainerPtr resolveByName (const std::string& name, const Ice::Current& current);
+
+		/**
+		 * \brief Resolve a petition given an interface
+		 *
+		 * @param name The name of interface to search
+		 * @param current the Ice data
+		 *
+		 */
+		virtual jderobot::NodeContainerPtr resolveByInterface (const std::string& interface, const Ice::Current& current);
+
+	private:
+
+		bool remove (const jderobot::NamingNodePtr& node);
+		bool exists (const jderobot::NamingNodePtr& node);
+
+		bool save();
+		bool load();
+		std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems);
+
+
+
+		jderobot::NodeContainerPtr mNodes;
+	};
+
+}

Added: trunk/src/stable/components/namingService/main.cpp
===================================================================
--- trunk/src/stable/components/namingService/main.cpp	                        (rev 0)
+++ trunk/src/stable/components/namingService/main.cpp	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,141 @@
+/*
+ *  Copyright (C) 2014 JdeRobot developers
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ *  Authors : Roberto Calvo Palomino <rocapal [at] gsyc [dot] urjc [dot] es>
+ *
+ */
+
+#include <iostream>
+#include <Ice/Ice.h>
+#include <IceUtil/IceUtil.h>
+
+#include "pthread.h"
+#include <signal.h>
+#include <log/Logger.h>
+
+#include "NamingServiceJdeRobot.h"
+
+#include <pthread.h>
+
+int status,i;
+Ice::CommunicatorPtr ic;
+int n_components=0;
+pthread_t thread;
+pthread_attr_t attr;
+Ice::PropertiesPtr prop;
+Ice::ObjectAdapterPtr adapter;
+
+NamingService::NamingServiceJdeRobot* namingservice_prx;
+
+void exitComponent(int s)
+{
+	ic->shutdown();
+}
+
+int main(int argc, char** argv){
+
+	struct sigaction sigIntHandler;
+
+	sigIntHandler.sa_handler = exitComponent;
+	sigemptyset(&sigIntHandler.sa_mask);
+	sigIntHandler.sa_flags = 0;
+
+	sigaction(SIGINT, &sigIntHandler, NULL);
+
+
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+	try{
+		ic = Ice::initialize(argc,argv);
+		prop = ic->getProperties();
+	}catch (const Ice::Exception& ex) {
+		std::cerr << ex << std::endl;
+		return 1;
+	}
+	catch (const char* msg) {
+		jderobot::Logger::getInstance()->error(msg);
+		return 1;
+	}
+
+
+	try{
+
+		std::string prefixComponent("NamingService");
+
+		// Analyze LOG section
+
+		std::string logFile = prop->getProperty(prefixComponent + ".Log.File.Name");
+		if (logFile.size()==0)
+			jderobot::Logger::getInstance()->warning("You didn't set log file!");
+		else
+			jderobot::Logger::getInstance()->setFileLog(logFile);
+
+		std::string logLevel = prop->getProperty(prefixComponent + ".Log.File.Level");
+		if (logLevel.size()==0)
+			jderobot::Logger::getInstance()->warning("You didn't set *.Log.File.Level key!");
+		else
+			jderobot::Logger::getInstance()->setFileLevel(jderobot::Levels(boost::lexical_cast<int>(logLevel)));
+
+		std::string screenLevel = prop->getProperty(prefixComponent + ".Log.Screen.Level");
+		if (screenLevel.size()==0)
+			jderobot::Logger::getInstance()->warning("You didn't set *.Log.Screen.Level key!");
+		else
+			jderobot::Logger::getInstance()->setScreenLevel(jderobot::Levels(boost::lexical_cast<int>(screenLevel)));
+
+		jderobot::Logger::getInstance()->info("Logger:: screenLevel=" + screenLevel + " logLevel=" + logLevel + " LogFile=" + logFile);
+
+
+
+
+		// Analyze EndPoint
+		std::string Endpoints = prop->getProperty(prefixComponent + ".Endpoints");
+		adapter =ic->createObjectAdapterWithEndpoints(prefixComponent, Endpoints);
+
+		int communicator_active =prop->getPropertyAsIntWithDefault(prefixComponent + ".Active",0);
+		if (communicator_active){
+
+			std::string Name = prop->getProperty(prefixComponent + ".Name");
+			jderobot::Logger::getInstance()->info("Creating Communicator: " + Name + " (" + Endpoints + ")");
+			std::string objPrefix(prefixComponent);
+			namingservice_prx = new NamingService::NamingServiceJdeRobot (objPrefix);
+
+			adapter->add(namingservice_prx, ic->stringToIdentity(Name));
+		}
+		else
+		{
+			jderobot::Logger::getInstance()->warning("No NamingService Interface declared!");
+			exit(-1);
+		}
+
+		adapter->activate();
+
+		ic->waitForShutdown();
+		adapter->destroy();
+
+	}catch (const Ice::Exception& ex) {
+		std::cerr << ex << std::endl;
+		return 1;
+	}
+	catch (const char* msg) {
+		std::cerr << msg << std::endl;
+		return 1;
+	}
+
+	jderobot::Logger::getInstance()->info("Component finished correctly!");
+
+	return 0;
+
+}

Added: trunk/src/stable/components/namingService/namingService.cfg
===================================================================
--- trunk/src/stable/components/namingService/namingService.cfg	                        (rev 0)
+++ trunk/src/stable/components/namingService/namingService.cfg	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,13 @@
+
+NamingService.Endpoints=default -h 0.0.0.0 -p 10000
+
+# Communicator Interface
+NamingService.Active=1
+NamingService.Name=NamingServiceJdeRobot
+
+# Log
+# Levels: 0(DEBUG), 1(INFO), 2(WARNING), 3(ERROR)
+
+NamingService.Log.File.Name=./log/namingService.cfg
+NamingService.Log.File.Level=0
+NamingService.Log.Screen.Level=0

Added: trunk/src/stable/interfaces/slice/jderobot/namingService.ice
===================================================================
--- trunk/src/stable/interfaces/slice/jderobot/namingService.ice	                        (rev 0)
+++ trunk/src/stable/interfaces/slice/jderobot/namingService.ice	2014-03-17 08:21:05 UTC (rev 1191)
@@ -0,0 +1,62 @@
+/*
+ *
+ *  Copyright (C) 1997-2014 JDE Developers Team
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/. 
+ *
+ *  Author : Robeto Calvo <rocapal en gsyc.urjc.es>
+ *
+ */
+
+
+#ifndef NAMING_SERVICE_ICE
+#define NAMING_SERVICE_ICE
+
+#include <common.ice>
+
+module jderobot {
+
+	exception NameAlreadyExistException extends JderobotException {};
+	exception NameNotExistException extends JderobotException {};
+	exception InterfaceNotExistException extends JderobotException {};
+	
+	class NamingNode
+	{
+		string name;
+		string interfaceName;
+		string protocol;
+		string ip;
+		int port;
+		
+	};
+	
+	sequence<NamingNode> nodeList;
+
+	class NodeContainer
+	{	
+		nodeList nodes;
+	};
+
+	interface NamingService
+	{
+		void bind(NamingNode node) throws NameAlreadyExistException;
+		void unbind(NamingNode node) throws NameAlreadyExistException, NameNotExistException;
+		
+		idempotent NodeContainer resolveByName (string name) throws NameNotExistException;
+		idempotent NodeContainer resolveByInterface (string interfaceName) throws InterfaceNotExistException;
+	};
+
+}; /*module*/
+
+#endif /*NAMING_SERVICE_ICE*/
\ No newline at end of file



More information about the Jderobot-admin mailing list