Fawkes API  Fawkes Development Version
thread_adapter.cpp
1 
2 /***************************************************************************
3  * thread_adapter.cpp - PLEXIL thread name adapter
4  *
5  * Created: Wed Aug 15 12:35:55 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "thread_adapter.h"
23 
24 #include <core/threading/thread.h>
25 
26 #include <AdapterConfiguration.hh>
27 #include <AdapterExecInterface.hh>
28 #include <AdapterFactory.hh>
29 #include <cstring>
30 
31 /** @class ThreadNamePlexilAdapter "log_adapter.h"
32  * Plexil adapter to provide logging facilities.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor.
37  * @param execInterface Reference to the parent AdapterExecInterface object.
38  */
39 ThreadNamePlexilAdapter::ThreadNamePlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
40 : InterfaceAdapter(execInterface)
41 {
42 }
43 
44 /** Constructor from configuration XML.
45  * @param execInterface Reference to the parent AdapterExecInterface object.
46  * @param xml A const reference to the XML element describing this adapter
47  * @note The instance maintains a shared pointer to the XML.
48  */
49 ThreadNamePlexilAdapter::ThreadNamePlexilAdapter(PLEXIL::AdapterExecInterface &execInterface,
50  pugi::xml_node const xml)
51 : InterfaceAdapter(execInterface, xml)
52 {
53 }
54 
55 /** Destructor. */
57 {
58 }
59 
60 /** Initialize adapter.
61  * @return true if initialization was successful, false otherwise.
62  */
63 bool
65 {
66  return true;
67 }
68 
69 /** Start adapter.
70  * @return true if starting was successful, false otherwise.
71  */
72 bool
74 {
75  std::string name;
76 
77  pugi::xml_node config = getXml();
78  pugi::xml_attribute xml_attr = config.attribute("name");
79  if (xml_attr) {
80  name = xml_attr.value();
81  } else {
82  for (const auto &c : config.children()) {
83  if (strcmp(c.name(), "Parameter") == 0) {
84  pugi::xml_attribute xml_key_attr = c.attribute("key");
85  if (xml_key_attr && strcmp(xml_key_attr.value(), "name") == 0) {
86  name = c.text().get();
87  }
88  }
89  }
90  }
91 
92  if (!name.empty()) {
94  } else if (fawkes::Thread::current_thread_name() == "") {
95  fawkes::Thread::current_thread_name("PlexilExecutive");
96  }
97  return true;
98 }
99 
100 /** Stop adapter.
101  * @return true if successful, false otherwise.
102  */
103 bool
105 {
106  return true;
107 }
108 
109 /** Reset adapter.
110  * @return true if successful, false otherwise.
111  */
112 bool
114 {
115  return true;
116 }
117 
118 /** Shut adapter down.
119  * @return true if successful, false otherwise.
120  */
121 bool
123 {
124  return true;
125 }
126 
127 extern "C" {
128 void
129 initThreadName()
130 {
131  REGISTER_ADAPTER(ThreadNamePlexilAdapter, "ThreadName");
132 }
133 }
ThreadNamePlexilAdapter::reset
virtual bool reset()
Reset adapter.
Definition: thread_adapter.cpp:112
ThreadNamePlexilAdapter::stop
virtual bool stop()
Stop adapter.
Definition: thread_adapter.cpp:103
ThreadNamePlexilAdapter::~ThreadNamePlexilAdapter
virtual ~ThreadNamePlexilAdapter()
Destructor.
Definition: thread_adapter.cpp:55
ThreadNamePlexilAdapter
Interface adapter to set thread name.
Definition: thread_adapter.h:30
fawkes::Thread::current_thread_name
static std::string current_thread_name()
Get the name of the current thread.
Definition: thread.cpp:1322
ThreadNamePlexilAdapter::ThreadNamePlexilAdapter
ThreadNamePlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
Constructor.
Definition: thread_adapter.cpp:38
ThreadNamePlexilAdapter::shutdown
virtual bool shutdown()
Shut adapter down.
Definition: thread_adapter.cpp:121
ThreadNamePlexilAdapter::initialize
virtual bool initialize()
Initialize adapter.
Definition: thread_adapter.cpp:63
ThreadNamePlexilAdapter::start
virtual bool start()
Start adapter.
Definition: thread_adapter.cpp:72