From a87e24416b6292b378e8e783cb13880a1fcfebb0 Mon Sep 17 00:00:00 2001
From: Ruud Overeem <overeem@astron.nl>
Date: Fri, 20 Jun 2008 16:46:03 +0000
Subject: [PATCH] Bug 1000: Added ClaimManagerTask for communicating with the
 ClaimManager of PVSS. This communication is async so we have to create a
 seperate task to handle the IO. The test dir contains a program that
 demonstrates how to use the CMtask.

Not fully implemented and tested yet!.
---
 .gitattributes                           |   1 +
 MAC/APL/RTDBCommon/src/CM_Protocol.prot  |  86 ++++++++
 MAC/APL/RTDBCommon/src/ClaimMgrTask.cc   | 242 +++++++++++++++++++++++
 MAC/APL/RTDBCommon/src/ClaimMgrTask.h    | 103 ++++++++++
 MAC/APL/RTDBCommon/src/Makefile.am       |  35 +++-
 MAC/APL/RTDBCommon/src/RTDButilities.cc  |  16 +-
 MAC/APL/RTDBCommon/test/Makefile.am      |   6 +-
 MAC/APL/RTDBCommon/test/tClaimManager.cc | 120 +++++++++++
 MAC/APL/RTDBCommon/test/tClaimManager.h  |  50 +++++
 9 files changed, 642 insertions(+), 17 deletions(-)
 create mode 100644 MAC/APL/RTDBCommon/src/CM_Protocol.prot
 create mode 100644 MAC/APL/RTDBCommon/src/ClaimMgrTask.cc
 create mode 100644 MAC/APL/RTDBCommon/src/ClaimMgrTask.h
 create mode 100644 MAC/APL/RTDBCommon/test/tClaimManager.cc
 create mode 100644 MAC/APL/RTDBCommon/test/tClaimManager.h

diff --git a/.gitattributes b/.gitattributes
index 7ccbc87513b..60ed2e4240d 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -410,6 +410,7 @@ MAC/APL/RTDBCommon/configure.in -text
 MAC/APL/RTDBCommon/include/APL/RTDBCommon/Makefile.am -text
 MAC/APL/RTDBCommon/include/APL/RTDBCommon/RTDButilities.h -text
 MAC/APL/RTDBCommon/include/Makefile.am -text
+MAC/APL/RTDBCommon/src/CM_Protocol.prot -text
 MAC/APL/RTDBCommon/src/Makefile.am -text
 MAC/APL/RTDBCommon/src/RTDButilities.cc -text
 MAC/APL/RTDBCommon/test/Makefile.am -text
diff --git a/MAC/APL/RTDBCommon/src/CM_Protocol.prot b/MAC/APL/RTDBCommon/src/CM_Protocol.prot
new file mode 100644
index 00000000000..2f351a2515b
--- /dev/null
+++ b/MAC/APL/RTDBCommon/src/CM_Protocol.prot
@@ -0,0 +1,86 @@
+//#  CM_Protocol.prot: Protocol definition for the ClaimManager results
+//#
+//#  Copyright (C) 2008
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  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 2 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, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+//#  $Id: CM_Protocol.prot 11023 2008-03-18 08:07:14Z overeem $
+
+autogen definitions protocol;
+
+description = "Protocol for the Claim Manager results";
+prefix = "CM"; // for the signal names
+id = "(LOFAR::MACIO::F_GCF_PROTOCOL + 4)";
+
+// specify extra include files
+// include = '<someFile.h>';
+
+prelude = << PRELUDE_END
+
+// The following messages are defined in the servicebroker protocol
+//
+// CLAIM_RESULT (uint16 seqnr, string servicename);
+//
+PRELUDE_END;
+
+//
+// Define the errors
+//
+error = {
+	id = NO;
+	msg = "No errors";
+};
+error = {
+	id = NO_CONNECTION;
+	msg = "No connection with the ClaimManager in PVSS";
+};
+error = {
+	id = UNKNOWN_TYPE;
+	msg = "ObjectType unknown";
+};
+
+//
+// An "event" has a "signal" and a "dir" (direction)
+// and zero or more "param"s.
+// "dir" can be one of "IN" or "OUT".
+// A "param" has a "name" and a "type".
+//
+event = {
+	signal = CLAIM_RESULT;
+	dir = IN;
+	param = {
+		name = "result";
+		type = "uint32";
+	};
+	param = {
+		name = "typeName";
+		type = "string";
+	};
+	param = {
+		name = "nameInAppl";
+		type = "string";
+	};
+	param = {
+		name = "DPname";
+		type = "string";
+	};
+	param = {
+		name = "claimDate";
+		type = "string";
+	};
+};
+
diff --git a/MAC/APL/RTDBCommon/src/ClaimMgrTask.cc b/MAC/APL/RTDBCommon/src/ClaimMgrTask.cc
new file mode 100644
index 00000000000..07e6b0c80e2
--- /dev/null
+++ b/MAC/APL/RTDBCommon/src/ClaimMgrTask.cc
@@ -0,0 +1,242 @@
+//#  ClaimMgrTask.cc: Task for communicating with the ClaimManager in PVSS.
+//#
+//#  Copyright (C) 2008
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  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 2 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, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+//#  $Id$
+
+#include <lofar_config.h>
+#include <Common/LofarLogger.h>
+#include <Common/SystemUtil.h>
+#include <GCF/PVSS/GCF_PVString.h>
+#include <GCF/RTDB/DP_Protocol.ph>
+
+#include "CM_Protocol.ph"
+#include "ClaimMgrTask.h"
+
+namespace LOFAR {
+  using namespace GCF::TM;
+  using namespace GCF::PVSS;
+  using namespace GCF::RTDB;
+  namespace APL {
+    namespace RTDBCommon {
+
+//
+// Initialize static elements
+CMHandler* CMHandler::itsInstance = 0;
+
+//
+// CMHandler()
+//
+CMHandler::CMHandler()
+{
+}
+
+// --------------- Construction and destruction -----------
+//
+// ClaimMgrTask()
+//
+ClaimMgrTask::ClaimMgrTask() :
+	GCFTask((State)&ClaimMgrTask::operational, "ClaimManager"),
+	itsReplyPort	(0),
+	itsTimerPort	(new GCFTimerPort(*this, "timerPort")),
+	itsClaimMgrPS	(0),
+	itsResolveState(RO_UNDEFINED)
+{
+	registerProtocol(DP_PROTOCOL, DP_PROTOCOL_STRINGS);
+}
+
+//
+// ~ClaimMgrTask()
+//
+ClaimMgrTask::~ClaimMgrTask()
+{
+}
+
+// -------------------- static functions --------------------
+//
+// instance(temp)
+//
+ClaimMgrTask* ClaimMgrTask::instance(bool temporary)
+{
+	// if there is not taskHandler yet start one up.
+	if (!CMHandler::itsInstance) {    
+		CMHandler::itsInstance = new CMHandler();
+		// note: the CMHandler constructor also created an instance of theCMTask.
+		ASSERT(!CMHandler::itsInstance->mayDeleted());
+		CMHandler::itsInstance->itsCMTask.start();
+	}
+
+	if (!temporary) { 
+		CMHandler::itsInstance->use();
+	}
+
+	return (&CMHandler::itsInstance->itsCMTask);
+}
+
+//
+// release()
+//
+void ClaimMgrTask::release()
+{
+	ASSERT(CMHandler::itsInstance);
+	ASSERT(!CMHandler::itsInstance->mayDeleted());
+	// disconnect handler from the GCF engine.
+	CMHandler::itsInstance->leave(); 
+	if (CMHandler::itsInstance->mayDeleted()) {
+		delete CMHandler::itsInstance;
+		ASSERT(!CMHandler::itsInstance);
+	}
+}
+
+// -------------------- USER FUNCTIONS --------------------
+//
+// claimObject(objectype, nameInAppl)
+//
+void ClaimMgrTask::claimObject(const string&		objectType,
+							   const string&		nameInAppl,
+							   GCFPortInterface&	replyPort)	// ???
+{
+	ASSERTSTR(itsClaimMgrPS, "There is no propertyset to access the claimManager");
+
+	// save info
+	itsObjectType = objectType;
+	itsNameInAppl = nameInAppl;
+	itsReplyPort  = &replyPort;
+	if (itsResolveState == RO_READY) {
+		itsTimerPort->setTimer(0.1);	// wake up FSM
+	}
+	// else: some other time must be ative.
+}
+
+
+// -------------------- INTERNAL FUNCTIONS --------------------
+
+
+//
+// operational(event, port)
+//
+GCFEvent::TResult ClaimMgrTask::operational(GCFEvent& event, GCFPortInterface& port)
+{
+	LOG_DEBUG_STR ("ClaimMgrTask:" << eventName(event) << "@" << port.getName());
+	GCFEvent::TResult status = GCFEvent::HANDLED;
+  
+	switch (event.signal) {
+	case F_ENTRY: 
+		itsResolveState = RO_UNDEFINED;
+	break;
+
+	case F_INIT: {
+		LOG_DEBUG("Create propertySet for accessing the ClaimManaher");
+		itsResolveState = RO_CREATING;
+		itsClaimMgrPS = new RTDBPropertySet("ClaimManager", "ClaimManager", PSAT_RW, this);
+		itsTimerPort->setTimer(5.0);
+	}
+	break;
+
+	case DP_CREATED: {
+		DPCreatedEvent	dpEvent(event);
+		LOG_DEBUG_STR("Result of creating '" << dpEvent.DPname << "' = " << dpEvent.result);
+		itsResolveState = RO_CREATED;
+		itsTimerPort->cancelAllTimers();
+		itsTimerPort->setTimer(0.5); // give RTDB time to get the original value.
+	}
+	break;
+	
+	// Note: we finish each resolve state with a timer setting so each state always ends here.
+	//       This way we only have to implement 1 statemachine in stead of 7.
+	case F_TIMER: {
+		LOG_DEBUG_STR("itsResolveState=" << itsResolveState);
+
+		switch (itsResolveState) {
+		case RO_CREATED:
+			itsResolveState = RO_READY;
+			itsTimerPort->setTimer(0.1);
+			break;
+
+		case RO_READY:
+			if (itsObjectType.empty() || itsNameInAppl.empty()) {
+				LOG_DEBUG_STR("Nothing to claim");
+				break;
+			}
+			// request a DPname
+			itsClaimMgrPS->setValue("Request.TypeName",      
+							GCFPVString(itsObjectType), 0.0, false);
+			itsClaimMgrPS->setValue("Request.NewObjectName", 
+							GCFPVString(itsNameInAppl), 0.0, false);
+			itsClaimMgrPS->flush();
+			itsResolveState = RO_ASKED;
+			// clear result fields
+			itsFieldsReceived = 0;
+			itsResultDPname.clear();
+			itsTimerPort->setTimer(3.0);		// don't wait forever.
+		break;
+
+		case RO_ASKED:
+			LOG_ERROR_STR("No response from ClaimManager in 3 seconds");
+			// ???
+		break;
+		}
+	}
+	break;
+
+	case DP_CHANGED: {
+// NOTE: we are called here for every field!
+// CS001:ClaimManager.Request.NewObjectName
+// CS001:ClaimManager.Request.TypeName
+		DPChangedEvent	dpEvent(event);
+		LOG_DEBUG_STR("DP " << dpEvent.DPname << " changed");
+		if (dpEvent.DPname.find("Response.NewObjectName") != string::npos) {
+			string	fldContents(((GCFPVString*)(dpEvent.value._pValue))->getValue());
+			ASSERTSTR(fldContents == itsNameInAppl, "CM returned answer for request '" 
+						<< fldContents <<"' iso " << itsNameInAppl);
+			itsFieldsReceived++;
+		}
+		else if (dpEvent.DPname.find("Response.TypeName") != string::npos) {
+			string	fldContents(((GCFPVString*)(dpEvent.value._pValue))->getValue());
+			ASSERTSTR(fldContents == itsObjectType, "CM returned answer for type '" 
+						<< fldContents <<"' iso " << itsObjectType);
+			itsFieldsReceived++;
+		}
+		else if (dpEvent.DPname.find("Response.DPName") != string::npos) {
+			itsResultDPname = ((GCFPVString*)(dpEvent.value._pValue))->getValue();
+			itsFieldsReceived++;
+		}
+		if (itsFieldsReceived >= 3) {
+			LOG_DEBUG_STR("@@@@@ NewObjectName = " << itsResultDPname);
+			// ... BACK TO USER
+			itsObjectType.clear();
+			itsNameInAppl.clear();
+			itsResultDPname.clear();
+			itsResolveState = RO_READY;
+			itsTimerPort->cancelAllTimers();
+		}
+	}
+	break;
+
+	default:
+		LOG_DEBUG_STR ("claimManager: default");
+		status = GCFEvent::NOT_HANDLED;
+		break;
+	}    
+	return (status);
+}
+
+    } // namespace RTDBCommon
+  } // namespace APL
+} // namespace LOFAR
diff --git a/MAC/APL/RTDBCommon/src/ClaimMgrTask.h b/MAC/APL/RTDBCommon/src/ClaimMgrTask.h
new file mode 100644
index 00000000000..9c3e6754db4
--- /dev/null
+++ b/MAC/APL/RTDBCommon/src/ClaimMgrTask.h
@@ -0,0 +1,103 @@
+//#  ClaimMgrTask.h: singleton class; bridge between controller application 
+//#                    and Property Agent
+//#
+//#  Copyright (C) 2002-2003
+//#  ASTRON (Netherlands Foundation for Research in Astronomy)
+//#  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//#
+//#  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 2 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, write to the Free Software
+//#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//#
+//#  $Id$
+
+#ifndef GTM_SERVICEBROKER_H
+#define GTM_SERVICEBROKER_H
+
+#include <Common/lofar_map.h>
+#include <Common/lofar_list.h>
+#include <MACIO/GCF_Event.h>
+#include <GCF/TM/GCF_Control.h>
+#include <GCF/RTDB/RTDB_PropertySet.h>
+
+namespace LOFAR {
+  using GCF::TM::GCFTask;
+  using GCF::TM::GCFTCPPort;
+  using GCF::TM::GCFPortInterface;
+  using GCF::TM::GCFHandler;
+  using GCF::RTDB::RTDBPropertySet;
+  namespace APL {
+    namespace RTDBCommon {
+
+/**
+*/
+
+class CMHandler;
+
+class ClaimMgrTask : public GCFTask
+{
+public:
+    ~ClaimMgrTask ();
+    static ClaimMgrTask* instance(bool temporary = false);
+    static void release();
+
+	// member functions
+    void claimObject(const string&		objectType,
+					 const string&		nameInAppl,
+					 GCFPortInterface&	replyPort);
+  
+private:
+	enum {
+		RO_UNDEFINED = 0,
+		RO_CREATING,
+		RO_CREATED,
+		RO_ASKED,
+		RO_READY
+	};
+
+    friend class CMHandler;
+    ClaimMgrTask ();		// only allowed by CMHandler
+
+	// state methods
+    GCFEvent::TResult operational (GCFEvent& e, GCFPortInterface& p);
+        
+	// data members        
+	GCFPortInterface*	itsReplyPort;		// Port to send the result to
+	GCFTimerPort*		itsTimerPort;		// for reconnecting to brokers
+	RTDBPropertySet*	itsClaimMgrPS;		// for accessing the ClaimManager
+	string				itsObjectType;		// Objecttype of object in claim
+	string				itsNameInAppl;		// Name user likes to use
+	uint32				itsResolveState;	// Where we are in claiming the object.
+	// result fields
+	uint32				itsFieldsReceived;
+	string				itsResultDPname;
+};
+
+class CMHandler : public GCFHandler
+{
+public:
+    ~CMHandler() { itsInstance = 0; }
+    void workProc() {}
+    void stop () {}
+    
+private:
+    friend class ClaimMgrTask;
+    CMHandler();
+
+    static CMHandler*	itsInstance;
+    ClaimMgrTask 		itsCMTask;
+};
+  } // namespace SB
+ } // namespace GCF
+} // namespace LOFAR
+#endif
diff --git a/MAC/APL/RTDBCommon/src/Makefile.am b/MAC/APL/RTDBCommon/src/Makefile.am
index e9c79ac19ec..0fd1c3f2912 100644
--- a/MAC/APL/RTDBCommon/src/Makefile.am
+++ b/MAC/APL/RTDBCommon/src/Makefile.am
@@ -1,19 +1,36 @@
-DOCHDRS						= RTDButilities.h
-   
-BUILT_SOURCES 				= 
+AUTOGEN = autogen
+SUFFIXES = .ph
+%.ph: %.prot
+	$(AUTOGEN) --writable -L $(datadir)/MACIO $<
+	cp $*.ph $(top_builddir)/include/APL/RTDBCommon
 
-EXTRA_DIST 					= $(sysconf_DATA)
+%.cc: %.prot
+	$(AUTOGEN) --writable -L $(datadir)/MACIO $<
+
+clean-local:
+	rm -f *.ph
+
+bin_PROGRAMS = versionrtdbcommon
+
+INSTHDRS					= CM_Protocol.ph ClaimMgrTask.h
+NOINSTHDRS					=    
+BUILT_SOURCES 				= CM_Protocol.cc \
+							  CM_Protocol.ph
 
 pkgincludedir				= $(includedir)/APL/RTDBCommon
-pkginclude_HEADERS			= $(BUILT_SOURCES)
+pkginclude_HEADERS			= $(NOINSTHDRS) $(INSTHDRS)
+DOCHDRS						= $(pkginclude_HEADERS) $(BUILT_SOURCES)
+EXTRA_DIST 					= $(sysconf_DATA)
 
+# libraries
 lib_LTLIBRARIES 			= librtdbcommon.la
 
-librtdbcommon_la_SOURCES	= Package__Version.cc  $(BUILT_SOURCES) \
-							  RTDButilities.cc
-
-bin_PROGRAMS = versionrtdbcommon
+librtdbcommon_la_SOURCES	= Package__Version.cc \
+							  $(BUILT_SOURCES) \
+							  RTDButilities.cc \
+							  ClaimMgrTask.cc 
 
+# version stuff
 versionrtdbcommon_SOURCES      = versionrtdbcommon.cc
 versionrtdbcommon_LDADD        = librtdbcommon.la
 versionrtdbcommon_DEPENDENCIES = librtdbcommon.la $(LOFAR_DEPEND)
diff --git a/MAC/APL/RTDBCommon/src/RTDButilities.cc b/MAC/APL/RTDBCommon/src/RTDButilities.cc
index 05df6791ab8..d9fd1580595 100644
--- a/MAC/APL/RTDBCommon/src/RTDButilities.cc
+++ b/MAC/APL/RTDBCommon/src/RTDButilities.cc
@@ -25,11 +25,13 @@
 
 //# Includes
 #include <Common/LofarLogger.h>
+#include <GCF/TM/GCF_Control.h>
 #include <GCF/RTDB/DPservice.h>
 #include <GCF/PVSS/GCF_PVTypes.h>
 #include <APL/RTDBCommon/RTDButilities.h>
 
 namespace LOFAR {
+  using namespace GCF::TM;
   using namespace GCF::PVSS;
   using namespace GCF::RTDB;
   namespace APL {
@@ -41,13 +43,13 @@ typedef struct RTDBObjState {
 } RTDBobjState_t;
 
 RTDBobjState_t	objStateTable[] = {
-	{	 0,	"Off"			},
-	{	10,	"Operational"	},
-	{	20,	"Maintenance"	},
-	{	30,	"Test"			},
-	{	46,	"Suspicious"	},
-	{	56,	"Broken"		},
-	{	60,	"Not available"	}
+	{	 0,	"'Off'"				},
+	{	10,	"'Operational'"		},
+	{	20,	"'Maintenance'"		},
+	{	30,	"'Test'"			},
+	{	46,	"'Suspicious'"		},
+	{	56,	"'Broken'"			},
+	{	60,	"'Not available'"	}
 };
 
 // Every softwaremodule or hardwarecomponent has a color on the Navigator screens
diff --git a/MAC/APL/RTDBCommon/test/Makefile.am b/MAC/APL/RTDBCommon/test/Makefile.am
index 9b2d7c9c4d2..06bdbbe986d 100644
--- a/MAC/APL/RTDBCommon/test/Makefile.am
+++ b/MAC/APL/RTDBCommon/test/Makefile.am
@@ -1,4 +1,4 @@
-check_PROGRAMS 					= tRTDButilities
+check_PROGRAMS 					= tRTDButilities tClaimManager
 
 TESTS							= 
 
@@ -6,6 +6,10 @@ tRTDButilities_SOURCES 			= $(BUILT_SOURCES) tRTDButilities.cc
 tRTDButilities_LDADD			= ../src/librtdbcommon.la $(LOFAR_DEPEND)
 tRTDButilities_DEPENDENCIES		= ../src/librtdbcommon.la $(LOFAR_DEPEND)
 
+tClaimManager_SOURCES 			= $(BUILT_SOURCES) tClaimManager.cc
+tClaimManager_LDADD				= ../src/librtdbcommon.la $(LOFAR_DEPEND)
+tClaimManager_DEPENDENCIES		= ../src/librtdbcommon.la $(LOFAR_DEPEND)
+
 include $(top_srcdir)/Makefile.common
 
 
diff --git a/MAC/APL/RTDBCommon/test/tClaimManager.cc b/MAC/APL/RTDBCommon/test/tClaimManager.cc
new file mode 100644
index 00000000000..e88224bd995
--- /dev/null
+++ b/MAC/APL/RTDBCommon/test/tClaimManager.cc
@@ -0,0 +1,120 @@
+//
+//  tClaimManager.cc: Test program to test the resolve Observation mechanism
+//
+//  Copyright (C) 2008
+//  ASTRON (Netherlands Foundation for Research in Astronomy)
+//  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//
+//  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 2 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, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//
+//  $Id$
+
+#include <lofar_config.h>
+#include <Common/LofarLogger.h>
+#include <APL/RTDBCommon/CM_Protocol.ph>
+#include <APL/RTDBCommon/ClaimMgrTask.h>
+#include "tClaimManager.h"
+
+namespace LOFAR {
+  using namespace GCF;
+  using namespace GCF::TM;
+  namespace APL {
+    namespace RTDBCommon {
+
+int		gTestNr = 0;
+
+MgrTest::MgrTest(const string& name) : 
+	GCFTask((State)&MgrTest::doTest, name),
+	itsTimerPort(0)
+{
+	LOG_DEBUG_STR("=@=@= MgrTest(" << name << ")");
+
+	itsTimerPort = new GCFTimerPort(*this, "timerPort");
+	ASSERTSTR(itsTimerPort, "=@=@= Can't allocate GCFTimerPort");
+
+	registerProtocol(F_FSM_PROTOCOL, F_FSM_PROTOCOL_STRINGS);
+	registerProtocol(CM_PROTOCOL, 	 CM_PROTOCOL_STRINGS);
+}
+
+//
+// destructor
+//
+MgrTest::~MgrTest()
+{
+	LOG_DEBUG("=@=@= Deleting MgrTest");
+}
+
+//
+// doTest (event, port)
+//
+GCFEvent::TResult MgrTest::doTest(GCFEvent& event, GCFPortInterface& port)
+{
+	LOG_DEBUG_STR ("=@=@= doTest:" << eventName(event) << "@" << port.getName());
+	GCFEvent::TResult status = GCFEvent::HANDLED;
+
+	switch (event.signal) {
+	case F_INIT: 
+		LOG_DEBUG_STR("=@=@= Creating a ClaimManager");
+		itsClaimMgrTask = ClaimMgrTask::instance();
+		ASSERTSTR(itsClaimMgrTask, "Can't construct a claimMgrTask");
+
+		LOG_DEBUG_STR("=@=@= Calling claimManager for 'Observation5'");
+		itsClaimMgrTask->claimObject("Observation", "LOFAR_ObsSW_Observation5", *itsTimerPort);
+		itsTimerPort->setTimer(10.0);
+		break;
+
+	case F_ENTRY:
+	break;
+
+	case CM_CLAIM_RESULT: {
+		CMClaimResultEvent	cmEvent(event);
+		LOG_DEBUG_STR("=@=@= ObjectType: " << cmEvent.typeName);
+		LOG_DEBUG_STR("=@=@= NameInAppl: " << cmEvent.nameInAppl);
+		LOG_DEBUG_STR("=@=@= DBaseName : " << cmEvent.DPname);
+	}
+	break;
+
+	case F_TIMER:
+		stop();
+		break;
+
+	default:
+		status = GCFEvent::NOT_HANDLED;
+		break;
+	}
+
+	return status;
+}
+
+
+    } // namepsace RTDBCommon
+  } // namspace APL
+} // namespace LOFAR
+
+using namespace LOFAR;
+using namespace LOFAR::GCF;
+using namespace LOFAR::APL::RTDBCommon;
+
+int main(int argc, char* argv[])
+{
+	TM::GCFTask::init(argc, argv);
+
+	MgrTest test_task("UtilTest");  
+	test_task.start(); // make initial transition
+
+	TM::GCFTask::run();
+
+	return 0;
+}
diff --git a/MAC/APL/RTDBCommon/test/tClaimManager.h b/MAC/APL/RTDBCommon/test/tClaimManager.h
new file mode 100644
index 00000000000..de6c02bfa1d
--- /dev/null
+++ b/MAC/APL/RTDBCommon/test/tClaimManager.h
@@ -0,0 +1,50 @@
+//  tClaimManager.h: Definition of the DPservice task class.
+//
+//  Copyright (C) 2008
+//  ASTRON (Netherlands Foundation for Research in Astronomy)
+//  P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
+//
+//  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 2 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, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+//
+//  $Id$
+//
+
+#ifndef _RTDBCOMMON_TCLAIMMANAGER_H
+#define _RTDBCOMMON_TCLAIMMANAGER_H
+
+#include <GCF/TM/GCF_Control.h>
+
+namespace LOFAR {
+ namespace APL {
+  namespace RTDBCommon {
+
+class MgrTest : public GCFTask
+{
+public:
+	MgrTest (const string& name);
+	virtual ~MgrTest();
+
+	GCFEvent::TResult doTest	(GCFEvent& e, GCFPortInterface& p);
+
+private:
+	GCFTimerPort*		itsTimerPort;
+	ClaimMgrTask*		itsClaimMgrTask;
+};
+
+  } // namespace RTDBCommon
+ } // namespace APL
+} // namespace LOFAR
+
+#endif
-- 
GitLab