aboutsummaryrefslogtreecommitdiff
path: root/src/gatscon/proxywidget.cpp
blob: 215f95f1c37f4c58d9b80b5f0a1a562bf543a408 (plain)
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
#include "proxywidget.h"
#include "proxythread.h"

#include "gatstotree.h"
#include "treetogats.h"

#include <QMessageBox>

#include <gats/gatsstream.h>
#include <gats/types.h>
#include <bu/sio.h>
#include <bu/file.h>

using namespace Bu;

ProxyWidget::ProxyWidget( QWidget *pParent, int iPortIn,
		const QByteArray baHost, int iPortOut ) :
	QWidget( pParent ),
	pPrx( NULL )
{
	setupUi( this );

	pPrx = new ProxyThread( this, iPortIn, baHost, iPortOut );

	connect( pPrx, SIGNAL(gotConnection()),
		this, SLOT(gotConnection()), Qt::QueuedConnection );
	connect( pPrx, SIGNAL(recv( Gats::Object *)),
		this, SLOT(clientRecv(Gats::Object *)), Qt::QueuedConnection );
	connect( pPrx->pHost, SIGNAL(recv( Gats::Object *)),
		this, SLOT(hostRecv(Gats::Object *)), Qt::QueuedConnection );

	pPrx->start();
}

ProxyWidget::~ProxyWidget()
{
}

void ProxyWidget::saveTo( const QString &sFile )
{
	File fOut( sFile.toAscii().constData(), File::WriteNew );
	Gats::GatsStream gsOut( fOut );
	QTreeWidgetItem *pRoot = twHistory->invisibleRootItem();
	for( int j = 0; j < pRoot->childCount(); j++ )
	{
		Gats::Object *pObj = treeToGats( pRoot->child( j ) );
		gsOut.writeObject( pObj );
		delete pObj;
	}
}

void ProxyWidget::sendToClient()
{
	try
	{
		Gats::Object *pObj = Gats::Object::strToGats(
			leGats->text().toAscii().constData()
			);
		sio << "Send: " << *pObj << sio.nl;
		QTreeWidgetItem *pIt = new QTreeWidgetItem(
			twHistory->invisibleRootItem()
			);
		pIt->setText( 0, "proxy -> client" );
		gatsToTree( pIt, pObj );
		pPrx->send( pObj );
		delete pObj;

		leGats->setText("");
		leGats->setFocus();
	}
	catch( Bu::ExceptionBase &e )
	{
		QMessageBox::critical( this, "Gats Console - Error", e.what() );
	}
}

void ProxyWidget::sendToServer()
{
	try
	{
		Gats::Object *pObj = Gats::Object::strToGats(
			leGats->text().toAscii().constData()
			);
		sio << "Send: " << *pObj << sio.nl;
		QTreeWidgetItem *pIt = new QTreeWidgetItem(
			twHistory->invisibleRootItem()
			);
		pIt->setText( 0, "proxy -> host" );
		gatsToTree( pIt, pObj );
		pPrx->pHost->send( pObj );
		delete pObj;

		leGats->setText("");
		leGats->setFocus();
	}
	catch( Bu::ExceptionBase &e )
	{
		QMessageBox::critical( this, "Gats Console - Error", e.what() );
	}
}

void ProxyWidget::clientRecv( Gats::Object *pObj )
{
	sio << "Recv: " << *pObj << sio.nl;

	QTreeWidgetItem *pIt = new QTreeWidgetItem(
		twHistory->invisibleRootItem()
		);
	pIt->setText( 0, "client -> host" );
	gatsToTree( pIt, pObj );
	delete pObj;
}

void ProxyWidget::hostRecv( Gats::Object *pObj )
{
	sio << "Recv: " << *pObj << sio.nl;

	QTreeWidgetItem *pIt = new QTreeWidgetItem(
		twHistory->invisibleRootItem()
		);
	pIt->setText( 0, "host -> client" );
	gatsToTree( pIt, pObj );
	delete pObj;
}

void ProxyWidget::gotConnection()
{
	lwConnect->stop();
	swRoot->setCurrentIndex( 1 );
}