ACT-CV - Machine Vision for Cognitive Modeling
ActCvImpl.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
26 #ifndef ACTCVIMPL_H_
27 #define ACTCVIMPL_H_
28 
29 #if HAVE_ICE
30 
31 #include <string>
32 #include <map>
33 #include <sstream>
34 #include <Ice/Ice.h>
35 #include <IceUtil/IceUtil.h>
36 
37 #include <garbagecoll.h>
38 #include <GCString.h>
39 #include <Threads/Mutex.h>
40 
41 #include <Robot.h>
42 #include <vision/ActCvSource.h>
43 
44 #include "ActCV.h"
45 
46 
54 class ActCvImpl : public ActCV::Vision, public ActCvObserver {
56  mhthreads::Mutex mutex;
57 
62  std::string combinedData;
63 
65  std::map<std::string, std::string> data;
66 
67  void ComputeCombinedData() {
68  std::ostringstream ostr;
69  ostr << "(";
70  for (std::map<std::string, std::string>::iterator i=data.begin();
71  i!=data.end();i++) {
72  ostr << "("
73  << i->first << " "
74  << i->second << ")";
75  }
76  ostr << ")";
77  combinedData=ostr.str();
78  }
79 public:
80  ActCvImpl() : combinedData("()") {
81  }
82  std::string GetCurrent(const Ice::Current&) {
83  mutex.Enter();
84  std::string result = combinedData;
85  mutex.Leave();
86  return result;
87  }
88 
89  void NewVisionNotification(const ActCvSource &src) {
90  mutex.Enter();
91  data[src.GetTypeName()]=src.GetResult();
92  ComputeCombinedData();
93  mutex.Leave();
94  }
95 
96  const char* GetName() const {
97  return "vision";
98  }
99 };
100 
104 class KeyboardImpl : public ActCV::Keyboard {
105  Robot robot;
106  bool isAttached;
107 public:
108  KeyboardImpl() : isAttached(false) {
109  robot.SetAutoDelay(50);
110  }
111  ~KeyboardImpl() {}
112  void PressKey(int keyCode, const Ice::Current&) {
113  std::cout << "received key " << keyCode << std::endl;
114  if (!isAttached) {
116  isAttached=true;
117  }
118  robot.KeyPress(keyCode);
119  robot.KeyRelease(keyCode);
120  }
121 
122  const char* GetName() const {
123  return "keyboard";
124  }
125 };
126 
130 class IceServer {
131  friend struct IceServerAutoStop;
132  static IceServer *singleInstance;
133  IceServer() : adapterName("ActCV") {
134  Start();
135  }
136 
137  void Start() {
138  try {
139  communicator = Ice::initialize();
140  adapter =
141  communicator->createObjectAdapterWithEndpoints(adapterName, "tcp -p 34985");
142  adapter->add(&visionImpl, communicator->stringToIdentity(visionImpl.GetName()));
143  adapter->add(&keyboardImpl, communicator->stringToIdentity(keyboardImpl.GetName()));
144  adapter->activate();
145  } catch(const Ice::Exception& ex) {
146  std::cerr << ex << std::endl;
147  }
148  }
149 
150  Ice::CommunicatorPtr communicator;
151  Ice::ObjectAdapterPtr adapter;
152 
153  std::string adapterName;
154 
155  ActCvImpl visionImpl;
156  KeyboardImpl keyboardImpl;
157 
158 public:
159 
161  static IceServer* GetInstance() {
162  if (singleInstance==NULL) {
163  singleInstance=new IceServer();
164  }
165  return singleInstance;
166  }
167 
168  void Stop() {
169  communicator->destroy();
170  //communicator->waitForShutdown();
171  }
172 
173  ActCvObserver* GetVisionObserver() {
174  return &visionImpl;
175  }
176 };
177 
179 class IceClient {
180  Ice::CommunicatorPtr communicator;
181  ActCV::KeyboardPrx keyboard;
182 public:
183  IceClient() : keyboard(0), communicator(0) {}
184  void ConnectTo(const char* name, int port);
185  void SendKey(int keyCode);
186 };
187 
188 #endif // HAVE_ICE
189 
190 #endif /*ACTCVIMPL_H_*/


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)