File: source/peer-privileged.js

  1. /**
  2. * These are the list of Peer list retrieval states that Skylink would trigger.
  3. * - This relates to and requires the Privileged Key feature where Peers using
  4. * that Privileged alias Key becomes a privileged Peer with privileged functionalities.
  5. * @attribute GET_PEERS_STATE
  6. * @type JSON
  7. * @param {String} ENQUIRED <small>Value <code>"enquired"</code></small>
  8. * The state when the privileged Peer already enquired signaling for list of peers.
  9. * @param {String} RECEIVED <small>Value <code>"received"</code></small>
  10. * The state when the privileged Peer received list of peers from signaling.
  11. * @readOnly
  12. * @component Peer
  13. * @for Skylink
  14. * @since 0.6.1
  15. */
  16. Skylink.prototype.GET_PEERS_STATE = {
  17. ENQUIRED: 'enquired',
  18. RECEIVED: 'received'
  19. };
  20.  
  21. /**
  22. * These are the list of Peer introduction states that Skylink would trigger.
  23. * - This relates to and requires the Privileged Key feature where Peers using
  24. * that Privileged alias Key becomes a privileged Peer with privileged functionalities.
  25. * @attribute INTRODUCE_STATE
  26. * @type JSON
  27. * @param {String} INTRODUCING <small>Value <code>"enquired"</code></small>
  28. * The state when the privileged Peer have sent the introduction signal.
  29. * @param {String} ERROR <small>Value <code>"error"</code></small>
  30. * The state when the Peer introduction has occurred an exception.
  31. * @readOnly
  32. * @component Peer
  33. * @for Skylink
  34. * @since 0.6.1
  35. */
  36. Skylink.prototype.INTRODUCE_STATE = {
  37. INTRODUCING: 'introducing',
  38. ERROR: 'error'
  39. };
  40.  
  41. /**
  42. * Whether this user automatically introduce to other peers.
  43. * @attribute _autoIntroduce
  44. * @type Boolean
  45. * @default true
  46. * @private
  47. * @component Peer
  48. * @for Skylink
  49. * @since 0.6.1
  50. */
  51. Skylink.prototype._autoIntroduce = true;
  52.  
  53. /**
  54. * Whether this user is a privileged user.
  55. * @attribute isPrivileged
  56. * @type Boolean
  57. * @default false
  58. * @private
  59. * @component Peer
  60. * @for Skylink
  61. * @since 0.6.1
  62. */
  63. Skylink.prototype._isPrivileged = false;
  64.  
  65. /**
  66. * Parent key in case the current key is alias.
  67. * If the current key is not alias, this is the same as _appKey
  68. * @attribute _parentKey
  69. * @type String
  70. * @default null
  71. * @private
  72. * @component Peer
  73. * @for Skylink
  74. * @since 0.6.1
  75. */
  76. Skylink.prototype._parentKey = null;
  77.  
  78. /**
  79. * List of peers retrieved from signaling
  80. * @attribute _peerList
  81. * @type Object
  82. * @default null
  83. * @private
  84. * @component Peer
  85. * @for Skylink
  86. * @since 0.6.1
  87. */
  88. Skylink.prototype._peerList = null;
  89.  
  90. /**
  91. * Retrieves the list of rooms and peers under the same realm based
  92. * on the Application Key configured in {{#crossLink "Skylink/init:method"}}init(){{/crossLink}}
  93. * from the platform signaling.
  94. * This will only work if self is a privileged Peer.
  95. * @method getPeers
  96. * @param {Boolean} [showAll=false] The flag that indicates if returned list should
  97. * also include privileged and standard in the list. By default, the value is <code>false</code>.
  98. * Which means only unprivileged peers' ID (isPrivileged = autoIntroduce = false) is included.
  99. * @param {Function} [callback] The callback fired after the receiving the current
  100. * list of Peers from platform signaling or have met with an exception.
  101. * The callback signature is <code>function (error, success)</code>.
  102. * @param {Object} callback.error The error object received in the callback.
  103. * This is the exception thrown that caused the failure for getting self user media.
  104. * If received as <code>null</code>, it means that there is no errors.
  105. * @param {JSON} callback.success The success object received in the callback.
  106. * If received as <code>null</code>, it means that there are errors.
  107. * @example
  108. *
  109. * // To get list of unprivileged peers only
  110. * SkylinkDemo.getPeers();
  111. *
  112. * // To get list of all peers, including other privileged peers
  113. * SkylinkDemo.getPeers(true);
  114. *
  115. * // To get a list of unprivileged peers then invoke the callback
  116. * SkylinkDemo.getPeers(function(error, success){
  117. * if (error){
  118. * console.log("Error happened. Can not retrieve list of peers");
  119. * }
  120. * else{
  121. * console.log("Success fully retrieved list of peers", success);
  122. * }
  123. * });
  124. *
  125. * // To get a list of all peers then invoke the callback
  126. * SkylinkDemo.getPeers(true, function(error, success){
  127. * if (error){
  128. * console.log("Error happened. Can not retrieve list of peers");
  129. * }
  130. * else{
  131. * console.log("Success fully retrieved list of peers", success);
  132. * }
  133. * });
  134. *
  135. * @trigger getPeersStateChange
  136. * @component Peer
  137. * @for Skylink
  138. * @since 0.6.1
  139. */
  140. Skylink.prototype.getPeers = function(showAll, callback){
  141. var self = this;
  142. if (!self._isPrivileged){
  143. log.warn('Please upgrade your key to privileged to use this function');
  144. return;
  145. }
  146. if (!self._appKey){
  147. log.warn('App key is not defined. Please authenticate again.');
  148. return;
  149. }
  150. if (!self._parentKey){
  151. log.warn('Parent key is not defined. Please authenticate again.');
  152. return;
  153. }
  154.  
  155. // Only callback is provided
  156. if (typeof showAll === 'function'){
  157. callback = showAll;
  158. showAll = false;
  159. }
  160.  
  161. self._sendChannelMessage({
  162. type: self._SIG_MESSAGE_TYPE.GET_PEERS,
  163. privilegedKey: self._appKey,
  164. parentKey: self._parentKey,
  165. showAll: showAll || false
  166. });
  167. self._trigger('getPeersStateChange',self.GET_PEERS_STATE.ENQUIRED, self._user.sid, null);
  168.  
  169. log.log('Enquired server for peers within the realm');
  170.  
  171. if (typeof callback === 'function'){
  172. self.once('getPeersStateChange', function(state, privilegedPeerId, peerList){
  173. callback(null, peerList);
  174. }, function(state, privilegedPeerId, peerList){
  175. return state === self.GET_PEERS_STATE.RECEIVED;
  176. });
  177. }
  178.  
  179. };
  180.  
  181. /**
  182. * Introduces two Peers to each other to start a connection with each other.
  183. * This will only work if self is a privileged Peer.
  184. * @method introducePeer
  185. * @param {String} sendingPeerId The Peer ID of the peer
  186. * that initiates the connection with the introduced Peer.
  187. * @param {String} receivingPeerId The Peer ID of the
  188. * introduced peer who would be introduced to the initiator Peer.
  189. * @trigger introduceStateChange
  190. * @component Peer
  191. * @for Skylink
  192. * @since 0.6.1
  193. */
  194. Skylink.prototype.introducePeer = function(sendingPeerId, receivingPeerId){
  195. var self = this;
  196. if (!self._isPrivileged){
  197. log.warn('Please upgrade your key to privileged to use this function');
  198. self._trigger('introduceStateChange', self.INTRODUCE_STATE.ERROR, self._user.sid, sendingPeerId, receivingPeerId, 'notPrivileged');
  199. return;
  200. }
  201. self._sendChannelMessage({
  202. type: self._SIG_MESSAGE_TYPE.INTRODUCE,
  203. sendingPeerId: sendingPeerId,
  204. receivingPeerId: receivingPeerId
  205. });
  206. self._trigger('introduceStateChange', self.INTRODUCE_STATE.INTRODUCING, self._user.sid, sendingPeerId, receivingPeerId, null);
  207. log.log('Introducing',sendingPeerId,'to',receivingPeerId);
  208. };
  209.  
  210.