Advertisement
Guest User

Untitled

a guest
May 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. const STATUS = {
  2. OPEN: 0,
  3. CLOSED: 1,
  4. CLOSING: 2,
  5. }
  6.  
  7. const ROLE = {
  8. INITIATOR: 0,
  9. RESPONDER: 1
  10. }
  11.  
  12. class Connection {
  13. constructor (peerInfo, remoteMa, isInitiator) {
  14. /**
  15. * Connection identifier
  16. */
  17. this.id = (~~(Math.random() * 1e9)).toString(36) + Date.now()
  18.  
  19. /**
  20. * Remote peer info
  21. */
  22. this.peerInfo = peerInfo
  23.  
  24. /**
  25. * Status of the connection
  26. */
  27. this.status = STATUS.OPEN
  28.  
  29. /**
  30. * Endpoints multiaddrs
  31. */
  32. this.endpoints = {
  33. local: undefined,
  34. remote: remoteMa
  35. }
  36.  
  37. /**
  38. * Connection timeline
  39. */
  40. this.timeline = {
  41. openTs: Date.now(),
  42. closeTs: undefined
  43. }
  44.  
  45. /**
  46. * Role in the connection, initiator or responder
  47. */
  48. this.role = isInitiator ? ROLE.INITIATOR : ROLE.RESPONDER
  49.  
  50. /**
  51. * The multiplexer being used
  52. */
  53. this.multiplexer = undefined
  54.  
  55. /**
  56. * The encryption method being used
  57. */
  58. this.encryption = undefined
  59.  
  60. /**
  61. * Connection streams
  62. */
  63. this.streams = []
  64.  
  65. /**
  66. * User provided tags
  67. */
  68. this.tags = []
  69. }
  70.  
  71. newStream () {
  72. if (!this.multiplexer) {
  73. // await hasMultiplexerOrErrored() // magic ensues
  74. }
  75. // ... new stream creation
  76. }
  77.  
  78. getStreams () {
  79.  
  80. }
  81.  
  82. close () {
  83. this.timeline.closeTs = Date.now()
  84. }
  85.  
  86. setLocalAddress (ma) {
  87. this.endpoints.local = ma
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement