state.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {AsyncEvent} from 'ts-events';
  18. import TaskConnectionState = threema.TaskConnectionState;
  19. import GlobalConnectionState = threema.GlobalConnectionState;
  20. import ChosenTask = threema.ChosenTask;
  21. const enum Stage {
  22. Signaling,
  23. Task,
  24. }
  25. export class StateService {
  26. private logTag: string = '[StateService]';
  27. // Angular services
  28. private $log: ng.ILogService;
  29. private $interval: ng.IIntervalService;
  30. // Events
  31. public evtConnectionBuildupStateChange = new AsyncEvent<threema.ConnectionBuildupStateChange>();
  32. public evtGlobalConnectionStateChange = new AsyncEvent<threema.GlobalConnectionStateChange>();
  33. // Connection states
  34. public signalingConnectionState: saltyrtc.SignalingState;
  35. public taskConnectionState: TaskConnectionState;
  36. // Connection buildup state
  37. public connectionBuildupState: threema.ConnectionBuildupState;
  38. public progress = 0;
  39. private progressInterval: ng.IPromise<any> = null;
  40. public slowConnect = false;
  41. // Global connection state
  42. private stage: Stage;
  43. private _state: threema.GlobalConnectionState;
  44. public wasConnected: boolean;
  45. public static $inject = ['$log', '$interval'];
  46. constructor($log: ng.ILogService, $interval: ng.IIntervalService) {
  47. this.$log = $log;
  48. this.$interval = $interval;
  49. this.reset();
  50. }
  51. /**
  52. * Getters and setters for global connection state.
  53. */
  54. public get state(): threema.GlobalConnectionState {
  55. return this._state;
  56. }
  57. public set state(state: threema.GlobalConnectionState) {
  58. const prevState = this._state;
  59. if (prevState === state) {
  60. // No need to dispatch any events
  61. return;
  62. }
  63. this._state = state;
  64. this.evtGlobalConnectionStateChange.post({state: state, prevState: prevState});
  65. }
  66. /**
  67. * Signaling connection state.
  68. */
  69. public updateSignalingConnectionState(state: saltyrtc.SignalingState, chosenTask: ChosenTask): void {
  70. const prevState = this.signalingConnectionState;
  71. this.signalingConnectionState = state;
  72. if (this.stage === Stage.Signaling
  73. || (this.stage === Stage.Task && chosenTask === ChosenTask.RelayedData)) {
  74. this.$log.debug(this.logTag, 'Signaling connection state:', prevState, '=>', state);
  75. switch (state) {
  76. case 'new':
  77. case 'ws-connecting':
  78. case 'server-handshake':
  79. case 'peer-handshake':
  80. this.state = GlobalConnectionState.Warning;
  81. break;
  82. case 'task':
  83. this.stage = Stage.Task;
  84. if (chosenTask === ChosenTask.RelayedData) {
  85. this.updateTaskConnectionState(TaskConnectionState.Connected);
  86. } else {
  87. this.state = GlobalConnectionState.Warning;
  88. }
  89. break;
  90. case 'closing':
  91. case 'closed':
  92. this.state = GlobalConnectionState.Error;
  93. break;
  94. default:
  95. this.$log.warn(this.logTag, 'Ignored signaling connection state change to', state);
  96. }
  97. } else {
  98. this.$log.debug(this.logTag, 'Ignored signaling connection state to "' + state + '"');
  99. }
  100. }
  101. /**
  102. * Task connection state.
  103. */
  104. public updateTaskConnectionState(state: TaskConnectionState): void {
  105. const prevState = this.taskConnectionState;
  106. this.taskConnectionState = state;
  107. if (this.stage === Stage.Task) {
  108. this.$log.debug(this.logTag, 'Task connection state:', prevState, '=>', state);
  109. switch (state) {
  110. case TaskConnectionState.New:
  111. case TaskConnectionState.Connecting:
  112. case TaskConnectionState.Reconnecting:
  113. this.state = GlobalConnectionState.Warning;
  114. break;
  115. case TaskConnectionState.Connected:
  116. this.state = GlobalConnectionState.Ok;
  117. this.wasConnected = true;
  118. break;
  119. case TaskConnectionState.Disconnected:
  120. this.state = GlobalConnectionState.Error;
  121. break;
  122. default:
  123. this.$log.warn(this.logTag, 'Ignored task connection state change to "' + state + '"');
  124. }
  125. } else {
  126. this.$log.debug(this.logTag, 'Ignored task connection state change to "' + state + '"');
  127. }
  128. }
  129. /**
  130. * Connection buildup state.
  131. */
  132. public updateConnectionBuildupState(state: threema.ConnectionBuildupState): void {
  133. if (this.connectionBuildupState === state) {
  134. return;
  135. }
  136. const prevState = this.connectionBuildupState;
  137. this.$log.debug(this.logTag, 'Connection buildup state:', prevState, '=>', state);
  138. // Update state
  139. this.connectionBuildupState = state;
  140. this.evtConnectionBuildupStateChange.post({state: state, prevState: prevState});
  141. // Cancel progress interval if present
  142. if (this.progressInterval !== null) {
  143. this.$interval.cancel(this.progressInterval);
  144. this.progressInterval = null;
  145. }
  146. // Reset slow connect state
  147. this.slowConnect = false;
  148. // Update progress
  149. switch (state) {
  150. case 'new':
  151. this.progress = 0;
  152. break;
  153. case 'push':
  154. this.progress = 0;
  155. this.progressInterval = this.$interval(() => {
  156. if (this.progress < 12) {
  157. this.progress += 1;
  158. } else {
  159. this.slowConnect = true;
  160. }
  161. }, 800);
  162. break;
  163. case 'manual_start':
  164. this.progress = 0;
  165. break;
  166. case 'connecting':
  167. this.progress = 13;
  168. break;
  169. case 'waiting':
  170. this.progress = 14;
  171. break;
  172. case 'peer_handshake':
  173. this.progress = 15;
  174. this.progressInterval = this.$interval(() => {
  175. if (this.progress < 40) {
  176. this.progress += 5;
  177. } else if (this.progress < 55) {
  178. this.progress += 3;
  179. } else if (this.progress < 60) {
  180. this.progress += 1;
  181. } else {
  182. this.slowConnect = true;
  183. }
  184. }, 500);
  185. break;
  186. case 'loading':
  187. this.progress = 60;
  188. this.progressInterval = this.$interval(() => {
  189. if (this.progress < 80) {
  190. this.progress += 4;
  191. } else if (this.progress < 90) {
  192. this.progress += 2;
  193. } else if (this.progress < 99) {
  194. this.progress += 1;
  195. } else {
  196. this.slowConnect = true;
  197. }
  198. }, 600);
  199. break;
  200. case 'done':
  201. this.progress = 100;
  202. break;
  203. default:
  204. this.progress = 0;
  205. break;
  206. }
  207. }
  208. public readyToSubmit(chosenTask: ChosenTask): boolean {
  209. switch (chosenTask) {
  210. case ChosenTask.RelayedData:
  211. return true;
  212. case ChosenTask.WebRTC:
  213. default:
  214. return this.state === GlobalConnectionState.Ok;
  215. }
  216. }
  217. /**
  218. * Reset all states.
  219. */
  220. public reset(connectionBuildupState: threema.ConnectionBuildupState = 'new'): void {
  221. this.$log.debug(this.logTag, 'Reset states');
  222. // Reset state
  223. this.signalingConnectionState = 'new';
  224. this.taskConnectionState = TaskConnectionState.New;
  225. this.stage = Stage.Signaling;
  226. this.state = GlobalConnectionState.Error;
  227. this.wasConnected = false;
  228. this.connectionBuildupState = connectionBuildupState;
  229. this.progress = 0;
  230. }
  231. }