1
/* global module, require */
2
3
/**
4
* Utility functions specific to EasyRTC.
5
*
6
* @module easyrtc_util
7
* @author Priologic Software, info@easyrtc.com
8
* @copyright Copyright 2016 Priologic Software. All rights reserved.
9
* @license BSD v2, see LICENSE file in module root folder.
10
*/
11
12
var util = require("util");
13
var _ = require("underscore"); // General utility functions external module
14
var g = require("./general_util"); // General utility functions local module
15
var e = require("./easyrtc_private_obj"); // EasyRTC private object
16
17
/**
18
* Object to hold EasyRTC Utility methods and classes.
19
*
20
* @class
21
*/
22
var eu = module.exports;
23
24
/**
25
* Disconnects socket. Failure results in a debug level log message.
26
*
27
* @param {Object} socket Socket.io connection object.
28
*/
29
eu.socketDisconnect = function(socket) {
30
try {
31
socket.disconnect();
32
} catch(err) {
33
eu.log("debug", "Socket disconnection command failed. Socket may already be disconnected.");
34
}
35
};
36
37
38
/**
39
* Custom Error Object for EasyRTC Server Errors.
40
*
41
* @extends Error
42
* @param {string} msg Text message describing the error.
43
* @returns {Error}
44
*/
45
eu.ServerError = function(msg) {
46
eu.ServerError.super_.call(this, msg, this.constructor);
47
};
48
util.inherits(eu.ServerError, g.AbstractError);
49
eu.ServerError.prototype.name = "Server Error";
50
eu.ServerError.prototype.errorLevel = "error";
51
52
53
/**
54
* Custom Error Object for EasyRTC Application Errors.
55
*
56
* @extends Error
57
* @param {string} msg Text message describing the error.
58
* @returns {Error}
59
*/
60
eu.ApplicationError = function(msg) {
61
eu.ApplicationError.super_.call(this, msg, this.constructor);
62
};
63
util.inherits(eu.ApplicationError, g.AbstractError);
64
eu.ApplicationError.prototype.name = "Application Error";
65
eu.ApplicationError.prototype.errorLevel = "error";
66
67
68
/**
69
* Custom Error Object for Connection Errors.
70
*
71
* @extends Error
72
* @param {string} msg Text message describing the error.
73
* @returns {Error}
74
*/
75
eu.ConnectionError = function(msg) {
76
eu.ConnectionError.super_.call(this, msg, this.constructor);
77
};
78
util.inherits(eu.ConnectionError, g.AbstractError);
79
eu.ConnectionError.prototype.name = "Connection Error";
80
eu.ConnectionError.prototype.errorLevel = "error";
81
82
83
/**
84
* Custom Error Object for EasyRTC Server Warnings.
85
*
86
* @extends Error
87
* @param {string} msg Text message describing the error.
88
* @returns {Error}
89
*/
90
eu.ServerWarning = function(msg) {
91
eu.ServerWarning.super_.call(this, msg, this.constructor);
92
};
93
util.inherits(eu.ServerWarning, g.AbstractError);
94
eu.ServerWarning.prototype.name = "Server Warning";
95
eu.ServerWarning.prototype.errorLevel = "warning";
96
97
98
/**
99
* Custom Error Object for EasyRTC Application Warnings.
100
*
101
* @extends Error
102
* @param {string} msg Text message describing the error.
103
* @returns {Error}
104
*/
105
eu.ApplicationWarning = function(msg) {
106
eu.ApplicationWarning.super_.call(this, msg, this.constructor);
107
};
108
util.inherits(eu.ApplicationWarning, g.AbstractError);
109
eu.ApplicationWarning.prototype.name = "Application Warning";
110
eu.ApplicationWarning.prototype.errorLevel = "warning";
111
112
113
/**
114
* Custom Error Object for Connection Warnings.
115
*
116
* @extends Error
117
* @param {string} msg Text message describing the error.
118
* @returns {Error}
119
*/
120
eu.ConnectionWarning = function(msg) {
121
eu.ConnectionWarning.super_.call(this, msg, this.constructor);
122
};
123
util.inherits(eu.ConnectionWarning, g.AbstractError);
124
eu.ConnectionWarning.prototype.name = "Connection Warning";
125
eu.ConnectionWarning.prototype.errorLevel = "warning";
126
127
128
/**
129
* Determines if an Error object is an instance of ApplicationError, ConnectionError, or ServerError. If it is, it will return true.
130
*
131
* @param {Error} err
132
* @return {Boolean}
133
*/
134
eu.isError = function(err) {
135
if (err && ((err instanceof eu.ConnectionError)||(err instanceof eu.ApplicationError)||(err instanceof eu.ServerError)||(err instanceof Error))) {
136
return true;
137
} else {
138
return false;
139
}
140
};
141
142
143
/**
144
* Determines if an Error object is an instance of ApplicationWarning, ConnectionWarning, or ServerWarning. If it is, it will return true.
145
*
146
* @param {Error} err
147
* @return {Boolean}
148
*/
149
eu.isWarning = function(err) {
150
if (err && ((err instanceof eu.ConnectionWarning)||(err instanceof eu.ApplicationWarning)||(err instanceof eu.ServerWarning))) {
151
return true;
152
} else {
153
return false;
154
}
155
};
156
157
158
/**
159
* Returns a random available easyrtcid.
160
*
161
* @return {String} Available easyrtcid. A unique identifier for an EasyRTC connection.
162
*/
163
eu.getAvailableEasyrtcid = function() {
164
var newEasyrtcid = "";
165
var easyrtcidExists = false;
166
167
do {
168
newEasyrtcid = g.randomString();
169
easyrtcidExists = false;
170
for (var key in e.app) {
171
if (e.app[key].connection[newEasyrtcid]) {
172
easyrtcidExists = true;
173
break;
174
}
175
}
176
} while (easyrtcidExists);
177
178
return newEasyrtcid;
179
};