remote.cc 34.5 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 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
/*
 * "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 <config.h>
 #include <iostream>

 #if defined(HAVE_DBUS)
	#include <stdio.h>
	#include <dbus/dbus.h>
	#include <string.h>
	#include <malloc.h>
	#include <sys/types.h>
	#include <limits.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>
	#include <process.h>
 #else
	#include <unistd.h>

	#define HLLAPI_PACKET_IS_CONNECTED			"isConnected"
	#define HLLAPI_PACKET_GET_CSTATE			"getConnectionState"
	#define HLLAPI_PACKET_GET_PROGRAM_MESSAGE	"getProgramMessage"
	#define HLLAPI_PACKET_GET_SSL_STATE			"getSecureState"
	#define HLLAPI_PACKET_IS_READY				"isReady"
	#define HLLAPI_PACKET_DISCONNECT			"disconnect"
	#define HLLAPI_PACKET_GET_HOST				"getURL"
	#define HLLAPI_PACKET_SET_HOST				"setURL"
	#define HLLAPI_PACKET_GET_CURSOR			"getCursorAddress"
	#define HLLAPI_PACKET_GET_WIDTH				"getScreenWidth"
	#define HLLAPI_PACKET_GET_HEIGHT			"getScreenHeight"
	#define HLLAPI_PACKET_GET_LENGTH			"getScreenLength"
	#define HLLAPI_PACKET_ENTER					"enter"
	#define HLLAPI_PACKET_QUIT					"quit"
	#define HLLAPI_PACKET_ERASE					"erase"
	#define HLLAPI_PACKET_ERASE_EOF				"eraseEOF"
	#define HLLAPI_PACKET_ERASE_EOL				"eraseEOL"
	#define HLLAPI_PACKET_ERASE_INPUT			"eraseInput"
	#define HLLAPI_PACKET_PRINT					"print"
	#define HLLAPI_PACKET_ASC2EBC				"asc2ebc"
	#define HLLAPI_PACKET_EBC2ASC				"ebc2asc"
	#define HLLAPI_PACKET_SET_UNLOCK_DELAY		"setUnlockDelay"
 #endif // _WIN32

 #include <pw3270cpp.h>
 #include <lib3270/log.h>

 #include "private.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");
		}

		int query_strval(HLLAPI_PACKET id, unsigned char *buffer, size_t sz)
		{
			DWORD								  cbSize	= sizeof(struct hllapi_packet_text)+sz;
			struct hllapi_packet_text			* query;
			struct hllapi_packet_text			* response;
			int 								  rc		= -1;

			query = (struct hllapi_packet_text *) malloc(cbSize+2);
			memset(query,0,cbSize+2);
			query->packet_id = id;
			memcpy(query->text,buffer,sz);

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

			if(TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, cbSize, &cbSize,NULL))
			{
				if(response->packet_id)
				{
					rc = response->packet_id;
				}
				else
				{
					rc = 0;
					strncpy((char *) buffer,response->text,sz);
				}
			}

			free(response);
			free(query);

			return rc;

		}

		string query_string(void *query, size_t szQuery, size_t len)
		{
			struct hllapi_packet_text	* response;
			DWORD 						  sz		= sizeof(struct hllapi_packet_text)+len;
			DWORD						  cbSize	= (DWORD) sz;
			string						  s;
			char						  buffer[sz+2];

			memset(buffer,0,sz+2);

			response = (struct hllapi_packet_text *) buffer;

			if(TransactNamedPipe(hPipe,(LPVOID) query, szQuery, response, sz, &cbSize,NULL))
			{
				buffer[min(cbSize,sz)] = 0;

				trace("TransactNamedPipe call %d returns \"%s\"",(int) *( (unsigned char *) query), response->text);

				if(!response->packet_id)
					s.assign(response->text);
			}
			else
			{
				trace("TransactNamedPipe error on call %d",(int) *( (unsigned char *) query));
				s.assign("");
			}

			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");

		}

		void set_intval(HLLAPI_PACKET id, int value)
		{
			struct hllapi_packet_set_int	packet;
			DWORD							cbSize		= (DWORD) sizeof(packet);
			BOOL 							status;

			memset(&packet,0,sizeof(packet));
			packet.packet_id	= id;
			packet.value		= value;

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

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

		}


#elif defined(HAVE_DBUS)

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

		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)
		{
			string rc;

			if(msg)
			{
				DBusMessageIter iter;

				if(dbus_message_iter_init(msg, &iter))
				{
					if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
					{
						const char	* str;
						dbus_message_iter_get_basic(&iter, &str);
						trace("Response: [%s]",str);
						rc.assign(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 rc;
		}

		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));
		}

		int query_strval(const char *method, unsigned char *buffer, size_t sz)
		{
			DBusMessage * outMsg = create_message(method);

			if(outMsg)
			{
				dbus_message_append_args(outMsg, DBUS_TYPE_STRING, &buffer, DBUS_TYPE_INVALID);

				DBusMessage * rspMsg = call(outMsg);
				if(rspMsg)
				{
					DBusMessageIter iter;

					if(dbus_message_iter_init(rspMsg, &iter))
					{
						if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
						{
							const char	* str;
							dbus_message_iter_get_basic(&iter, &str);
							trace("Response: [%s]",str);
							strncpy((char *) buffer,str,sz);
							dbus_message_unref(rspMsg);
							return 0;
						}

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

						throw e;

					}
				}
			}

			return -1;
		}

		void set_intval(const char *method, int value)
		{
			DBusMessage * outMsg = create_message(method);

			if(outMsg)
			{
				dbus_int32_t v = (dbus_int32_t) value;

				dbus_message_append_args(outMsg, DBUS_TYPE_INT32, &v, DBUS_TYPE_INVALID);

				DBusMessage * rspMsg = call(outMsg);
				dbus_message_unref(rspMsg);

			}
		}

#else


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

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


#endif

	public:

#if defined(HAVE_DBUS)
		const char * makeBusName(string &name)
		{
			int val;

			// First uses the object ID
			val = this->sequence;
			while(val > 0)
			{
				char str[] = { (char) ('a'+(val % 25)), 0 };
				name.append(str);
				val /= 25;
			}
			name.append(".");

			val = (int) getpid();
			while(val > 0)
			{
				#pragma GCC diagnostic push
				#pragma GCC diagnostic ignored "-Wnarrowing"
				char str[] = { 'a'+(val % 25), 0 };
				#pragma GCC diagnostic pop
				name.append(str);
				val /= 25;
			}
			name.append(".");

			// And last, the project info
			name.append(intf);

			trace("Busname=\"%s\" sequence=%d this=%p",name.c_str(),sequence,this);

			return name.c_str();

		}
#endif // HAVE_DBUS

#if defined(_WIN32)

		static string getRegistryKey(const char *name) throw (std::exception)
		{
			char 					buffer[4096];
			HKEY 					hKey	= 0;
			unsigned long			datalen = sizeof(buffer);

			debug("%s(%s)",__FUNCTION__,name);

			*buffer = 0;
			if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS)
			{
				throw exception("Can't open key %s","HKLM\\Software\\pw3270");
			}
			else
			{
				unsigned long datatype;	// #defined in winnt.h (predefined types 0-11)

				if(RegQueryValueExA(hKey,name,NULL,&datatype,(LPBYTE) buffer,&datalen) != ERROR_SUCCESS)
					*buffer = 0;
				RegCloseKey(hKey);
			}

			return string(buffer);

		}

#endif // defined

		remote(const char *session)
		{
#if defined(_WIN32)
			static DWORD			  dwMode = PIPE_READMODE_MESSAGE;
			char	 				  buffer[4096];
			char					* str;
			char					* ptr;
			time_t					  timer	= time(0)+1;

			hPipe  = INVALID_HANDLE_VALUE;

			trace("%s(%s)",__FUNCTION__,session);

			if(strcasecmp(session,"start") == 0 || strcasecmp(session,"new") == 0)
			{
				// Start a new session
				string					appName = getRegistryKey("appName");
				char 					buffer[80];
				STARTUPINFO				si;
				PROCESS_INFORMATION		pi;

				// Get application path

				if(!appName.size())
				{
					throw exception("key %s\\appName is invalid","HKLM\\Software\\pw3270");
					return;
				}

				trace("%s appname=%s\n",__FUNCTION__,appName.c_str());

				snprintf(buffer,79,"%s --session=\"H%06d\"",appName.c_str(),getpid());

				ZeroMemory( &si, sizeof(si) );
				si.cb = sizeof(si);
				ZeroMemory( &pi, sizeof(pi) );

				// si.dwFlags = STARTF_PREVENTPINNING;
				trace("App: %s",appName.c_str());
				trace("CmdLine: %s",buffer);

				if(CreateProcess(NULL,buffer,NULL,NULL,0,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
				{
					CloseHandle( pi.hProcess );
					CloseHandle( pi.hThread );
				}
				else
				{
					throw exception("Can't start %s",appName.c_str());
					return;
				}

				snprintf(buffer,4095,"H%06d_a",getpid());
				str = strdup(buffer);

				// Até 20 segundos para o processo iniciar.
				timer = time(0)+20;

			}
			else
			{
				// Use an existing session
				str = strdup(session);

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

				// Wait?
				int delay;

				try
				{
					delay = atoi(getRegistryKey("hllapiWait").c_str());
				}
				catch(std::exception &e)
				{
					delay = 0;
				}

				if(delay) {
					timer = time(0) + delay;
				}


			}

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

			free(str);

			trace("Searching for \"%s\"",buffer);

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

			if(hPipe == INVALID_HANDLE_VALUE)
			{
				// Cant get session, wait.
				while(hPipe == INVALID_HANDLE_VALUE && time(0) < timer)
				{
					hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
					Sleep(1);
				}
			}

			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)

			static int	  sq	= 0;
			DBusError	  err;
			int			  rc;
			char		* str = strdup(session);
			char		* ptr;
			string		  busName;


			this->path = nullptr;
			this->intf = nullptr;
			this->sequence = (++sq) + time(0);

			try {

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

				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);

					// Path and interface always use the same name.
					path = strdup("/br/com/bb/pw3270");
					intf = strdup("br.com.bb.pw3270");

				}
				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;
				}


				rc = dbus_bus_request_name(conn, makeBusName(busName), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
				trace("dbus_bus_request_name(%s) rc=%d",busName.c_str(),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.c_str());
					throw exception("DBUS request for \"%s\" failed",session);
				}

				trace("%s: Using DBUS name %s",__FUNCTION__,busName.c_str());

				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);

			} catch(...) {

				if(path) {
					free(path);
					path = nullptr;
				}

				if(intf) {
					free(intf);
					intf = nullptr;
				}

			}

#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)
			{
				std::cerr << e.what();
			}

			string busName;
			makeBusName(busName);

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

			DBusError	err;

			dbus_error_init(&err);
			dbus_bus_release_name(conn,busName.c_str(),&err);

			if (dbus_error_is_set(&err))
			{
				//exception e = exception("Error when releasing DBUS name (%s)", err.message);
				std::cerr << err.message;
				dbus_error_free(&err);
				//throw e;
			}

#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);
		}

		LIB3270_MESSAGE get_program_message(void) {
			return (LIB3270_MESSAGE) query_intval(HLLAPI_PACKET_GET_PROGRAM_MESSAGE);
		}

		LIB3270_SSL_STATE get_secure(void) {
			return (LIB3270_SSL_STATE) query_intval(HLLAPI_PACKET_GET_SSL_STATE);
		}

		virtual int connect()
		{
			return connect("",0);
		}

		virtual int connect(const char *url, time_t wait)
		{
			int rc = EINVAL;

			debug("%s(%s,%u)",__FUNCTION__,url,(unsigned int) wait);

			if(!url) {
				url = "";
			}

#if defined(_WIN32)

			size_t						  cbSize	= sizeof(struct hllapi_packet_query) + strlen(url) + 1;
			struct hllapi_packet_query	* pkt		= (struct hllapi_packet_query *) malloc(cbSize);

			memset(pkt,0,cbSize);

			pkt->packet_id = HLLAPI_PACKET_CONNECT_URL;

			strcpy(((char *) (pkt+1)), url);

			rc = query_intval((void *) pkt, cbSize, true);

			if(!rc && wait) {
				time_t end = time(0) + wait;
				while(!is_connected()) {
					if(time(0) > end) {
						debug("%s: Timeout",__FUNCTION__);
						return ETIMEDOUT;
					}
					Sleep(500);
				}
			}

#elif defined(HAVE_DBUS)

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

			debug("connect(%s) rc=%d (%s)",url,rc,strerror(rc));

			if(!rc && wait) {
				time_t end = time(0) + wait;
				while(!is_connected()) {
					if(time(0) > end) {
						debug("%s: Timeout",__FUNCTION__);
						return ETIMEDOUT;
					}
					usleep(500);
				}
			}

#endif
			debug("connect(%s) rc=%d (%s)",url,rc,strerror(rc));

			return rc;

		}

		int set_url(const char *uri)
		{
			int rc;

#if defined(_WIN32)

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

			pkt->packet_id	= HLLAPI_PACKET_SET_HOST;
			strcpy(pkt->text,uri);

			rc = query_intval((void *) pkt,cbSize,true);

#elif defined(HAVE_DBUS)

			rc = query_intval(HLLAPI_PACKET_SET_HOST, DBUS_TYPE_STRING, &uri, DBUS_TYPE_INVALID);

#else

			rc = -1;

#endif

			return rc;

		}

		string get_url()
		{
#if defined(_WIN32)

			struct hllapi_packet_query query	= { HLLAPI_PACKET_GET_HOST };
			return query_string(&query,sizeof(query),1024);

#elif defined(HAVE_DBUS)

			return query_string(HLLAPI_PACKET_GET_HOST);

#else
			return string();
#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)
			{
				if(!is_connected())
					return ENOTCONN;

				if(is_ready())
					return 0;

				sleep(1);
			}

			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)
			if(wait)
				Sleep(250);
			return 0;
#elif defined(HAVE_DBUS)
			if(wait)
				sleep(1);
			return 0;
#else
			return -1;
#endif
		}

		string get_text_at(int row, int col, size_t sz, bool lf)
		{
#if defined(_WIN32)

			struct hllapi_packet_query_at query	= { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz, lf ? '\n' : 0 };

			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;
			unsigned char d = lf ? '\n' : 0;

			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_BYTE, &d, DBUS_TYPE_INVALID);

			return get_string(call(msg));

#else

			return string();

#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, bool lf)
		{
			debug("%s(%d,%d,\"%s\")",__FUNCTION__,row,col,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;
			query->lf 			= lf ? '\n' : 0;
			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;
			unsigned char d = lf ? '\n' : 0;

			return query_intval("cmpTextAt", DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &text, DBUS_TYPE_BYTE, &d, 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,false))
					return 0;

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

			return ETIMEDOUT;
		}

		string get_text(int baddr, size_t len, bool lf)
		{
#if defined(_WIN32)

			struct hllapi_packet_query_offset query = { HLLAPI_PACKET_GET_TEXT_AT_OFFSET, (unsigned short) baddr, (unsigned short) len, lf ? '\n' : 0 };
			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;
			unsigned char d = lf ? '\n' : 0;

			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_BYTE, &d, DBUS_TYPE_INVALID);

			return get_string(call(msg));
#else
			throw exception("%s","IPC support is unavailable");
			return string();
#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 get_width(void) {
			return query_intval(HLLAPI_PACKET_GET_WIDTH);
		}

		int get_height(void) {
			return query_intval(HLLAPI_PACKET_GET_HEIGHT);
		}

		int	get_length(void) {
			return query_intval(HLLAPI_PACKET_GET_LENGTH);
		}

		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 action(const char *str)
		{

#if defined(_WIN32)

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

			query = (struct hllapi_packet_text *) malloc(cbSize);
			query->packet_id 	= HLLAPI_PACKET_ACTION;
			strcpy(query->text,str);

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

#elif defined(HAVE_DBUS)

			return query_intval("action", 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

		}

		int get_is_protected(int baddr)
		{
#if defined(_WIN32)

			struct hllapi_packet_addr query = { HLLAPI_PACKET_IS_PROTECTED, (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("getIsProtected");
			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

		}

		int get_is_protected_at(int row,int col)
		{
#if defined(_WIN32)

			struct hllapi_packet_query_at query	= { HLLAPI_PACKET_IS_PROTECTED_AT, (unsigned short) row, (unsigned short) col, 0 };

			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;

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

			return -1;

#else

			return -1;

#endif

		}


		int set_host_charset(const char *charset)
		{
#if defined(_WIN32)

			size_t							  len		= strlen(charset);
			struct hllapi_packet_set_text 	* query;
			size_t							  cbSize	= sizeof(struct hllapi_packet_set_text)+len;

			query = (struct hllapi_packet_set_text *) malloc(cbSize);
			query->packet_id 	= HLLAPI_PACKET_SET_HOST_CHARSET;
			query->len			= len;
			strcpy(query->text,charset);

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

#elif defined(HAVE_DBUS)

			return query_intval("setHostCharset", DBUS_TYPE_STRING, &charset, DBUS_TYPE_INVALID);

#else
			return -1;
#endif
		}

		string get_host_charset(void)
		{
#if defined(_WIN32)

			struct hllapi_packet_query query = { HLLAPI_PACKET_GET_HOST_CHARSET };
			return query_string(&query,sizeof(query),100);

#elif defined(HAVE_DBUS)

			return query_string("getHostCharset");

#else

			return NULL;

#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);
		}

		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

		int	erase(void)
		{
			return query_intval(HLLAPI_PACKET_ERASE);
		}

		int	erase_eof(void)
		{
			return query_intval(HLLAPI_PACKET_ERASE_EOF);
		}

		int	erase_eol(void)
		{
			return query_intval(HLLAPI_PACKET_ERASE_EOL);
		}

		int	erase_input(void)
		{
			return query_intval(HLLAPI_PACKET_ERASE_INPUT);
		}

		int	print(void)
		{
			return query_intval(HLLAPI_PACKET_PRINT);
		}

		const char * asc2ebc(unsigned char *text, int sz)
		{
			query_strval(HLLAPI_PACKET_ASC2EBC,text,sz);
			return (const char *) text;
		}

		const char * ebc2asc(unsigned char *text, int sz)
		{
			query_strval(HLLAPI_PACKET_EBC2ASC,text,sz);
			return (const char *) text;
		}

		void set_unlock_delay(unsigned short ms)
		{
			set_intval(HLLAPI_PACKET_SET_UNLOCK_DELAY,(int) ms);
		}

 	};

	session	* session::create_remote(const char *session)
	{
		debug("create(%s)",session);

		if(strncasecmp(session,"service://",10)) {
			return new remote(session);
		}

#ifdef HAVE_DBUS
		return create_service_client(session+10);
#else
		throw exception("Can't create session \"%s\"",session);
#endif // HAVE_DBUS

	}

 }