aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-05-17 01:24:51 +0000
committerMike Buland <eichlan@xagasoft.com>2011-05-17 01:24:51 +0000
commitefcbdb7a0347b4399cbabacf3cbea432eeafb17b (patch)
treed468a9d8e129cba0cef68a09421c0e21c720ede6 /src
parent02c60c6720f41bcfc367d02ae4c655b651642991 (diff)
downloadlibgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.gz
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.bz2
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.xz
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.zip
Gats::Object now has a strToGats function, it's pretty slick, it takes a string
and produces a fully formed gats tree. Also, gatscon now can interact with a server directly.
Diffstat (limited to '')
-rw-r--r--src/gatscon/clientthread.cpp38
-rw-r--r--src/gatscon/clientthread.h37
-rw-r--r--src/gatscon/clientwidget.cpp67
-rw-r--r--src/gatscon/clientwidget.h13
-rw-r--r--src/gatscon/clientwidget.ui33
-rw-r--r--src/gatscon/gatstotree.cpp75
-rw-r--r--src/gatscon/gatstotree.h19
-rw-r--r--src/object.cpp192
-rw-r--r--src/object.h8
9 files changed, 465 insertions, 17 deletions
diff --git a/src/gatscon/clientthread.cpp b/src/gatscon/clientthread.cpp
new file mode 100644
index 0000000..b1dc4d0
--- /dev/null
+++ b/src/gatscon/clientthread.cpp
@@ -0,0 +1,38 @@
1#include "clientthread.h"
2
3#include <bu/tcpsocket.h>
4
5ClientThread::ClientThread( QObject *pParent, const QByteArray &baHost,
6 int iPort ) :
7 QThread( pParent ),
8 baHost( baHost ),
9 iPort( iPort ),
10 gsCli( ssCli )
11{
12}
13
14ClientThread::~ClientThread()
15{
16}
17
18void ClientThread::send( Gats::Object *pObj )
19{
20 gsCli.writeObject( pObj );
21}
22
23void ClientThread::run()
24{
25 ssCli.setStream(
26 new Bu::TcpSocket( baHost.constData(), iPort )
27 );
28
29 while( !ssCli.isEos() )
30 {
31 Gats::Object *pObj = gsCli.readObject();
32 if( pObj == NULL )
33 break;
34
35 emit recv( pObj );
36 }
37}
38
diff --git a/src/gatscon/clientthread.h b/src/gatscon/clientthread.h
new file mode 100644
index 0000000..3182d37
--- /dev/null
+++ b/src/gatscon/clientthread.h
@@ -0,0 +1,37 @@
1#ifndef CLIENT_THREAD_H
2#define CLIENT_THREAD_H
3
4#include <QThread>
5#include <QByteArray>
6
7#include <bu/streamstack.h>
8#include <gats/gatsstream.h>
9
10namespace Gats
11{
12 class Object;
13};
14
15class ClientThread : public QThread
16{
17 Q_OBJECT;
18public:
19 ClientThread( QObject *pParent, const QByteArray &baHost, int iPort );
20 virtual ~ClientThread();
21
22 void send( Gats::Object *pObj );
23
24signals:
25 void recv( Gats::Object *pObj );
26
27protected:
28 virtual void run();
29
30private:
31 QByteArray baHost;
32 int iPort;
33 Bu::StreamStack ssCli;
34 Gats::GatsStream gsCli;
35};
36
37#endif
diff --git a/src/gatscon/clientwidget.cpp b/src/gatscon/clientwidget.cpp
index 4872d9d..6039035 100644
--- a/src/gatscon/clientwidget.cpp
+++ b/src/gatscon/clientwidget.cpp
@@ -1,24 +1,73 @@
1#include "clientwidget.h" 1#include "clientwidget.h"
2#include "connectdlg.h" 2#include "connectdlg.h"
3#include "clientthread.h"
4
5#include "gatstotree.h"
6
7#include <QMessageBox>
8
9#include <bu/tcpsocket.h>
10#include <bu/sio.h>
11
12using namespace Bu;
3 13
4ClientWidget::ClientWidget( QWidget *pParent ) : 14ClientWidget::ClientWidget( QWidget *pParent ) :
5 QWidget( pParent ), 15 QWidget( pParent )
6 gsCli( ssCli )
7{ 16{
8 setupUi( this ); 17 setupUi( this );
9 18
10 ConnectDlg dlg( this ); 19 {
11 dlg.exec(); 20 ConnectDlg dlg( this );
21 dlg.exec();
12 22
13 ssCli.setStream( 23 sio << "Connect: " << dlg.getHostname().constData() << ":"
14 new Bu::TcpSocket( dlg.getHost().constData(), dlg.getPort() ) 24 << dlg.getPort() << sio.nl;
15 );
16 25
17 Gats::Object *pObj = gsCli.readObject(); 26 pCli = new ClientThread( this, dlg.getHostname(), dlg.getPort() );
18 gatsToTree( twHistory, pObj ); 27 }
28 pCli->start();
29 connect( pCli, SIGNAL(recv( Gats::Object *)),
30 this, SLOT(recv(Gats::Object *)), Qt::QueuedConnection );
19} 31}
20 32
21ClientWidget::~ClientWidget() 33ClientWidget::~ClientWidget()
22{ 34{
23} 35}
24 36
37void ClientWidget::send()
38{
39 try
40 {
41 Gats::Object *pObj = Gats::Object::strToGats(
42 leGats->text().toAscii().constData()
43 );
44 sio << "Send: " << *pObj << sio.nl;
45 QTreeWidgetItem *pIt = new QTreeWidgetItem(
46 twHistory->invisibleRootItem()
47 );
48 pIt->setText( 0, "send" );
49 gatsToTree( pIt, pObj );
50 pCli->send( pObj );
51 delete pObj;
52
53 leGats->setText("");
54 leGats->setFocus();
55 }
56 catch( Bu::ExceptionBase &e )
57 {
58 QMessageBox::critical( this, "GatsCon - Error", e.what() );
59 }
60}
61
62void ClientWidget::recv( Gats::Object *pObj )
63{
64 sio << "Recv: " << *pObj << sio.nl;
65
66 QTreeWidgetItem *pIt = new QTreeWidgetItem(
67 twHistory->invisibleRootItem()
68 );
69 pIt->setText( 0, "recv" );
70 gatsToTree( pIt, pObj );
71 delete pObj;
72}
73
diff --git a/src/gatscon/clientwidget.h b/src/gatscon/clientwidget.h
index 3a96f07..21fb1b7 100644
--- a/src/gatscon/clientwidget.h
+++ b/src/gatscon/clientwidget.h
@@ -3,8 +3,10 @@
3 3
4#include "ui_clientwidget.h" 4#include "ui_clientwidget.h"
5 5
6#include <bu/streamstack.h> 6namespace Gats
7#include <gats/gatsstream.h> 7{
8 class Object;
9};
8 10
9class ClientWidget : public QWidget, protected Ui::ClientWidget 11class ClientWidget : public QWidget, protected Ui::ClientWidget
10{ 12{
@@ -13,9 +15,12 @@ public:
13 ClientWidget( QWidget *pParent=NULL ); 15 ClientWidget( QWidget *pParent=NULL );
14 virtual ~ClientWidget(); 16 virtual ~ClientWidget();
15 17
18public slots:
19 void send();
20 void recv( Gats::Object *pObj );
21
16private: 22private:
17 Bu::StreamStack ssCli; 23 class ClientThread *pCli;
18 Gats::GatsStream gsCli;
19}; 24};
20 25
21#endif 26#endif
diff --git a/src/gatscon/clientwidget.ui b/src/gatscon/clientwidget.ui
index 9761401..a0cb997 100644
--- a/src/gatscon/clientwidget.ui
+++ b/src/gatscon/clientwidget.ui
@@ -6,8 +6,8 @@
6 <rect> 6 <rect>
7 <x>0</x> 7 <x>0</x>
8 <y>0</y> 8 <y>0</y>
9 <width>364</width> 9 <width>363</width>
10 <height>289</height> 10 <height>291</height>
11 </rect> 11 </rect>
12 </property> 12 </property>
13 <property name="windowTitle"> 13 <property name="windowTitle">
@@ -23,6 +23,11 @@
23 </column> 23 </column>
24 <column> 24 <column>
25 <property name="text"> 25 <property name="text">
26 <string>Type</string>
27 </property>
28 </column>
29 <column>
30 <property name="text">
26 <string>Value</string> 31 <string>Value</string>
27 </property> 32 </property>
28 </column> 33 </column>
@@ -38,7 +43,7 @@
38 </widget> 43 </widget>
39 </item> 44 </item>
40 <item> 45 <item>
41 <widget class="QLineEdit" name="lineEdit"/> 46 <widget class="QLineEdit" name="leGats"/>
42 </item> 47 </item>
43 <item> 48 <item>
44 <widget class="QPushButton" name="pushButton"> 49 <widget class="QPushButton" name="pushButton">
@@ -58,5 +63,25 @@
58 </layout> 63 </layout>
59 </widget> 64 </widget>
60 <resources/> 65 <resources/>
61 <connections/> 66 <connections>
67 <connection>
68 <sender>pushButton</sender>
69 <signal>clicked()</signal>
70 <receiver>ClientWidget</receiver>
71 <slot>send()</slot>
72 <hints>
73 <hint type="sourcelabel">
74 <x>332</x>
75 <y>276</y>
76 </hint>
77 <hint type="destinationlabel">
78 <x>322</x>
79 <y>367</y>
80 </hint>
81 </hints>
82 </connection>
83 </connections>
84 <slots>
85 <slot>send()</slot>
86 </slots>
62</ui> 87</ui>
diff --git a/src/gatscon/gatstotree.cpp b/src/gatscon/gatstotree.cpp
index af94fa5..d01e4b8 100644
--- a/src/gatscon/gatstotree.cpp
+++ b/src/gatscon/gatstotree.cpp
@@ -1,8 +1,83 @@
1#include "gatstotree.h"
2
1#include <gats/types.h> 3#include <gats/types.h>
2 4
3#include <QTreeWidgetItem> 5#include <QTreeWidgetItem>
4 6
5void gatsToTree( QTreeWidgetItem *p, Gats::Object *pObj ) 7void gatsToTree( QTreeWidgetItem *p, Gats::Object *pObj )
6{ 8{
9 switch( pObj->getType() )
10 {
11 case Gats::typeInteger:
12 gatsToTree( p, dynamic_cast<Gats::Integer *>( pObj ) );
13 break;
14
15 case Gats::typeString:
16 gatsToTree( p, dynamic_cast<Gats::String *>( pObj ) );
17 break;
18
19 case Gats::typeFloat:
20 gatsToTree( p, dynamic_cast<Gats::Float *>( pObj ) );
21 break;
22
23 case Gats::typeBoolean:
24 gatsToTree( p, dynamic_cast<Gats::Boolean *>( pObj ) );
25 break;
26
27 case Gats::typeList:
28 gatsToTree( p, dynamic_cast<Gats::List *>( pObj ) );
29 break;
30
31 case Gats::typeDictionary:
32 gatsToTree( p, dynamic_cast<Gats::Dictionary *>( pObj ) );
33 break;
34 }
35}
36
37void gatsToTree( QTreeWidgetItem *p, Gats::Integer *pObj )
38{
39 p->setText( 1, "int");
40 p->setText( 2, QString("%1").arg( pObj->getValue() ) );
41}
42
43void gatsToTree( QTreeWidgetItem *p, Gats::String *pObj )
44{
45 p->setText( 1, "str");
46 p->setText( 2, QString("%1").arg( pObj->getStr() ) );
47}
48
49void gatsToTree( QTreeWidgetItem *p, Gats::Float *pObj )
50{
51 p->setText( 1, "float");
52 p->setText( 2, QString("%1").arg( pObj->getValue() ) );
53}
54
55void gatsToTree( QTreeWidgetItem *p, Gats::Boolean *pObj )
56{
57 p->setText( 1, "bool");
58 p->setText( 2, pObj->getValue()?"true":"false" );
59}
60
61void gatsToTree( QTreeWidgetItem *p, Gats::List *pObj )
62{
63 p->setText( 1, "list");
64 int j = 0;
65 for( Gats::List::iterator i = pObj->begin(); i; i++ )
66 {
67 QTreeWidgetItem *pIt = new QTreeWidgetItem( p );
68 pIt->setText( 0, QString("%1").arg( j++ ) );
69 gatsToTree( pIt, *i );
70 }
71}
72
73void gatsToTree( QTreeWidgetItem *p, Gats::Dictionary *pObj )
74{
75 p->setText( 1, "dict");
76 for( Gats::Dictionary::iterator i = pObj->begin(); i; i++ )
77 {
78 QTreeWidgetItem *pIt = new QTreeWidgetItem( p );
79 pIt->setText( 0, QString( i.getKey().getStr() ) );
80 gatsToTree( pIt, *i );
81 }
7} 82}
8 83
diff --git a/src/gatscon/gatstotree.h b/src/gatscon/gatstotree.h
index cab9eee..26fb76c 100644
--- a/src/gatscon/gatstotree.h
+++ b/src/gatscon/gatstotree.h
@@ -3,6 +3,25 @@
3 3
4class QTreeWidgetItem; 4class QTreeWidgetItem;
5 5
6namespace Gats
7{
8 class Integer;
9 class String;
10 class Float;
11 class Boolean;
12 class List;
13 class Dictionary;
14 class Object;
15};
16
17#include <gats/types.h>
18
6void gatsToTree( QTreeWidgetItem *p, Gats::Object *pObj ); 19void gatsToTree( QTreeWidgetItem *p, Gats::Object *pObj );
20void gatsToTree( QTreeWidgetItem *p, Gats::Integer *pObj );
21void gatsToTree( QTreeWidgetItem *p, Gats::String *pObj );
22void gatsToTree( QTreeWidgetItem *p, Gats::Float *pObj );
23void gatsToTree( QTreeWidgetItem *p, Gats::Boolean *pObj );
24void gatsToTree( QTreeWidgetItem *p, Gats::List *pObj );
25void gatsToTree( QTreeWidgetItem *p, Gats::Dictionary *pObj );
7 26
8#endif 27#endif
diff --git a/src/object.cpp b/src/object.cpp
index af81017..da77ff8 100644
--- a/src/object.cpp
+++ b/src/object.cpp
@@ -7,9 +7,14 @@
7#include "gats/list.h" 7#include "gats/list.h"
8#include "gats/dictionary.h" 8#include "gats/dictionary.h"
9 9
10#include <stdlib.h>
11
10#include <bu/formatter.h> 12#include <bu/formatter.h>
11#include <bu/stream.h> 13#include <bu/stream.h>
12 14
15#include <bu/sio.h>
16using namespace Bu;
17
13Gats::Object::Object() 18Gats::Object::Object()
14{ 19{
15} 20}
@@ -63,6 +68,193 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn )
63 return pObj; 68 return pObj;
64} 69}
65 70
71void Gats::Object::skipWs( Bu::String::const_iterator &i )
72{
73 for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { }
74}
75
76Bu::String Gats::Object::token( Bu::String::const_iterator &i )
77{
78 Bu::String sRet;
79 if( *i == '\"' )
80 {
81 for( i++; i && *i != '\"' ; i++ )
82 {
83 if( *i == '\\' )
84 i++;
85 sRet += i;
86 }
87 i++;
88 }
89 else
90 {
91 for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' &&
92 *i != ',' && *i != ']' && *i != '}' && *i != '[' &&
93 *i != '{'; i++ )
94 {
95 sRet += i;
96 }
97 }
98
99 return sRet;
100}
101
102Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i )
103{
104 skipWs( i );
105
106 switch( *i )
107 {
108 case '[':
109 {
110 Gats::List *pLst = new Gats::List();
111 i++;
112 for(;;)
113 {
114 Gats::Object *pObj = strToGats( i );
115 if( !pObj )
116 break;
117 pLst->append( pObj );
118 skipWs( i );
119 switch( *i )
120 {
121 case ',':
122 i++;
123 break;
124
125 case ']':
126 i++;
127 return pLst;
128
129 default:
130 throw Bu::ExceptionBase("Invalid character found.");
131 }
132 }
133 }
134 break;
135
136 case '{':
137 {
138 Gats::Dictionary *pDict = new Gats::Dictionary();
139 i++;
140 for(;;)
141 {
142 skipWs( i );
143 if( *i != '\"' )
144 throw Bu::ExceptionBase("Keys must be quoted strings.");
145 Bu::String sKey = token( i );
146 skipWs( i );
147 if( *i != ':' )
148 throw Bu::ExceptionBase("Keys and values must be "
149 "seperated with colons.");
150 i++;
151 Gats::Object *pObj = strToGats( i );
152 if( !pObj )
153 throw Bu::ExceptionBase("No value object found.");
154 pDict->insert( sKey, pObj );
155 skipWs( i );
156 switch( *i )
157 {
158 case ',':
159 i++;
160 break;
161
162 case '}':
163 i++;
164 return pDict;
165
166 default:
167 throw Bu::ExceptionBase("Invalid character found.");
168 }
169 }
170 }
171 break;
172
173 case '\"':
174 return new Gats::String( token( i ) );
175 break;
176
177 case '0':
178 case '1':
179 case '2':
180 case '3':
181 case '4':
182 case '5':
183 case '6':
184 case '7':
185 case '8':
186 case '9':
187 case '.':
188 case '+':
189 case '-':
190 {
191 Bu::String s = token( i );
192 int iSize = s.getSize();
193 if( s[iSize-1] == 'i' )
194 {
195 return new Gats::Integer(
196 strtoll( s.getStr(), NULL, 10 )
197 );
198 }
199 else if( s[iSize-1] == 'f' )
200 {
201 return new Gats::Float(
202 strtod( s.getStr(), NULL )
203 );
204 }
205 else
206 {
207 for( Bu::String::iterator i = s.begin(); i; i++ )
208 {
209 if( *i == '.' )
210 return new Gats::Float(
211 strtod( s.getStr(), NULL )
212 );
213 }
214 return new Gats::Integer(
215 strtoll( s.getStr(), NULL, 10 )
216 );
217 }
218 }
219 break;
220
221 default:
222 {
223 Bu::String s = token( i );
224 int iSize = s.getSize();
225 // Test for explicit types first
226 if( iSize > 2 )
227 {
228 if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' )
229 {
230 }
231 else
232 {
233 Bu::String st = s.toLower();
234 if( st == "true" )
235 {
236 return new Gats::Boolean( true );
237 }
238 else if( st == "false" )
239 {
240 return new Gats::Boolean( false );
241 }
242 }
243 }
244 }
245 break;
246 }
247
248 return NULL;
249}
250
251Gats::Object *Gats::Object::strToGats( const Bu::String &sStr )
252{
253 Bu::String::const_iterator i = sStr.begin();
254
255 return strToGats( i );
256}
257
66Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) 258Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj )
67{ 259{
68 switch( obj.getType() ) 260 switch( obj.getType() )
diff --git a/src/object.h b/src/object.h
index 1c114b3..91c48ad 100644
--- a/src/object.h
+++ b/src/object.h
@@ -1,6 +1,8 @@
1#ifndef GATS_OBJECT_H 1#ifndef GATS_OBJECT_H
2#define GATS_OBJECT_H 2#define GATS_OBJECT_H
3 3
4#include <bu/string.h>
5
4namespace Bu 6namespace Bu
5{ 7{
6 class Stream; 8 class Stream;
@@ -34,6 +36,12 @@ namespace Gats
34 virtual void read( Bu::Stream &rIn, char cType )=0; 36 virtual void read( Bu::Stream &rIn, char cType )=0;
35 37
36 static Object *read( Bu::Stream &rIn ); 38 static Object *read( Bu::Stream &rIn );
39 static Object *strToGats( const Bu::String &sStr );
40
41 private:
42 static Object *strToGats( Bu::String::const_iterator &i );
43 static Bu::String token( Bu::String::const_iterator &i );
44 static void skipWs( Bu::String::const_iterator &i );
37 }; 45 };
38 46
39 const char *typeToStr( Type t ); 47 const char *typeToStr( Type t );