summaryrefslogtreecommitdiff
path: root/project/js/app.server.model.js
blob: 771353efd0b4afe51f7521f84e0f724ed4ee7ea6 (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
/*global $, tizen, app */
/**
 * @class Model
 */
function ServerModel(parent) {
	'use strict';
	this.server = parent;
	this.init();
}

(function () { // strict mode wrapper
	'use strict';
	ServerModel.prototype = {

		/**
		 * API module initialisation
		 */
		init: function ServerModel_init() {},

		registerServer: function ServerModel_registerServer(adapter, serviceUUID, callback) {
			if (this.server.getNumberOfClients() === 0) {
				try {
					adapter.registerRFCOMMServiceByUUID(serviceUUID, 'Chat service', callback, function (error) { console.error(error.message); });
				} catch (error) {
					console.error(error.message);
				}
			}
		},

		unregisterChatServer: function ServerModel_unregisterChatServer(globalRecordHandler, successCallback, errorCallback, showButtonsCallback) {
			try {
				if (globalRecordHandler !== null) {
					globalRecordHandler.unregister(successCallback, errorCallback);
				} else {
					showButtonsCallback();
				}
			} catch (error) {
				errorCallback();
			}
		},

		sendMessage: function ServerModel_sendMessage(name, socket, message, callback) {
			var sendTextMsg = [], messageObj, messageObjToString, i, len;
			name = encodeURIComponent(name);
			message = encodeURIComponent(message);
			messageObj = {name: name, text: message, ping: false, bye: false};
			messageObjToString = JSON.stringify(messageObj);
			len = messageObjToString.length;
			for (i = 0; i < len; i += 1) {
				sendTextMsg[i] = messageObjToString.charCodeAt(i);
			}
			try {
				if (socket !== null && socket.state === "OPEN") {
					socket.writeData(sendTextMsg);
					callback(message);
				}
			} catch (error) {
				console.error('sendMessage: ' + error.message);
			}
		},

		sendBye: function ServerModel_sendBye(name, socket) {
			var sendTextMsg = [], messageObj, messageObjToString, i, len;
			name = encodeURIComponent(name);
			messageObj = {name: name, text: '', ping: false, bye: true};
			messageObjToString = JSON.stringify(messageObj);
			len = messageObjToString.length;
			for (i = 0; i < len; i += 1) {
				sendTextMsg[i] = messageObjToString.charCodeAt(i);
			}
			try {
				if (socket !== null && socket.state === "OPEN") {
					socket.writeData(sendTextMsg);
				}
			} catch (error) {
				console.error('sendBye: ' + error.message);
			}
		}

	};
}());