nd-view.h 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
//<->VW_header<->
/*
 nd-view.h
 03 January 2012, Version: 0.1
 NDView View
 Antonio Carlos Domí­nguez-Brito <adominguez@iusiani.ulpgc.es>
 IUSIANI - Universidad de Las Palmas de Gran Canaria - Spain


 Generated by coolbot-c version 1.9.0

 Copyright: See COPYING file that comes with this distribution
*/
//<->/VW_header<->

//<->VW_define_include<->
#ifndef ND_VIEW_H
#define ND_VIEW_H
//<->/VW_define_include<->

// Gtk and Glib
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include <iostream>
#include <sstream>
using namespace std;

#include "coolbot.h"
#include "util/coolbot_naminginterface.h"
#include "ports/coolbot_connectinginterface.h"
#include "component/coolbot_packet_instances.h"

#include "network/coolbot_sockettable.h"
#include "network/coolbot_outconnectiontable.h"
#include "network/coolbot_inconnectiontable.h"
#include "network/coolbot_networkutils.h"
#include "network/coolbot_portmatching.h"
#include "network/coolbot_dc3pcommand.h"

#include "error/coolbot_networkerror.h"
#include "error/coolbot_networkreceivingerror.h"
#include "error/coolbot_networksendingerror.h"
#include "error/coolbot_networkservernotfound.h"
#include "error/coolbot_networkreject.h"
#include "error/coolbot_portinforequestmismatch.h"
#include "util/coolbot_gtk_util.h"

using namespace CoolBOT;

#include <ace/SOCK_Connector.h>
#include <ace/SOCK_Acceptor.h>
#include <ace/SOCK_Stream.h>
#include <ace/INET_Addr.h>
#include <ace/Handle_Set.h>

// INCLUDE USER SECTION: BEGIN

#include "player-robot-packets.h"

#include "grid-map.h"

#include "nd-packets.h"

#include "nd.h"

// INCLUDE USER SECTION: END

//<->VW_define_namespace<->
namespace NDViewSpace
//<->/VW_define_namespace<->
{
//<->VW_define_class<->
    class NDView:
//<->/VW_define_class<->
        public GtkElement,
        public ConnectingInterface,
        public NamingInterface
    {
        public:

//<->VW_public_enum<->
            //PUBLIC INPUT PORTS
            enum InputPorts
            {
                DEFAULT_INPUT_PORTS=0,
                ROBOT_CONFIG=DEFAULT_INPUT_PORTS,
                ND_DATA,
                INPUT_PORTS
            };
            //PUBLIC OUTPUT PORTS
            enum OutputPorts
            {
                DEFAULT_OUTPUT_PORTS=0,
                OUTPUT_PORTS=DEFAULT_OUTPUT_PORTS
            };
//<->/VW_public_enum<->

            enum
            {
                INVALID_TCP_PORT=0
            };

//<->VW_define_constructor<->
            NDView(
//<->/VW_define_constructor<->
                // USER FILL IN SECTION:BEGIN
                long refreshingPeriod = _DEFAULT_REFRESHING_PERIOD_,
                // USER FILL IN SECTION:END
                unsigned short tcpPort = INVALID_TCP_PORT
            );

//<->VW_define_destructor<->
            ~NDView()
//<->/VW_define_destructor<->
            {
                // USER FILL IN SECTION:BEGIN

                // USER FILL IN SECTION:END

                //if network needed
                if(_tcpPort_!=INVALID_TCP_PORT)
                  _networkDeactivation_();
                _release_();
            }

            OBox* getOBox(int port)
            {
              return (
                ( (port<0) || (port>=OUTPUT_PORTS) )?
                  NULL: _pOBox_
              );
            }

            IBoxInterface* getIBox(int port)
            {
              return (
                ( (port<0) ||  (port>=INPUT_PORTS) )?
                  NULL: _pIBox_
              );
            }

            IConnection* getConnection(int inputPort)
            {
              if( (inputPort<0) || (inputPort>=INPUT_PORTS) ) return NULL;

              return new IConnection(_pIBox_,inputPort,0);
            }

            IConnection* getConnection(int inputPort, int inputIndex)
            {
              if(
                (inputPort<0) || (inputPort>=INPUT_PORTS) ||
                (inputIndex<0) || (inputIndex>=_pIBox_->getPort(inputPort)->length())
              ) return NULL;

              return new IConnection(_pIBox_,inputPort,inputIndex);
            }

            IConnection* connect(int oPort, IConnection* pConnection)
            {
              return (
                ( _pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS) )?
                  _pOBox_->connect(oPort,pConnection): NULL
              );
            }

            IConnection* connect(int oPort, int oIndex, IConnection* pConnection)
            {
              return (
                (_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS))?
                  _pOBox_->connect(oPort,oIndex,pConnection): NULL
              );
            }

            IConnection* connect(int oPort,IBoxInterface* pIBox,int iPort)
            {
              return (
                (_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS))?
                  _pOBox_->connect(oPort,pIBox,iPort): NULL
              );
            }

            IConnection* connect(
              int oPort,
              IBoxInterface* pIBox,
              int iPort,
              int iIndex
            )
            {
              return (
                (_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS))?
                  _pOBox_->connect(oPort,pIBox,iPort,iIndex): NULL
              );
            }

            IConnection* connect(
              int oPort,
              int oIndex,
              IBoxInterface* pIBox,
              int iPort,
              int iIndex
            )
            {
              return (
                (_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS))?
                  _pOBox_->connect(oPort,oIndex,pIBox,iPort,iIndex): NULL
              );
            }

            void disconnect(int oPort,IConnection* pConnection)
            { if(_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS)) _pOBox_->disconnect(oPort,pConnection); }

            void disconnect(int oPort,IConnection* pConnection,int index)
            { if(_pOBox_ && (oPort>=0) && (oPort<OUTPUT_PORTS)) _pOBox_->disconnect(oPort,pConnection,index); }

            void disconnectAll()
            {
              if(!_pOBox_) return;
              for(int i=0; i<OUTPUT_PORTS; i++) _pOBox_->disconnectAll(i);
            }

            // NETWORK CONNECTION AND DISCONNECTION METHODS: BEGIN

            bool connect(
                int oPort,
                char *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort
            );

            bool disconnect(
                int oPort,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort
            );

            bool connect(
                int oPort,
                int oIndex,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort
            );

            bool disconnect(
                int oPort,
                int oIndex,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort
            );

            bool connect(
                int oPort,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort,
                int iIndex
            );

            bool disconnect(
                int oPort,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort,
                int iIndex
            );

            bool connect(
                int oPort,
                int oIndex,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort,
                int iIndex
            );

            bool disconnect(
                int oPort,
                int oIndex,
                ACE_TCHAR *remoteComponentTCPAddress,
                unsigned short remoteComponentTCPPort,
                int iPort,
                int iIndex
            );

            bool connectAllSlots(
              int oPort,
              ACE_TCHAR *remoteComponentTCPAddress,
              unsigned short remoteComponentTCPPort,
              int iPort
            );

            bool disconnectAllSlots(
              int oPort,
              ACE_TCHAR *remoteComponentTCPAddress,
              unsigned short remoteComponentTCPPort,
              int iPort
            );

            bool disconnectAllPorts();

            // NETWORK CONNECTION AND DISCONNECTION METHODS: END

//<->VW_define_name_method<->
            const char* name() const { return "NDView"; }
//<->/VW_define_name_method<->

            bool widgetCreation(
              GtkWidget* pVBox,
              GtkWidget* pNotebook,
              const char* pName = NULL
            );

            void deleteEvent();

            // PUBLIC USER SECTION: BEGIN

            // PUBLIC USER SECTION: END

    private:

//<->VW_private_enum<->

            //PRIVATE CONSTANTS
            enum
            {
                _DEFAULT_REFRESHING_PERIOD_=250,
                _DRAW_AREA_MARGIN_=3,
                _DRAW_AREA_WIDTH_=300,
                _DRAW_AREA_HEIGTH_=300,
                _DRAW_AREA_XY_DIVS_=GridMapSpace::GridMap::DEFAULT_GRID_XY_RANGE/1000,
                _GRID_AXES_FONT_SIZE_=8,
                _ARROW_WIDTH_=6,
                _ARROW_DEEP_=6,
                _ND_AREA_WIDTH_=300,
                _ND_AREA_HEIGTH_=250,
                _ND_AREA_X_DIVS_=4,
                _ND_AREA_Y_DIVS_=4,
                _GOAL_LINE_WIDTH_=6
            };
//<->/VW_private_enum<->

            // Enumerations for network input and output threads: begin

            // NETWORK INPUT PORTS
            enum _NetworkInputPorts_
            {
                _NETWORK_INPUT_CONTROL_=OUTPUT_PORTS,
                _NETWORK_OUTPUT_CONTROL_,
                _NETWORK_INPUT_MONITORING_,
                _NETWORK_OUTPUT_MONITORING_,
                _NETWORK_OUTPUT_TIMER_,
                _NETWORK_INPUT_PORTS_
            };

            enum _NetworkThreads_
            {
              _INPUT_NETWORK_THREAD_=0,
              _OUTPUT_NETWORK_THREAD_,
              _NETWORK_THREADS_
            };

            enum
            {
              _INPUT_NETWORK_THREAD_TIMEOUT_= 1000, // milliseconds
              _OUTPUT_NETWORK_THREAD_TIMER_PERIOD_= 1000, // milliseconds
              _NETWORK_SEND_RECEIVE_TIMEOUT_=2000 // milliseconds
            };

            // Enumerations for network input and output threads: end

            bool _pConnected_[INPUT_PORTS];

            // function prototypes for handling port packets
//<->VW_define_prototypes_handling_port_packets_function<->
            typedef void (*_PortFunction_)(NDView* pThis, GtkWidget* pWidget);
//<->/VW_define_prototypes_handling_port_packets_function<->

            IBox* _pIBox_;
            OBox* _pOBox_;
            Timer _timer_;
            long _refreshingPeriod_;

            // Data members for network support: begin

            PortThread* _pONetworkThread_;
            PortThread* _pINetworkThread_;

            IBoxInterface* _pONetworkThreadIBox_;
            IBoxInterface* _pINetworkThreadIBox_;
            IBoxInterface* _pONetworkMonitoringThreadIBox_;
            IBoxInterface* _pINetworkMonitoringThreadIBox_;

            OBox* _pINetworkThreadOBox_;

            unsigned short _tcpPort_;

            ACE_Handle_Set _masterHandleSet_, _activeHandleSet_;
            ACE_SOCK_Acceptor _acceptor_;

            TimerWrap* _pOutputNetworkThreadTimer_;

            SocketTable _outputSocketTable_;
            SocketTable _inputSocketTable_;
            OutConnectionTable* _pOutputConnectionTable_;
            InConnectionTable* _pInputConnectionTable_;

            void _networkAllocation_();

            void _networkRelease_();

            void _networkActivation_();

            void _networkDeactivation_();

            static bool _inputNetworkThreadBody_(void* pComp);
            static bool _idlePortHandler_(int port,void* pComp) { return true; }

            static bool _outputNetworkThreadPortHandler_(int port,void* pComp);

            bool _dc3pNextAction_(ACE_SOCK_Stream &sock,Dc3pCommand &command);

            unsigned short _getConnectionID_(ACE_INET_Addr &remoteAddr);

            unsigned short _getConnectionID_(ACE_SOCK_Stream &sock);

            // Data members for network support: end

            // PRIVATE USER SECTION: BEGIN

            GtkWidget* _pVBox_;

            GtkWidget* _pNotebook_;

            GtkWidget* _pStateLabel_;

            GtkWidget* _pExceptionCombo_;

            GtkWidget* _pTextView_;
            GtkTextBuffer* _pTextBuffer_;

            GtkWidget* _pDrawingArea_;
            GdkPixmap* _pPixmap_;

            GtkWidget* _pNDArea_;
            GdkPixmap* _pNDPixmap_;

            double _sensorMaxRange_;
            Point2D<int> _gridSize_;
            Point2D<int> _gridOrigin_;
            Point2D<int> _gridFirst_;
            Point2D<int> _gridLast_;
            Coordinates2D _gridDivs_;
            Coordinates2D _gridScaleFactor_;

            PangoLayout* _pStrMin_;
            PangoLayout* _pStrMax_;
            PangoLayout* _pStrZero_;
            PangoLayout* _pStrX_;
            PangoLayout* _pStrY_;

            PangoContext* _pStrContext_;

            Point2D<int> _originPND_;
            Point2D<int> _lastPND_;
            Point2D<int> _originRND_;
            Point2D<int> _lastRND_;
            Point2D<int> _sizePNDRND_;

            PangoLayout* _pStrPND_;
            PangoLayout* _pStrRND_;
            PangoLayout* _pStr0_;
            PangoLayout* _pStr180_;
            PangoLayout* _pStr360_;

            static GdkColor _black_;
            static GdkColor _white_;
            static GdkColor _red_;
            static GdkColor _lightRed_;
            static GdkColor _blue_;
            static GdkColor _lightBlue_;
            static GdkColor _green_;
            static GdkColor _lightGreen_;
            static GdkColor _orange_;

            PangoMatrix _identity_;
            PangoMatrix _rotatedText_;

            Coordinates2D _robotFi_;
            Coordinates2D _robotFd_;
            Coordinates2D _robotBi_;
            Coordinates2D _robotBd_;
//          Coordinates2D _robotO_;
//          Coordinates2D _robotI_;
//          Coordinates2D _robotJ_;
            double _robotRadius_;
            double _securityDistance_;

            // PRIVATE USER SECTION: END

            void _defaults_()
            {
                _pIBox_=NULL;
                _pOBox_=NULL;

                Util::set(_pConnected_,INPUT_PORTS,false);

                // network thread data members
                _pONetworkThread_=_pINetworkThread_=NULL;
                _pONetworkThreadIBox_=_pINetworkThreadIBox_=NULL;
                _pONetworkMonitoringThreadIBox_=_pINetworkMonitoringThreadIBox_=NULL;
                _pINetworkThreadOBox_=NULL;
                _pOutputNetworkThreadTimer_=NULL;
                _pOutputConnectionTable_=NULL;
                _pInputConnectionTable_ = NULL;

                // USER FILL IN SECTION: BEGIN

                _pVBox_=_pNotebook_=_pStateLabel_=_pExceptionCombo_=_pTextView_=NULL;
                _pTextBuffer_=NULL;
                _pDrawingArea_=_pNDArea_=NULL;
                _pPixmap_=_pNDPixmap_=NULL;

                _pStrMin_=_pStrMax_=NULL;
                _pStrZero_=_pStrX_=_pStrY_=NULL;
                _pStrPND_=_pStrRND_=NULL;
                _pStr0_=_pStr180_=_pStr360_=NULL;
                _pStrContext_=NULL;

                // USER FILL IN SECTION: END
            }

            void _release_()
            {
                auto_ptr<IBox> iBox(_pIBox_);
                auto_ptr<OBox> oBox(_pOBox_);

                if(_tcpPort_!=INVALID_TCP_PORT)
                  _networkRelease_();
            }

            // USER MEMBERS METHODS SECTION: BEGIN

            // Callback prototypes
            static gboolean _expose_(GtkWidget *pWidget, GdkEventExpose *event, gpointer data);
            static gboolean _NDExpose_(GtkWidget *pWidget, GdkEventExpose *event, gpointer data);

            static gboolean _configure_( GtkWidget *pWidget, GdkEventConfigure *event, gpointer data );
            static gboolean _NDConfigure_( GtkWidget *pWidget, GdkEventConfigure *event, gpointer data );

            void _configureGrid_(GtkWidget *pWidget);
            void _drawGrid_(GtkWidget*  pWidget);

            void _configureNDArea_(GtkWidget *pWidget);

            void _drawArrowUp_(GdkPixmap* pArea, GdkGC* pGc, int x, int y);
            void _drawArrowRight_(GdkPixmap* pArea, GdkGC* pGc, int x, int y);
            void _drawArrow_(GdkPixmap* pArea, GdkGC* pGc, int x, int y, double angle);

            void _drawRobot_(GtkWidget* pWidget);
            void _drawNDArea_(GtkWidget* pWidget);

            static void _gtkDataScan_(NDView* pThis, GtkWidget* pWidget);
            static void _gtkData_(NDView* pThis, GtkWidget* pWidget);

            void _configRobot_();

            // USER MEMBERS METHODS SECTION: END

            static void _timerCallback_(void* pPtr);

            static _PortFunction_ _pPortFunctions_[INPUT_PORTS];

            bool _activate_(long refreshingPeriod)
            {
              _timer_.start(
                refreshingPeriod,
                refreshingPeriod,
                _timerCallback_,
                this
              );
            }

            bool _deactivate_()
            {
                _timer_.stop();
            }

            static void _staticInitialization_();
            static void _staticFinalization_();

//<->VW_define_staticInit<->
            // StaticInit<NDView> carries out initialization of static variables
            friend class StaticInit<NDView>;
//<->/VW_define_staticInit<->

    };
}
namespace
{
//<->VW_define_instance_staticInit<->
 /*
  * NOTE: an instance of CoolBOT::StaticInit<NDViewSpace::NDView> obligues to initialize
  * the static variables of NDView before its use.
  */
  CoolBOT::StaticInit<NDViewSpace::NDView> JOIN2(staticInit,NDView);
//<->/VW_define_instance_staticInit<->

  // USER SECTION: BEGIN

  // USER SECTION: END
}
//<->VW_define_end_include<->
#endif // ND_VIEW_H
//<->/VW_define_end_include<->