server-client.service.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { BehaviorSubject, Subject } from 'rxjs';
  2. import { ClientRequest, ConnectionAttribute, ConnectionRequest, ConnectionState, Message, ServerRequest, State } from '../interfaces/general.interface';
  3. import { GrpcServiceMethod } from './grpc.service.method';
  4. import { BufferService } from './buffer.service';
  5. import * as dotenv from 'dotenv'
  6. import * as _ from 'lodash'
  7. dotenv.config()
  8. /**
  9. * @deprecated This service will be removed in future versions.
  10. * We are using websocket now.
  11. */
  12. export class ServerClientManager {
  13. private connectionAttributes: ConnectionAttribute[] = []
  14. private grpcService: GrpcServiceMethod = new GrpcServiceMethod()
  15. private defaultServerAttribute: ServerRequest = {
  16. name: 'Default - Server',
  17. serverUrl: "localhost:3000",
  18. connectionType: 'GRPC',
  19. messageToBePublishedFromApplication: new Subject<Message>()
  20. }
  21. private defaultClientAttribute: ClientRequest = {
  22. name: 'Default - Client',
  23. targetServer: "localhost:3001",
  24. connectionType: 'GRPC',
  25. messageToBeReceivedFromRemote: new Subject<Message>()
  26. }
  27. constructor() {
  28. // this.generateConnection({})
  29. }
  30. // create connection based on the request from the application
  31. public async generateConnection(request: ConnectionRequest): Promise<any> {
  32. return new Promise(async (resolve, reject) => {
  33. let initialReport: ConnectionState
  34. let reportSubject: BehaviorSubject<ConnectionState>
  35. let retransmission: BufferService
  36. let errorString: string
  37. let originalRequest = _.cloneDeep(request)
  38. let database: string
  39. let response: any = { message: `Fail to complete connection generation` }
  40. let statusChain: State = 1
  41. let connectionAttribute: ConnectionAttribute
  42. if (statusChain == 1) {
  43. if (!request.server) {
  44. request.server = this.defaultServerAttribute
  45. }
  46. if (!request.client) {
  47. request.client = this.defaultClientAttribute
  48. }
  49. if (request.database) {
  50. database = request.database
  51. } else {
  52. database = request.server.name + request.client.name
  53. }
  54. /* Inject retransmission here */
  55. initialReport = { status: 'BUFFER' }
  56. reportSubject = new BehaviorSubject(initialReport)
  57. retransmission = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
  58. }
  59. if (statusChain == 1) {
  60. // Connection Type checking
  61. if (request.server!.connectionType != request.client!.connectionType) {
  62. statusChain = -1
  63. errorString = "Connection Type DOES NOT MATCH!"
  64. } else {
  65. statusChain = 1
  66. }
  67. }
  68. if (statusChain == 1) {
  69. connectionAttribute = {
  70. ConnectionID: {
  71. local: request.server!.name + request.client!.name,
  72. remote: request.client!.name + request.server!.name
  73. },
  74. outGoing: {
  75. StreamID: request.server!.name,
  76. PublisherID: request.server!.name,
  77. SubscriberID: request.server!.name,
  78. serverUrl: request.server?.serverUrl,
  79. MessageToBePublished: retransmission!.getMessages(),
  80. MessageToBeReceived: null
  81. },
  82. inComing: {
  83. StreamID: request.client!.name,
  84. PublisherID: request.client!.name,
  85. SubscriberID: request.client!.name,
  86. serverUrl: request.client?.targetServer,
  87. MessageToBePublished: null,
  88. MessageToBeReceived: request.client!.messageToBeReceivedFromRemote
  89. },
  90. connectionStatus: reportSubject!
  91. }
  92. }
  93. if (statusChain == 1) {
  94. // to prevent duplicate connection from being created
  95. await this.checkConnectionAttribute(connectionAttribute!).then((res) => {
  96. if (res == true) {
  97. console.log(`Connection<${connectionAttribute.ConnectionID.local}> already exists `)
  98. }
  99. if (res == false) {
  100. this.connectionAttributes.push(connectionAttribute)
  101. console.log(`Connection ${connectionAttribute.ConnectionID.local} registered...`)
  102. }
  103. console.log(`There is now ${this.connectionAttributes.length} connection Attributes`)
  104. })
  105. }
  106. if (statusChain == 1) {
  107. // This is default connection`
  108. if (!request.client!.connectionType) {
  109. request.client!.connectionType = 'GRPC'
  110. }
  111. // For each connection type:
  112. if (request.client!.connectionType == 'GRPC') {
  113. this.grpcService.create(connectionAttribute!, this.connectionAttributes).then(() => {
  114. // logic here
  115. }).catch(() => {
  116. errorString = `Something wrong with gRPC methods`
  117. statusChain = -1
  118. })
  119. }
  120. }
  121. if (statusChain == 1) {
  122. response = {
  123. message: "Channel Response",
  124. requestedTo: originalRequest,
  125. data: connectionAttribute!
  126. }
  127. resolve(response);
  128. } else if (statusChain == -1) {
  129. response = {
  130. message: "Channel Response Error",
  131. requestedTo: originalRequest,
  132. data: errorString! // put error string here
  133. }
  134. resolve(response);
  135. }
  136. })
  137. }
  138. private async checkConnectionAttribute(connectionAttribute: ConnectionAttribute): Promise<boolean> {
  139. return new Promise((resolve) => {
  140. let result: boolean = this.connectionAttributes.some(connection =>
  141. connection.ConnectionID.local === connectionAttribute.ConnectionID.local
  142. );
  143. resolve(result);
  144. });
  145. }
  146. }