ipc.h 30.1 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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como ipc.h e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

#ifndef LIB3270_IPC_H_INCLUDED

	#define LIB3270_IPC_H_INCLUDED 1

	#include <iostream>
	#include <cstdarg>
	#include <vector>
	#include <functional>
	#include <lib3270.h>
	#include <lib3270/keyboard.h>
	#include <lib3270/actions.h>

	#if defined(_WIN32) || defined(_MSC_VER)

		#define TN3270_PUBLIC	__declspec (dllexport)
		#define TN3270_PRIVATE

	#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)

		#define TN3270_PUBLIC
		#define TN3270_PRIVATE

	#elif defined(__GNUC__)

		#define TN3270_PUBLIC	__attribute__((visibility("default")))
		#define TN3270_PRIVATE	__attribute__((visibility("hidden")))

	#else

		#error Unable to set visibility attribute

	#endif

#ifdef __cplusplus

	#include <string>

	namespace TN3270 {

		class Host;
		class Controller;
		class Action;

		#define DEFAULT_TIMEOUT 5

		/**
		 * @brief Get IPC module library version.
		 *
		 * @return Constant string with the IPC module library version (It's not the same of lib3270).
		 *
		 */
		TN3270_PUBLIC const char * getVersion();

		/**
		 * @brief Get IPC module library revision.
		 *
		 * @return Constant string with the IPC module library revision (It's not the same of lib3270).
		 *
		 */
		TN3270_PUBLIC const char * getRevision();

		/**
		 * @brief Get protocol library install location.
		 *
		 * @return The installation path of the tn3270 protocol library.
		 *
		 */
		TN3270_PUBLIC std::string getInstallLocation();

		/**
		 * @brief Get list of attributes.
		 *
		 */
		TN3270_PUBLIC std::vector<const LIB3270_PROPERTY *> getAttributes();

		/**
		 * @brief Get list of actions.
		 *
		 */
		TN3270_PUBLIC std::vector<const LIB3270_ACTION *> getActions();

		/**
		 * @brief Lib3270 dynamic memory block wrapper.
		 *
		 */
		template<typename T>
		class lib3270_auto_cleanup {
		private:
			T *data;

		public:
			lib3270_auto_cleanup(T *data) {
				this->data = data;
			}

			~lib3270_auto_cleanup() {
				lib3270_free((void *) this->data);
			}

			operator bool() const noexcept {
				return this->data != NULL;
			}

			T * operator->() {
				return this->data;
			}

			operator T *() const noexcept {
				return this->data;
			}

		};

		/**
		 * @brief TN3270 Event.
		 *
		 */
		class TN3270_PUBLIC Event {
		public:

			/// @brief Event type.
			enum Type : uint8_t {
				All,			///< @brief All events (undefined).
				Popup,			///< @brief Popup message.
				Trace,			///< @brief Trace message.
				Message,		///< @brief Generic message.
				Connection		///< @brief Connect/Disconnect event.
			};

		private:
			Type type;

		protected:

			/// @brief Creates an event.
			///
			/// @param Type of the event.
			Event(enum Type type);

		public:
			virtual ~Event();

			/// @brief Check event type
			inline bool is(Event::Type type) const noexcept {
				return this->type == type;
			}

			/// @brief Check event type
			inline bool operator==(Event::Type type) const noexcept {
				return this->type == type;
			}

			inline operator Event::Type() const noexcept {
				return this->type;
			}

			/// @brief Get event description.
			virtual std::string toString() const = 0;

		};

		/// @brief TN3270 Program message.
		enum ProgramMessage : uint8_t {
			MESSAGE_NONE			= LIB3270_MESSAGE_NONE,				///< @brief No message
			MESSAGE_SYSWAIT			= LIB3270_MESSAGE_SYSWAIT,			///< @brief --
			MESSAGE_TWAIT			= LIB3270_MESSAGE_TWAIT,			///< @brief --
			MESSAGE_CONNECTED		= LIB3270_MESSAGE_CONNECTED,		///< @brief Connected
			MESSAGE_DISCONNECTED	= LIB3270_MESSAGE_DISCONNECTED,		///< @brief Disconnected from host
			MESSAGE_AWAITING_FIRST	= LIB3270_MESSAGE_AWAITING_FIRST,	///< @brief --
			MESSAGE_MINUS			= LIB3270_MESSAGE_MINUS,			///< @brief --
			MESSAGE_PROTECTED		= LIB3270_MESSAGE_PROTECTED,		///< @brief --
			MESSAGE_NUMERIC			= LIB3270_MESSAGE_NUMERIC,			///< @brief --
			MESSAGE_OVERFLOW		= LIB3270_MESSAGE_OVERFLOW,			///< @brief --
			MESSAGE_INHIBIT			= LIB3270_MESSAGE_INHIBIT,			///< @brief --
			MESSAGE_KYBDLOCK		= LIB3270_MESSAGE_KYBDLOCK,			///< @brief Keyboard is locked

			MESSAGE_X				= LIB3270_MESSAGE_X,				///< @brief --
			MESSAGE_RESOLVING		= LIB3270_MESSAGE_RESOLVING,		///< @brief Resolving hostname (running DNS query)
			MESSAGE_CONNECTING		= LIB3270_MESSAGE_CONNECTING		///< @brief Connecting to host

		};

		/// @brief connection state.
		enum ConnectionState : uint8_t {
			DISCONNECTED		= LIB3270_NOT_CONNECTED,				///< @brief disconnected
			RESOLVING			= LIB3270_RESOLVING,					///< @brief resolving hostname
			PENDING				= LIB3270_PENDING,						///< @brief connection pending
			CONNECTED_INITIAL	= LIB3270_CONNECTED_INITIAL,			///< @brief connected, no mode yet
			CONNECTED_ANSI		= LIB3270_CONNECTED_ANSI,				///< @brief connected in NVT ANSI mode
			CONNECTED_3270		= LIB3270_CONNECTED_3270,				///< @brief connected in old-style 3270 mode
			CONNECTED_INITIAL_E	= LIB3270_CONNECTED_INITIAL_E,			///< @brief connected in TN3270E mode, unnegotiated
			CONNECTED_NVT		= LIB3270_CONNECTED_NVT,				///< @brief connected in TN3270E mode, NVT mode
			CONNECTED_SSCP		= LIB3270_CONNECTED_SSCP,				///< @brief connected in TN3270E mode, SSCP-LU mode
			CONNECTED_TN3270E	= LIB3270_CONNECTED_TN3270E,			///< @brief connected in TN3270E mode, 3270 mode
		};

		/// @brief Security state of the connection with the host.
		enum SSLState : uint8_t {
			SSL_UNSECURE	= LIB3270_SSL_UNSECURE,                   	///< @brief No secure connection
			SSL_SECURE		= LIB3270_SSL_SECURE,						///< @brief Connection secure with CA check
			SSL_NEGOTIATED	= LIB3270_SSL_NEGOTIATED,					///< @brief Connection secure, no CA, self-signed or expired CRL
			SSL_NEGOTIATING	= LIB3270_SSL_NEGOTIATING,					///< @brief Negotiating SSL
			SSL_UNDEFINED	= LIB3270_SSL_UNDEFINED						///< @brief Undefined
		};

		/// @brief PF Key
		enum PFKey : uint8_t {
			PF_1,
			PF_2,
			PF_3,
			PF_4,
			PF_5,
			PF_6,
			PF_7,
			PF_8,
			PF_9,
			PF_10,
			PF_11,
			PF_12,
			PF_13,
			PF_14,
			PF_15,
			PF_16,
			PF_17,
			PF_18,
			PF_19,
			PF_20,
			PF_21,
			PF_22,
			PF_23,
			PF_24
		};

		/// @brief PA Key
		enum PAKey : uint8_t {
			PA_1,
			PA_2,
			PA_3
		};

		/// @brief Keyboard Actions.
		enum KeyboardAction : uint8_t {
			ENTER,				///< @brief Enter key
			ERASE,
			ERASE_EOF,
			ERASE_EOL,
			ERASE_INPUT,
			KYBD_RESET,
			NEWLINE,
			CLEAR,
			SELECT_FIELD,
			SELECT_ALL,
			UNSELECT,
			RESELECT,
			CHAR_DELETE,
			DUP,
			FIELDMARK,
			BACKSPACE,
			WORD_PREVIOUS,
			WORD_NEXT,
			FIELD_END,		///< @brief Move the cursor to the first blank after the last nonblank in the field.
			FIELD_FIRST,	///< @brief Move to first unprotected field on screen.
			FIELD_NEXT,		///< @brief Jump to the next field.
			FIELD_PREVIOUS,	///< @brief Tab backward to previous field.
			ATTN,			///< @brief ATTN key, per RFC 2355.  Sends IP, regardless.
			BREAK,
			WORD_DELETE,	///< @brief Backspaces the cursor until it hits the front of a word (does a ^W).
			FIELD_DELETE,	///< @brief Delete field key (does a ^U).
			SYSREQ,
			KYBD_UNLOCK,	///< @brief Unlock the keyboard if it was locked by operator error.
		};

		/// @brief Dynamic Data type
		class TN3270_PUBLIC Attribute {
		public:

			/// @brief IPC Data type.
			enum Type : uint8_t {
				String	= 's',
				Boolean	= 'b',
				Uchar	= 'y',
				Int16	= 'n',
				Uint16	= 'q',
				Int32	= 'i',
				Int32x	= 'h',
				Uint32	= 'u',
				Int64	= 'x',
				Uint64	= 't'
			};

		private:
			Type 	  type;			///< @brief Data type.

		protected:

			size_t	  szData;
			uint8_t	* data;			///< @brief Internal worker.

			struct {
				std::function<const char *(const void *worker)> name;
				std::function<const char *(const void *worker)> description;

				std::function <std::string (const Attribute & attr, const void *worker)> asString;
				std::function <int32_t (const Attribute & attr, const void *worker)> asInt32;
				std::function <uint32_t (const Attribute & attr, const void *worker)> asUint32;
				std::function <bool (const Attribute & attr, const void *worker)> asBoolean;
			} get;

			struct {
				std::function <void (const Attribute & attr, const void *worker, const char *value)> asString;
				std::function <void (const Attribute & attr, const void *worker, const int32_t value)> asInt32;
				std::function <void (const Attribute & attr, const void *worker, const uint32_t value)> asUint32;
				std::function <void (const Attribute & attr, const void *worker, const bool value)> asBoolean;
			} set;

			Attribute(Type type, size_t szWorker = 0);

		public:

			Attribute(const Attribute &src);
			Attribute(const Attribute *src);

			~Attribute();

			inline const char * getName() const {
				return this->get.name(this->data);
			}

			inline const char * getDescription() const {
				return this->get.description(this->data);
			}

			inline std::string getString() const {
				return get.asString(*this,data);
			}

			inline int32_t getInt32() const {
				return get.asInt32(*this,data);
			}

			inline uint32_t getUint32() const {
				return get.asUint32(*this,data);
			}

			inline bool getBoolean() const {
				return get.asBoolean(*this,data);
			}

			inline std::string toString() const {
				return get.asString(*this, data);
			}

			inline void setString(const char * value) {
				set.asString(*this,data,value);
			}

			inline void setInt32(const int32_t value) {
				printf("\n%s %d\n\n",__FUNCTION__,value);
				set.asInt32(*this,data,value);
			}

			inline void setUint32(const uint32_t value) {
				set.asUint32(*this,data,value);
			}

			inline void setBoolean(const bool value) {
				set.asBoolean(*this,data,value);
			}

			inline Attribute & operator=(const char * value) {
				set.asString(*this,data,value);
				return *this;
			}

			inline Attribute & operator=(const int32_t value) {
				set.asInt32(*this,data,value);
				return *this;
			}

			inline Attribute & operator=(const uint32_t value) {
				set.asUint32(*this,data,value);
				return *this;
			}

			inline Attribute & operator=(const bool value) {
				set.asBoolean(*this,data,value);
				return *this;
			}

			inline bool operator==(Type type) const noexcept {
				return this->type == type;
			}

			inline Type getType() const {
				return this->type;
			}

			inline operator Type() const {
				return this->type;
			}


		};

		/// @brief TN3270 Session.
		class TN3270_PUBLIC Session {
		protected:
			Session();

			/// @brief Write information to log file.
			void info(const char *fmt, ...) const;

			/// @brief Write warning to log file.
			void warning(const char *fmt, ...) const;

			/// @brief Write error to log file.
			void error(const char *fmt, ...) const;

			/// @brief Fire event.
			void fire(const Event &event);

		public:

			/// @brief Get an instance of the TN3270 session based on the supplied ID.
			static Session * getInstance(const char *id = nullptr, const char *charset = nullptr);
			virtual ~Session();

			// States
			virtual ProgramMessage getProgramMessage() const = 0;
			virtual ConnectionState getConnectionState() const = 0;
			virtual SSLState getSSLState() const = 0;

			// Contents
			virtual std::string	toString(int baddr = 0, int len = -1, char lf = '\n') const = 0;
			virtual std::string	toString(unsigned short row, unsigned short col, int len, char lf = '\n') const = 0;

			/// @brief Input string.
			virtual void push(const char *text, size_t length) = 0;
			virtual void push(int baddr, const char *text, int length) = 0;
			virtual void push(unsigned short row, unsigned short col, const char *text, int length) = 0;

			inline void push(const std::string &text) {
				push(-1,text.c_str(),text.size());
			}

			inline void push(int baddr, const std::string &text) {
				push(baddr,text.c_str(),text.size());
			}

			inline void push(unsigned short row, unsigned short col, const std::string &text) {
				push(row,col,text.c_str(),text.size());
			}

			void push(const PFKey key);
			void push(const PAKey key);
			virtual void push(const KeyboardAction action) = 0;

			/// @brief Get contents of field ad address.
			virtual void pop(int baddr, std::string &text) = 0;

			/// @brief Get contents of field at position.
			virtual void pop(unsigned short row, unsigned short col, std::string &text) = 0;

			/// @brief Get contents of field at cursor position.
			virtual void pop(std::string &text) = 0;

			/// @brief Input string parsing control char.
			///
			/// Insert string parsing the action codes prefixed with the defined control character.
			///
			/// Value | Action      | Description                                                |
			/// :----:|:------------|:-----------------------------------------------------------|
			///  @@P  | -           | Print the screen contents (if available)                   |
			///  @@@@ | -           | Input the @@ char.                                         |
			///  @@E  | ENTER       | -                                                          |
			///  @@F  | ERASE_EOF   | -                                                          |
			///  @@1  | PF1         | Send the PF1 key.                                          |
			///  @@2  | PF2         | Send the PF2 key.                                          |
			///  @@3  | PF3         | Send the PF3 key.                                          |
			///  @@4  | PF4         | Send the PF4 key.                                          |
			///  @@5  | PF5         | Send the PF5 key.                                          |
			///  @@6  | PF6         | Send the PF6 key.                                          |
			///  @@7  | PF7         | Send the PF7 key.                                          |
			///  @@8  | PF8         | Send the PF8 key.                                          |
			///  @@9  | PF9         | Send the PF9 key.                                          |
			///  @@a  | PF10        | Send the PF10 key.                                         |
			///  @@b  | PF11        | Send the PF11 key.                                         |
			///  @@c  | PF12        | Send the PF12 key.                                         |
			///  @@d  | PF13        | Send the PF13 key.                                         |
			///  @@e  | PF14        | Send the PF14 key.                                         |
			///  @@f  | PF15        | Send the PF15 key.                                         |
			///  @@g  | PF16        | Send the PF16 key.                                         |
			///  @@h  | PF17        | Send the PF17 key.                                         |
			///  @@u  | PF18        | Send the PF18 key.                                         |
			///  @@j  | PF19        | Send the PF19 key.                                         |
			///  @@k  | PF20        | Send the PF20 key.                                         |
			///  @@l  | PF21        | Send the PF21 key.                                         |
			///  @@m  | PF22        | Send the PF22 key.                                         |
			///  @@n  | PF23        | Send the PF23 key.                                         |
			///  @@o  | PF24        | Send the PF24 key.                                         |
			///  @@x  | PA1         | Send the PA1 key.                                          |
			///  @@y  | PA2         | Send the PA2 key.                                          |
			///  @@z  | PA3         | Send the PA3 key.                                          |
			///  @@D  | CHAR_DELETE |                                                            |
			///  @@N  | NEWLINE     |                                                            |
			///  @@C  | CLEAR       |                                                            |
			///  @@R  | KYBD_RESET  |                                                            |
			///  @@<  | BACKSPACE   |                                                            |
			///
			/// @return The keyboard lock state.
			///
			LIB3270_KEYBOARD_LOCK_STATE input(const std::string &str, const char control_char = '@');

			// Attributes
			virtual Attribute getAttribute(const char *name) const = 0;
			virtual void getAttributes(std::vector<Attribute> & attributes) const = 0;
			std::vector<Attribute> getAttributes() const;

			virtual void getAttribute(const char *name, int &value) const;
			virtual void getAttribute(const char *name, unsigned int &value) const;
			virtual void getAttribute(const char *name, std::string &value) const;
			virtual void getAttribute(const char *name, bool &value) const;

			virtual void setAttribute(const char *name, const int value);
			virtual void setAttribute(const char *name, const char *value);

			/// @brief Get the lib3270 version string.
			virtual std::string getVersion() const = 0;

			/// @brief Get the lib3270 revision string.
			virtual std::string getRevision() const = 0;

			/// @brief Get the LU name associated with the session, if there is one.
			virtual std::string getAssociatedLUName() const = 0;

			/// @brief Get the terminal lock state code.
			virtual LIB3270_KEYBOARD_LOCK_STATE getKeyboardLockState() const = 0;

			virtual std::string getHostURL() const = 0;
			virtual void setHostURL(const char *url) = 0;

			virtual void setUnlockDelay(unsigned short delay = 350) = 0;
			virtual void setLockOnOperatorError(bool lock = true) = 0;

			virtual unsigned short getScreenWidth() const = 0;
			virtual unsigned short getScreenHeight() const = 0;
			virtual unsigned short getScreenLength() const = 0;

			/// @brief Set cursor address.
			virtual unsigned short setCursor(int addr = -1) = 0;

			/// @brief Set cursor position.
			virtual unsigned short setCursor(unsigned short row, unsigned short col) = 0;

			/// @brief Get cursor address
			virtual unsigned short getCursorAddress() = 0;

			/// @brief Set local charset.
			virtual void setCharSet(const char *charset = NULL) = 0;

			/// @brief Execute action by name.
			virtual void action(const char *action_name) = 0;

			// Connect/disconnect
			virtual void connect(const char *url = nullptr, int seconds = DEFAULT_TIMEOUT) = 0;
			virtual void disconnect() = 0;

			/// @brief Wait.
			virtual void wait(time_t seconds) const = 0;

			/// @brief Wait until session state changes to "ready".
			virtual void waitForReady(time_t timeout = DEFAULT_TIMEOUT) const = 0;

			/// @brief Wait for screen changes.
			virtual void waitForChange(time_t seconds = DEFAULT_TIMEOUT) const = 0;

			/// @brief Wait for screen changes.
			virtual LIB3270_KEYBOARD_LOCK_STATE waitForKeyboardUnlock(time_t seconds = DEFAULT_TIMEOUT) const = 0;

			/// @brief Wait for string.
			virtual void wait(const char *text, int seconds = DEFAULT_TIMEOUT) = 0;

			/// @brief Wait for string.
			virtual void wait(unsigned short row, unsigned short col, const char *text, int seconds = DEFAULT_TIMEOUT) = 0;

			/// @brief Wait for string.
			virtual void wait(int addr, const char *text, int seconds = DEFAULT_TIMEOUT) = 0;

			/// @brief Send PF.
			virtual void pfkey(unsigned short value) = 0;

			/// @brief Send PA.
			virtual void pakey(unsigned short value) = 0;

			/// @brief Request print
			virtual void print(LIB3270_CONTENT_OPTION option = LIB3270_CONTENT_ALL) = 0;

			/// @brief Insert event listener.
			// void insert(Event::Type type, std::function <void(const Event &event)> listener);

			/// @brief Create an action object.
			virtual Action * getAction(const LIB3270_ACTION *descriptor);

			/// @brief Create an action object.
			Action * getAction(const char *name);

			/// @brief Checks if the terminal contains the string.
			size_t find(const char * str, size_t pos = 0) const;

			/// @brief Get the number of occurrences of a string in the terminal.
			size_t count(const char * str, size_t pos = 0) const;

			/// @brief Compare contents.
			int compare(int baddr, const char* s, int len = -1) const;
			int compare(unsigned short row, unsigned short col, const char* s, int len = -1) const;

		};

		/// @brief TN3270 Host
		class TN3270_PUBLIC Host : public std::basic_streambuf<char, std::char_traits<char> > {
		private:

			/// @brief Connection with the host
			Session *session;

			/// @brief How much seconds we wait for the terminal to be ready?
			time_t timeout;

		protected:

			/// @brief Writes characters to the associated file from the put area
			int sync() override;

			/// @brief Writes characters to the associated output sequence from the put area.
			int overflow(int c) override;

			/*
			/// @brief Write information to log file.
			void info(const char *fmt, ...) const;

			/// @brief Write warning to log file.
			void warning(const char *fmt, ...) const;

			/// @brief Write error to log file.
			void error(const char *fmt, ...) const;
			*/

		public:
			Host(const char *id, const char *charset = nullptr);

			~Host();

			inline bool operator==(ConnectionState state) const noexcept {
				return session->getConnectionState() == state;
			}

			// Contents

			/**
			 * @brief Input string parsing control char.
			 */
			LIB3270_KEYBOARD_LOCK_STATE input(const char *text, int length = -1, const char control_char = '@');

			Host & push(int baddr, const std::string &text);
			Host & push(unsigned short row, unsigned short col, const std::string &text);
			Host & push(const std::string &text);

			Host & push(int baddr, const char *str, int len = -1);
			Host & push(unsigned short row, unsigned short col, const char *str, int len = -1);
			Host & push(const char *str, int len = -1);

			Host & pop(int baddr, std::string &text);
			Host & pop(unsigned short row, unsigned short col, std::string &text);
			Host & pop(std::string &text);

			std::string toString() const;
			std::string toString(int baddr, int len = -1, char lf = '\n') const;
			std::string toString(unsigned short row, unsigned short col, int len = -1, char lf = '\n') const;

			template<typename T>
			Host & push(T value) {
				session->push(value);
				sync();
				return *this;
			}

			template<typename T>
			Host & pop(T value) {
				sync();
				session->pop(value);
				return *this;
			}

			// Actions
			Host & connect(const char *url = nullptr);
			Host & disconnect();
			Host & waitForReady(time_t seconds);

			inline Host & waitForReady() {
				return waitForReady(this->timeout);
			}

			inline LIB3270_KEYBOARD_LOCK_STATE waitForKeyboardUnlock() noexcept {
				return this->session->waitForKeyboardUnlock(timeout);
			}

			/// @brief Execute action by name.
			inline Host & action(const char *action_name) {
				session->action(action_name);
				return *this;
			}

			inline ProgramMessage getProgramMessage() const {
				return session->getProgramMessage();
			}

			operator bool() const;

			inline operator ProgramMessage() const {
				return getProgramMessage();
			}

			inline ConnectionState getConnectionState() const {
				return session->getConnectionState();
			}

			bool isReady() const;
			bool isConnected() const;

			inline operator ConnectionState() const {
				return getConnectionState();
			}

			/// @brief Set cursor address.
			inline Host & setCursor(int addr = -1) {
				session->setCursor(addr);
				return *this;
			}

			/// @brief Set cursor position.
			inline Host & setCursor(unsigned short row, unsigned short col) {
				session->setCursor(row,col);
				return *this;
			}

			/// @brief Get cursor address
			inline unsigned short getCursorAddress() {
				return session->getCursorAddress();
			}

			inline void setHostURL(const char *url) {
				session->setHostURL(url);
			}


			// Get properties
			Attribute getAttribute(const char *name) const;
			std::vector<Attribute> getAttributes() const;

			inline time_t getTimeout() const noexcept {
				return this->timeout;
			}

			inline Attribute operator[](const char *name) const {
				return getAttribute(name);
			}

			/// @brief Get lib3270 version.
			inline std::string getVersion() const {
				return session->getVersion();
			}

			/// @brief Get lib3270 revision.
			std::string getRevision() const {
				return session->getRevision();
			}

			/// @brief Get LU Name.
			std::string getAssociatedLUName() const {
				return session->getAssociatedLUName();
			}

			inline std::string getHostURL() const {
				return session->getHostURL();
			}

			inline LIB3270_KEYBOARD_LOCK_STATE getKeyboardLockState() const {
				return session->getKeyboardLockState();
			}

			inline SSLState getSSLState() const {
				return session->getSSLState();
			}

			inline unsigned short getScreenWidth() const {
				return session->getScreenWidth();
			}

			inline unsigned short getScreenHeight() const {
				return session->getScreenHeight();
			}

			inline unsigned short getScreenLength() const {
				return session->getScreenLength();
			}

			// Set properties
			inline void setTimeout(time_t timeout = DEFAULT_TIMEOUT) noexcept {
				this->timeout = timeout;
			}

			inline void setUnlockDelay(unsigned short delay = 350) {
				session->setUnlockDelay(delay);
			}

			inline void setLockOnOperatorError(bool lock = true) {
				session->setLockOnOperatorError(lock);
			}

			inline void setCharSet(const char *charset = NULL) {
				session->setCharSet(charset);
			}


			// Actions

			/// @brief Create new action object.
			///
			/// Alocate a new action object associated with the session, delete it
			/// when no longer necessary.
			///
			inline TN3270::Action * getAction(const LIB3270_ACTION *descriptor) {
				return session->getAction(descriptor);
			}

			/// @brief Create new action object.
			///
			/// Alocate a new action object associated with the session, delete it
			/// when no longer necessary.
			///
			inline TN3270::Action * getAction(const char *name) {
				return session->getAction(name);
			}

			/// @brief Send PF.
			inline Host & pfkey(unsigned short value) {
				session->pfkey(value);
				return *this;
			}

			/// @brief Send PA.
			inline Host & pakey(unsigned short value) {
				session->pakey(value);
				return *this;
			}

			/// @brief Request print
			inline Host & print(LIB3270_CONTENT_OPTION option = LIB3270_CONTENT_ALL) {
				session->print(option);
				return *this;
			}

			/// @brief Wait.
			inline Host & wait(unsigned short seconds) {
				session->wait(seconds);
				return *this;
			}

			/// @brief Wait for update.
			inline Host & waitForChange(unsigned short seconds) {
				session->waitForChange(seconds);
				return *this;
			}

			/// @brief Wait for string.
			inline void wait(const char *text) {
				return session->wait(text,this->timeout);
			}

			/// @brief Wait for string.
			inline void wait(unsigned short row, unsigned short col, const char *text) {
				return session->wait(row,col,text,this->timeout);
			}

			inline void wait(unsigned short row, unsigned short col, const char *text, time_t timeout) {
				return session->wait(row,col,text,timeout);
			}

			/// @brief Wait for string.
			inline void wait(int addr, const char *text) {
				return session->wait(addr,text,this->timeout);
			}

			inline void wait(int addr, const char *text, time_t timeout) {
				return session->wait(addr,text,timeout);
			}

			/// @brief Checks if the terminal contains the string.
			size_t find(const char * str, size_t pos = 0) const;

			/// @brief Get the number of occurrences of a string in the terminal.
			size_t count(const char * str, size_t pos = 0) const;

			/// @brief Compare contents.
			int compare(int baddr, const char* s, int len = -1) const;
			int compare(unsigned short row, unsigned short col, const char* s, int len = -1) const;

			// Set contents.

			/// @brief Input string.
			///
			/// Input the supplied string processing commands delimited by the control char in
			/// the same way of the old HLLAPI.
			///
			/// @param text			String with the text to input.
			/// @param control_char	Control character used to identify commands.
			// Host & input(const char *text, const char control_char = '@');

			// Host & input(const char *text, size_t sz);


			/*
			// Event listeners
			inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept {
				session->insert(type, listener);
				return *this;
			}
			*/


		};

	}

	/// @brief Convert the program message to a human readable string.
	///
	/// @param programMessage	The program message code.
	///
	/// @return Description of the program message.
	TN3270_PUBLIC const char * toCharString(const TN3270::ProgramMessage programMessage);

	/// @brief Convert the connection state to a human readable string.
	///
	/// @param connectionState	The conection state code.
	///
	/// @return Description of the connection state.
	TN3270_PUBLIC const char * toCharString(const TN3270::ConnectionState connectionState);

	/// @brief Convert the action's description.
	///
	/// @param The action code.
	///
	/// @return The action description.
	TN3270_PUBLIC const char * toCharString(const TN3270::KeyboardAction action);

	template <typename T>
	inline TN3270_PUBLIC TN3270::Session & operator<<(TN3270::Session& session, const T value) {
		return session.push(value);
	}

	template <typename T>
	inline TN3270_PUBLIC TN3270::Session & operator>>(TN3270::Session& session, const T value) {
		return session.pop(value);
	}

	template <typename T>
	inline TN3270_PUBLIC TN3270::Host & operator<<(TN3270::Host& host, const T value) {
		return host.push(value);
	}

	inline std::ostream & operator<<(std::ostream &stream, const TN3270::Host& host) {
        stream << host.toString();
        return stream;
	}

	inline std::ostream & operator<<(std::ostream &stream, const TN3270::Attribute& attribute) {
        stream << attribute.toString();
        return stream;
	}

#endif

#endif // LIB3270_IPC_H_INCLUDED