File: serverWidget.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (330 lines) | stat: -rw-r--r-- 7,673 bytes parent folder | download | duplicates (4)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/KERNEL/serverWidget.h>
#include <BALL/CONCEPT/client.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/DIALOGS/preferences.h>
#include <BALL/VIEW/DIALOGS/serverPreferences.h>
#include <BALL/VIEW/KERNEL/message.h>
#include <BALL/FORMAT/INIFile.h>

#include <QtWidgets/QStatusBar>
#include <QtGui/QPixmap>
#include <QtWidgets/QToolTip>

using namespace std;

namespace BALL
{
	namespace VIEW
	{
		const char* ServerWidget::mini_ray_xpm_[] =
		{
        "16 14 4 1",
        "   c None",
        ".  c black",
        "X  c yellow",
        "o  c gray50",
        "     .........  ",
        "     .XXXXXX.o  ",
        "    .XXXXXX.o   ",
        "    .XXXXX.o    ",
        "   .XXXXX.o     ",
        "   .XXXX.....   ",
        "  .XXXXXXXX.o   ",
        "  .....XXX.o    ",
        "   oo.XXX.o     ",
        "     .XX.o      ",
        "    .XX.o       ",
        "    .X.o        ",
        "   .X.o         ",
        "   ..o          "
		};        
  
		ServerWidget::NotCompositeObject::NotCompositeObject(const char* file, int line)
			:	Exception::GeneralException(file, line, string("NotCompositeObject"), string("received an non composite object!"))
		{
		}

		ServerWidget::ServerWidget(QWidget* parent, const char* name)
			:	QObject(parent),
				ModularWidget(name),
				server_(this, VIEW_DEFAULT_PORT, true),
				object_creator_(0),
				port_(VIEW_DEFAULT_PORT),
				server_preferences_(0),
				server_icon_(0)
		{
			#ifdef BALL_VIEW_DEBUG
				Log.error() << "new ServerWidget " << this << std::endl;
			#endif

			// register ModularWidget
			registerWidget(this);
			
			unregisterObjectCreator();

			server_.setMainControl(getMainControl());

			connect(this, SIGNAL(lockRequested(bool)), this, SLOT(handleLocking(bool)));
		}

		ServerWidget::ServerWidget(const ServerWidget& /*s*/)
			: QObject(),
				ModularWidget(),
				server_(this, VIEW_DEFAULT_PORT, true)
		{
			// register ModularWidget
			registerWidget(this);
			
			unregisterObjectCreator();

			server_.setMainControl(getMainControl());
		}

		ServerWidget::~ServerWidget()
		{
			#ifdef BALL_VIEW_DEBUG
				Log.error() << "Destructing object " << this << " ServerWidget" << std::endl;
			#endif 

			server_.deactivate();
			server_.terminate();
			server_.wait();

			ConnectionObject::destroy();

			if (object_creator_ != 0) delete object_creator_;
		}

		void ServerWidget::clear()
		{
			ConnectionObject::clear();

			server_.deactivate();
		}

		int ServerWidget::getPort() const
		{
			return port_;
		}
		
		void ServerWidget::setPort(const int port)
		{
			port_ = port;
			server_.setPort(port_);
		}
		
		void ServerWidget::initializeWidget(MainControl& main_control)
		{
			server_icon_ = new QLabel(main_control.statusBar());
			main_control.statusBar()->addPermanentWidget(server_icon_);
//   			QToolTip::add(server_icon_, "VIEW server status");
			QPixmap icon(mini_ray_xpm_);

  		server_icon_->setFrameShape(QLabel::NoFrame);
			server_icon_->setPixmap(icon);
			server_icon_->setMaximumSize(14,20);
			server_icon_->setMinimumSize(14,20);
			server_icon_->show();
 		}

		void ServerWidget::finalizeWidget(MainControl& main_control)
		{
			main_control.statusBar()->removeWidget(server_icon_);	
			delete server_icon_;
		}
			
		void ServerWidget::initializePreferencesTab(Preferences &preferences)
		{
			server_preferences_ = new ServerPreferences();

			preferences.insertEntry(server_preferences_);
		}

		void ServerWidget::finalizePreferencesTab(Preferences &preferences)
		{
			if (server_preferences_ != 0)
			{
				preferences.removeEntry(server_preferences_);
				server_preferences_ = 0;
			}
		}
		
		void ServerWidget::applyPreferences()
		{
			if (server_preferences_ == 0) return;

			// get server mode
			if (server_preferences_->getServerStatus())
			{
				// retrieve the port number
				int port = server_preferences_->getPort();

				// set the port and active the server
				setPort(port);
				server_.start();

				if (server_.isRunning())
				{
					// adjust the tool tip and update the server icon
					QString tip;
					tip.sprintf("VIEW Server listening on port %d", port); 
					server_icon_->setToolTip(tip);
					server_icon_->show();
				}
			}
			else
			{
				// stop the server
				server_.deactivate();

				// hide the icon
				server_icon_->hide();
			}
		}

		bool ServerWidget::isValid() const
		{
			return (ConnectionObject::isValid());
		}

		void ServerWidget::dump(ostream& s, Size depth) const
		{
			BALL_DUMP_STREAM_PREFIX(s);
			
			BALL_DUMP_DEPTH(s, depth);
			BALL_DUMP_HEADER(s, this, this);

			ConnectionObject::dump(s, depth + 1);

			BALL_DUMP_STREAM_SUFFIX(s);
		}

		void ServerWidget::changeLock(bool lock)
		{
			Q_EMIT lockRequested(lock);
		}

		void ServerWidget::handleLocking(bool lock)
		{
			if (lock)
			{
				// try releasing a lock if we already hold one
				unlockComposites();
				if (lockComposites())
					server_.setLocked(true);
				server_.setLocked(true);
			}
			else
			{
				unlockComposites();
				server_.setLocked(false);
			}
		}

		ServerWidget::BALLViewServer::BALLViewServer(ServerWidget* parent, Size port, bool restart)
			: BALLThread(),
				TCPServerThread(port, true, restart),
				parent_widget_(parent)
		{
		}

		void ServerWidget::BALLViewServer::run()
		{
			try {
				activate();
			} catch (std::exception& e) {
				output_(e.what());
			}
		}

		void ServerWidget::BALLViewServer::handleConnection()
		{
			unsigned int command;

			// reading command
			connected_stream_ >> command;
			
			// process commands
			switch (command)
			{
				case Client::COMMAND__SEND_OBJECT:
					sendObject();
				break;

   			default:
					Log.info() << "Server: unkown command." << endl;
					connected_stream_.close();
					break;
			}
		}

	  void ServerWidget::BALLViewServer::sendObject()
	{
			output_("Server: receiving object ... \n");

			unsigned long object_handle;
			
			// get object handle
			connected_stream_ >> object_handle;
			
			ObjectCreator* object_creator = &(parent_widget_->getObjectCreator());
			output_("creating object\n");

			// use specified object creator for inserting the object in the scene
			Composite* new_composite_ptr = object_creator->operator()(connected_stream_);
			if (new_composite_ptr == 0)
			{
				throw NotCompositeObject(__FILE__, __LINE__);
			}

			received_composite_ = new_composite_ptr;

			// get composite with handle
 			CompositeHashMap::Iterator iterator = composite_hashmap_.find(object_handle);

			is_locked_ = false;
			parent_widget_->changeLock(true);
			
			while (!is_locked_)
			{
				// give the GUI thread time to update widgets, etc., and try
				// again in 2 seconds
				msleep(2000);
				parent_widget_->changeLock(true);
			}

		 	// already in hashmap ?
			if (iterator != composite_hashmap_.end())
			{
				// tell the main control to get rid of it
				sendMessage_(new CompositeMessage(*iterator->second,
																					CompositeMessage::REMOVED_COMPOSITE,
																					true));

				// remove old composite from hashmap
				composite_hashmap_.erase(object_handle);
			}

			// insert new composite 
			composite_hashmap_.insert(CompositeHashMap::ValueType(object_handle, new_composite_ptr));
			sendMessage_(new CompositeMessage(*new_composite_ptr,
																			 CompositeMessage::NEW_COMPOSITE,
																			 true));

			parent_widget_->changeLock(false);
    }

		void ServerWidget::BALLViewServer::setLocked(bool is_locked)
		{
			is_locked_ = is_locked;
		}

#		ifdef BALL_NO_INLINE_FUNCTIONS
#			include <BALL/VIEW/KERNEL/serverWidget.iC>
#		endif 
	} 
} // namespaces