remote.cc 22.3 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 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
/*
 * "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 remote.cc e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

 #include <lib3270/config.h>

 #if defined(HAVE_DBUS)
	#include <stdio.h>
	#include <dbus/dbus.h>
	#include <string.h>
	#include <malloc.h>
	#include <sys/types.h>
	#include <unistd.h>

	#ifndef DBUS_TIMEOUT_INFINITE
		#define DBUS_TIMEOUT_INFINITE ((int) 0x7fffffff)
	#endif // !DBUS_TIMEOUT_INFINITE

 #endif // HAVE_DBUS

 #if defined(WIN32)
	#include <windows.h>
	#include <pw3270/ipcpackets.h>
 #else
	#define HLLAPI_PACKET_IS_CONNECTED	"isConnected"
	#define HLLAPI_PACKET_GET_CSTATE	"getConnectionState"
	#define HLLAPI_PACKET_IS_READY		"isReady"
	#define HLLAPI_PACKET_DISCONNECT	"disconnect"
	#define HLLAPI_PACKET_GET_CURSOR	"getCursorAddress"
	#define HLLAPI_PACKET_ENTER			"enter"
	#define HLLAPI_PACKET_QUIT			"quit"
 #endif // WIN32

 #include <pw3270/class.h>
 #include <lib3270/log.h>

#if defined(HAVE_DBUS)
 static const char	* prefix_dest	= "br.com.bb.";
 static const char	* prefix_path	= "/br/com/bb/";
#endif // HAVE_DBUS

/*--[ Implement ]--------------------------------------------------------------------------------------------------*/

 namespace PW3270_NAMESPACE
 {

	class remote : public session
 	{
	private:

#if defined(WIN32)

		HANDLE			  hPipe;

		int query_intval(HLLAPI_PACKET id)
		{
			struct hllapi_packet_query      query		= { id };
			struct hllapi_packet_result		response;
			DWORD							cbSize		= sizeof(query);
			if(TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL))
				return response.rc;

			throw exception(GetLastError(),"%s","Transaction error");
		}

		string * query_string(void *query, size_t szQuery, size_t len)
		{
			struct hllapi_packet_text			* response;
			DWORD								  cbSize	= sizeof(struct hllapi_packet_text)+len;
			string								* s;

			response = (struct hllapi_packet_text *) malloc(cbSize+2);
			memset(response,0,cbSize+2);

			if(TransactNamedPipe(hPipe,(LPVOID) query, szQuery, &response, cbSize, &cbSize,NULL))
			{
				if(response->packet_id)
					s = new string("");
				else
					s = new string(response->text);
			}
			else
			{
				s = new string("");
			}

			free(response);

			return s;
		}

		int query_intval(void *pkt, size_t szQuery, bool dynamic = false)
		{
			struct hllapi_packet_result		response;
			DWORD							cbSize		= (DWORD) szQuery;
			BOOL 							status;

			status = TransactNamedPipe(hPipe,(LPVOID) pkt, cbSize, &response, sizeof(response), &cbSize,NULL);

			if(dynamic)
				free(pkt);

			if(status)
				return response.rc;

			throw exception(GetLastError(),"%s","Transaction error");

		}


#elif defined(HAVE_DBUS)

		DBusConnection	* conn;
		char			* dest;
		char			* path;
		char			* intf;

		DBusMessage * create_message(const char *method)
		{
			DBusMessage * msg = dbus_message_new_method_call(	this->dest,		// Destination
																this->path,		// Path
																this->intf,		// Interface
																method);		// method

			if (!msg)
				throw exception("Error creating DBUS message for method %s",method);

			return msg;

		}

		DBusMessage	* call(DBusMessage *msg)
		{
			DBusMessage		* reply;
			DBusError		  error;

			dbus_error_init(&error);
			reply = dbus_connection_send_with_reply_and_block(conn,msg,10000,&error);
			dbus_message_unref(msg);

			if(!reply)
			{
				exception e = exception("%s",error.message);
				dbus_error_free(&error);
				throw e;
			}
			return reply;
		}

		string * get_string(DBusMessage * msg)
		{
			if(msg)
			{
				DBusMessageIter iter;

				if(dbus_message_iter_init(msg, &iter))
				{
					if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
					{
						string 		* rc;
						const char	* str;
						dbus_message_iter_get_basic(&iter, &str);
						trace("Response: [%s]",str);
						rc = new string(str);
						dbus_message_unref(msg);
						return rc;
					}

					exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32);
					dbus_message_unref(msg);

					throw e;

				}

			}

			return NULL;
		}

		string * query_string(const char *method)
		{
			return get_string(call(create_message(method)));
		}

		int get_intval(DBusMessage * msg)
		{
			if(msg)
			{
				DBusMessageIter iter;

				if(dbus_message_iter_init(msg, &iter))
				{
					if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32)
					{
						dbus_int32_t iSigned;
						dbus_message_iter_get_basic(&iter, &iSigned);
						dbus_message_unref(msg);
						return (int) iSigned;
					}

					exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32);

					dbus_message_unref(msg);
					throw e;
					return -1;
				}
				dbus_message_unref(msg);
			}
			return -1;
		}

		int query_intval(const char *method)
		{
			return get_intval(call(create_message(method)));
		}

		int query_intval(const char *method, int first_arg_type, ...)
		{
			va_list 	  var_args;
			DBusMessage * msg = dbus_message_new_method_call(	this->dest,		// Destination
																this->path,		// Path
																this->intf,		// Interface
																method);		// method

			if (!msg)
			{
				throw exception("Error creating DBUS message for method %s",method);
				return -1;
			}

			va_start(var_args, first_arg_type);
			dbus_message_append_args_valist(msg,first_arg_type,var_args);
			va_end(var_args);

			return get_intval(call(msg));
		}

#else


		int query_intval(const char *method)
		{
			throw exception("Call to unimplemented RPC method \"%s\"",method);
			return -1;
		}

#endif

	public:

		remote(const char *session)
		{
#if defined(WIN32)
			static DWORD	  dwMode = PIPE_READMODE_MESSAGE;
			char	 		  buffer[4096];
			char			* str = strdup(session);
			char			* ptr;

			hPipe  = INVALID_HANDLE_VALUE;

			for(ptr=str;*ptr;ptr++)
			{
				if(*ptr == ':')
					*ptr = '_';
				else
					*ptr = tolower(*ptr);
			}

			snprintf(buffer,4095,"\\\\.\\pipe\\%s",str);

			free(str);

			if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT))
			{
				exception e = exception(GetLastError(),"Service instance %s unavailable",str);
				free(str);
				throw e;
				return;
			}

			hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);

			if(hPipe == INVALID_HANDLE_VALUE)
			{
				throw exception("Can´t create service pipe %s",buffer);
			}
			else if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL))
			{
				exception e = exception(GetLastError(),"%s","Can´t set pipe state");
				CloseHandle(hPipe);
				hPipe = INVALID_HANDLE_VALUE;
				throw e;
			}

#elif defined(HAVE_DBUS)

			DBusError	  err;
			int			  rc;
			char		* str = strdup(session);
			char		* ptr;
			char		  busname[4096];
			char		  pidname[10];
			int			  pid			= (int) getpid();

			trace("%s str=%p",__FUNCTION__,str);

			for(ptr=str;*ptr;ptr++)
				*ptr = tolower(*ptr);

			ptr = strchr(str,':');

			if(ptr)
			{
				size_t 		  sz;

				*(ptr++) = 0;

				// Build destination
				sz		= strlen(ptr)+strlen(str)+strlen(prefix_dest)+2;
				dest	= (char *) malloc(sz+1);
				strncpy(dest,prefix_dest,sz);
				strncat(dest,str,sz);
				strncat(dest,".",sz);
				strncat(dest,ptr,sz);

				// Build path
				sz		= strlen(str)+strlen(prefix_path);
				path	= (char *) malloc(sz+1);
				strncpy(path,prefix_path,sz);
				strncat(path,str,sz);

				// Build intf
				sz		= strlen(str)+strlen(prefix_dest)+1;
				intf	= (char *) malloc(sz+1);
				strncpy(intf,prefix_dest,sz);
				strncat(intf,str,sz);

			}
			else
			{
				size_t sz;

				// Build destination
				sz		= strlen(str)+strlen(prefix_dest)+2;
				dest	= (char *) malloc(sz+1);
				strncpy(dest,prefix_dest,sz);
				strncat(dest,str,sz);

				// Build path
				sz		= strlen(str)+strlen(prefix_path);
				path	= (char *) malloc(sz+1);
				strncpy(path,prefix_path,sz);
				strncat(path,str,sz);

				// Build intf
				sz		= strlen(str)+strlen(prefix_dest)+1;
				intf	= (char *) malloc(sz+1);
				strncpy(intf,prefix_dest,sz);
				strncat(intf,str,sz);

			}

			trace("DBUS:\nDestination:\t[%s]\nPath:\t\t[%s]\nInterface:\t[%s]",dest,path,intf);

			free(str);

			dbus_error_init(&err);

			conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
			trace("dbus_bus_get conn=%p",conn);

			if (dbus_error_is_set(&err))
			{
				exception e = exception("DBUS Connection Error (%s)", err.message);
				dbus_error_free(&err);
				throw e;
				return;
			}

			if(!conn)
			{
				throw exception("%s", "DBUS Connection failed");
				return;
			}

			memset(pidname,0,10);
			for(int f = 0; f < 9 && pid > 0;f++)
			{
				pidname[f] = 'a'+(pid % 25);
				pid /= 25;
			}

			snprintf(busname, 4095, "%s.rx3270.br.com.bb",pidname);

			trace("Busname: [%s]",busname);

			rc = dbus_bus_request_name(conn, busname, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
			trace("dbus_bus_request_name rc=%d",rc);

			if (dbus_error_is_set(&err))
			{
				exception e = exception("Name Error (%s)", err.message);
				dbus_error_free(&err);
				throw e;
				return;
			}

			if(rc != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
			{
				trace("%s: DBUS request for name %s failed",__FUNCTION__, busname);
				throw exception("DBUS request for \"%s\" failed",session);
				return;
			}

			trace("%s: Using DBUS name %s",__FUNCTION__,busname);

			const char * id   = "r";
			static const dbus_int32_t flag = 1;
			query_intval("setScript", DBUS_TYPE_STRING, &id, DBUS_TYPE_INT32, &flag, DBUS_TYPE_INVALID);


#else

			throw exception("%s","RPC support is incomplete.");

#endif
		}

		virtual ~remote()
		{
#if defined(WIN32)

			if(hPipe != INVALID_HANDLE_VALUE)
				CloseHandle(hPipe);

#elif defined(HAVE_DBUS)

			try
			{
				const char * id   = "r";
				static const dbus_int32_t flag = 0;
				query_intval("setScript", DBUS_TYPE_STRING, &id, DBUS_TYPE_INT32, &flag, DBUS_TYPE_INVALID);
			}
			catch(exception e)
			{

			}

			free(dest);
			free(path);
			free(intf);

#else

#endif
		}

		bool is_connected(void)
		{
			return query_intval(HLLAPI_PACKET_IS_CONNECTED) != 0;
		}

		LIB3270_CSTATE get_cstate(void)
		{
			return (LIB3270_CSTATE) query_intval(HLLAPI_PACKET_GET_CSTATE);
		}

		int connect(const char *uri, bool wait)
		{
#if defined(WIN32)

			size_t							  cbSize	= sizeof(struct hllapi_packet_connect)+strlen(uri);
			struct hllapi_packet_connect	* pkt		= (struct hllapi_packet_connect *) malloc(cbSize);

			pkt->packet_id	= HLLAPI_PACKET_CONNECT;
			pkt->wait		= (unsigned char) wait;
			strcpy(pkt->hostname,uri);

			trace("Sending %s",pkt->hostname);

			return query_intval((void *) pkt,cbSize,true);

#elif defined(HAVE_DBUS)

			int rc = query_intval("connect", DBUS_TYPE_STRING, &uri, DBUS_TYPE_INVALID);

			if(!rc && wait)
				return wait_for_ready(120);

			return rc;

#else
			return -1;

#endif
		}

		int wait_for_ready(int seconds)
		{
#if defined(WIN32)

			time_t end = time(0)+seconds;

			while(time(0) < end)
			{
				if(!is_connected())
					return ENOTCONN;

				if(is_ready())
					return 0;

				Sleep(250);
			}

			return ETIMEDOUT;

#elif defined(HAVE_DBUS)

			time_t end = time(0)+seconds;

			while(time(0) < end)
			{
				static const dbus_int32_t delay = 2;

				int rc = query_intval("waitForReady", DBUS_TYPE_INT32, &delay, DBUS_TYPE_INVALID);

				trace("waitForReady exits with rc=%d",rc);

				if(rc != ETIMEDOUT)
					return rc;
			}

			return ETIMEDOUT;

#else

			return -1;

#endif

		}

		bool is_ready(void)
		{
			return query_intval(HLLAPI_PACKET_IS_READY) != 0;
		}


		int disconnect(void)
		{
			return query_intval(HLLAPI_PACKET_DISCONNECT);
		}


		int wait(int seconds)
		{
#if defined(WIN32)

			time_t end = time(0)+seconds;

			while(time(0) < end)
			{
				if(!is_connected())
					return ENOTCONN;
				Sleep(500);
			}

			return 0;

#elif defined(HAVE_DBUS)

			time_t end = time(0)+seconds;

			while(time(0) < end)
			{
				if(!is_connected())
					return ENOTCONN;
				usleep(500);
			}

			return 0;

#else

			return -1;

#endif

		}

		int iterate(bool wait)
		{
#if defined(WIN32)
			return 0;
#elif defined(HAVE_DBUS)
			return 0;
#else
			return -1;
#endif
		}

		string * get_text_at(int row, int col, size_t sz)
		{
#if defined(WIN32)

			struct hllapi_packet_query_at query	= { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz };

			return query_string(&query,sizeof(query),sz);

#elif defined(HAVE_DBUS)

			dbus_int32_t r = (dbus_int32_t) row;
			dbus_int32_t c = (dbus_int32_t) col;
			dbus_int32_t l = (dbus_int32_t) sz;

			DBusMessage * msg = create_message("getTextAt");
			if(!msg)
				return NULL;

			trace("%s(%d,%d,%d)",__FUNCTION__,r,c,l);
			dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_INT32, &l, DBUS_TYPE_INVALID);

			return get_string(call(msg));

#else

			return NULL;

#endif

		}

		int set_text_at(int row, int col, const char *str)
		{
#if defined(WIN32)

			struct hllapi_packet_text_at 	* query;
			struct hllapi_packet_result	  	  response;
			DWORD							  cbSize		= sizeof(struct hllapi_packet_text_at)+strlen((const char *) str);

			query = (struct hllapi_packet_text_at *) malloc(cbSize);
			query->packet_id 	= HLLAPI_PACKET_SET_TEXT_AT;
			query->row			= row;
			query->col			= col;
			strcpy(query->text,(const char *) str);

			TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, sizeof(response), &cbSize,NULL);

			free(query);

			return response.rc;

#elif defined(HAVE_DBUS)

			dbus_int32_t r = (dbus_int32_t) row;
			dbus_int32_t c = (dbus_int32_t) col;

			return query_intval("setTextAt", DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);

#else

			return -1;

#endif

		}

		int cmp_text_at(int row, int col, const char *text)
		{
#if defined(WIN32)

			struct hllapi_packet_text_at 	* query;
			size_t							  cbSize		= sizeof(struct hllapi_packet_text_at)+strlen(text);

			query = (struct hllapi_packet_text_at *) malloc(cbSize);
			query->packet_id 	= HLLAPI_PACKET_CMP_TEXT_AT;
			query->row			= row;
			query->col			= col;
			strcpy(query->text,text);

			return query_intval((void *) query, cbSize, true);

#elif defined(HAVE_DBUS)

			dbus_int32_t r = (dbus_int32_t) row;
			dbus_int32_t c = (dbus_int32_t) col;

			return query_intval("cmpTextAt", DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);

#endif

			return 0;
		}

		int wait_for_text_at(int row, int col, const char *key, int timeout)
		{
			time_t end = time(0)+timeout;

			while(time(0) < end)
			{
				if(!is_connected())
					return ENOTCONN;

				if(!cmp_text_at(row,col,key))
					return 0;

#ifdef WIN32
				Sleep(500);
#else
				usleep(500);
#endif
			}

			return ETIMEDOUT;
		}

		string * get_text(int baddr, size_t len)
		{
#if defined(WIN32)
			struct hllapi_packet_query_offset query = { HLLAPI_PACKET_GET_TEXT_AT_OFFSET, (unsigned short) baddr, (unsigned short) len };
			return query_string(&query,sizeof(query),len);

#elif defined(HAVE_DBUS)

			dbus_int32_t b = (dbus_int32_t) baddr;
			dbus_int32_t l = (dbus_int32_t) len;

			DBusMessage * msg = create_message("getText");
			if(!msg)
				return NULL;

			trace("%s(%d,%d)",__FUNCTION__,b,l);
			dbus_message_append_args(msg, DBUS_TYPE_INT32, &b, DBUS_TYPE_INT32, &l, DBUS_TYPE_INVALID);

			return get_string(call(msg));
#else
			throw exception("%s","IPC support is unavailable");
			return NULL;
#endif
		}


		int set_cursor_position(int row, int col)
		{
#if defined(WIN32)

			struct hllapi_packet_cursor query = { HLLAPI_PACKET_SET_CURSOR_POSITION, (unsigned short) row, (unsigned short) col };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t r = (dbus_int32_t) row;
			dbus_int32_t c = (dbus_int32_t) col;

			return query_intval("setCursorAt", DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_INVALID);

#endif

			return -1;
		}

		int set_cursor_addr(int addr)
		{
#if defined(WIN32)

			struct hllapi_packet_addr query = { HLLAPI_PACKET_SET_CURSOR, (unsigned short) addr };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) addr;

			return query_intval("setCursorAddress", DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);

#endif

			return -1;
		}

		int get_cursor_addr(void)
		{
			return query_intval(HLLAPI_PACKET_GET_CURSOR);
		}


		int enter(void)
		{
			return query_intval(HLLAPI_PACKET_ENTER);
		}

		int pfkey(int key)
		{
#if defined(WIN32)

			struct hllapi_packet_keycode query = { HLLAPI_PACKET_PFKEY, (unsigned short) key };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) key;

			return query_intval("pfKey", DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);

#else

			return -1;

#endif

		}

		int pakey(int key)
		{
#if defined(WIN32)

			struct hllapi_packet_keycode query = { HLLAPI_PACKET_PAKEY, (unsigned short) key };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) key;

			return query_intval("paKey", DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);

#else

			return -1;

#endif

		}

		int quit(void)
		{
			return query_intval(HLLAPI_PACKET_QUIT);
		}

		int set_toggle(LIB3270_TOGGLE ix, bool value)
		{
#if defined(WIN32)

			struct hllapi_packet_set query = { HLLAPI_PACKET_SET_TOGGLE, (unsigned short) ix, (unsigned short) value };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t i = (dbus_int32_t) ix;
			dbus_int32_t v = (dbus_int32_t) value;

			return query_intval("setToggle", DBUS_TYPE_INT32, &i, DBUS_TYPE_INT32, &v, DBUS_TYPE_INVALID);

#else
			return -1;

#endif

		}

		int emulate_input(const char *str)
		{
#if defined(WIN32)

			size_t                                len           = strlen(str);
			struct hllapi_packet_emulate_input 	* query;
			size_t							      cbSize		= sizeof(struct hllapi_packet_emulate_input)+len;

			query = (struct hllapi_packet_emulate_input *) malloc(cbSize);
			query->packet_id 	= HLLAPI_PACKET_EMULATE_INPUT;
			query->len			= len;
			query->pasting		= 1;
			strcpy(query->text,str);

			return query_intval((void *) query, cbSize, true);

#elif defined(HAVE_DBUS)

			return query_intval("input", DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
#else

			return -1;

#endif

		}

		int get_field_start(int baddr)
		{
#if defined(WIN32)

			struct hllapi_packet_addr query = { HLLAPI_PACKET_FIELD_START, (unsigned short) baddr };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) baddr;

			return query_intval("getFieldStart", DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);

#else

			return -1;

#endif

		}

		int get_field_len(int baddr)
		{
#if defined(WIN32)

			struct hllapi_packet_addr query = { HLLAPI_PACKET_FIELD_LEN, (unsigned short) baddr };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) baddr;

			return query_intval("getFieldLength", DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);

#else

			return -1;

#endif
		}

		int get_next_unprotected(int baddr)
		{
#if defined(WIN32)

			struct hllapi_packet_addr query = { HLLAPI_PACKET_NEXT_UNPROTECTED, (unsigned short) baddr };

			return query_intval((void *) &query, sizeof(query));

#elif defined(HAVE_DBUS)

			dbus_int32_t k = (dbus_int32_t) baddr;

			DBusMessage * msg = create_message("getNextUnprotected");
			if(msg)
			{
				dbus_message_append_args(msg, DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);
				return get_intval(call(msg));
			}

			return -1;

#else

			return -1;

#endif

		}

#if defined(HAVE_DBUS)
		string * get_clipboard(void)
		{
			return query_string("getClipboard");
		}

		int set_clipboard(const char *text)
		{
			return query_intval("setClipboard", DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
		}

		int set_host_charset(const char *charset)
		{
			return query_intval("setHostCharset", DBUS_TYPE_STRING, &charset, DBUS_TYPE_INVALID);
		}

		string * get_host_charset(void)
		{
			return query_string("getHostCharset");
		}

		string * get_display_charset(void)
		{
			return query_string("getDisplayCharset");
		}

		int popup_dialog(LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, ...)
		{
			DBusMessage * msg = dbus_message_new_method_call(	this->dest,		// Destination
																this->path,		// Path
																this->intf,		// Interface
																"showPopup");	// method

			if (!msg)
			{
				throw exception("%s","Error creating DBUS message for popup");
				return -1;
			}
			else
			{
				char			  text[4096];
				char			* ptr = text;
				va_list			  arg_ptr;
				dbus_int32_t	  i = (dbus_int32_t) id;

				va_start(arg_ptr, fmt);
				vsnprintf(text,4095,fmt,arg_ptr);
				va_end(arg_ptr);

				if(!dbus_message_append_args(msg, DBUS_TYPE_INT32, &i, DBUS_TYPE_STRING, &title, DBUS_TYPE_STRING, &message, DBUS_TYPE_STRING, &ptr, DBUS_TYPE_INVALID))
				{
					dbus_message_unref(msg);
					throw exception("%s","Cant append args for popup message");
				}
				else
				{
					DBusMessage             * reply;
					DBusError                 error;

					dbus_error_init(&error);
					reply = dbus_connection_send_with_reply_and_block(conn,msg,DBUS_TIMEOUT_INFINITE,&error);
					dbus_message_unref(msg);

					if(!reply)
					{
						exception e = exception("%s",error.message);
						dbus_error_free(&error);
						throw e;
						return -1;
					}

					return get_intval(reply);

				}
			}
			return 0;
		}

#endif // HAVE_DBUS

 	};

	session	* session::create_remote(const char *session)
	{
		return new remote(session);
	}

 }