server-client.service.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { BehaviorSubject, Subject } from 'rxjs';
  2. import { ColorCode, Message, ReportStatus, ConnectionAttribute, ConnectionRequest, ConnectionStatus } from '../interfaces/general.interface';
  3. import { GrpcServiceMethod } from './grpc.service.method';
  4. import { FisRetransmissionService } from './fis.retransmission.service';
  5. export class ServerClientManager {
  6. private connectionAttributes: ConnectionAttribute[] = []
  7. constructor(private grpcService: GrpcServiceMethod) {
  8. }
  9. public generateConnection(request: ConnectionRequest) {
  10. let initialReport: ReportStatus = { code: ColorCode.YELLOW, message: 'Initialization of the subject' }
  11. let reportSubject: BehaviorSubject<ReportStatus> = new BehaviorSubject(initialReport)
  12. let retransmission: FisRetransmissionService = new FisRetransmissionService()
  13. let messageToBePublished: Subject<Message> = retransmission.handleMessage(request.server.messageToBePublishedfromApplication, reportSubject)
  14. let connectionAttribute: ConnectionAttribute = {
  15. ConnectionID: {
  16. local: '',
  17. remote: ''
  18. },
  19. outGoing: {
  20. MessageToBePublished: messageToBePublished,
  21. MessageToBeReceived: null
  22. },
  23. inComing: {
  24. MessageToBePublished: null,
  25. MessageToBeReceived: request.client.messageToBeReceivedFromRemote
  26. },
  27. connectionStatus: reportSubject // this is not related to report status for the retrasmission service
  28. }
  29. // let connectionStatus: Record<string, ConnectionStatus> = {
  30. // localStatus: ConnectionStatus.RED,
  31. // remoteStatus: ConnectionStatus.RED
  32. // }
  33. // This is default connection
  34. if (!request.server.connectionType) {
  35. request.server.connectionType = 'GRPC'
  36. }
  37. // For each connection type:
  38. if (request.server.connectionType == 'GRPC') {
  39. this.grpcService.create(request, connectionAttribute)
  40. this.connectionAttributes.push(connectionAttribute)
  41. }
  42. // Check to make sure both the connection at local and remote is up, then report to retransmission service to release message
  43. // connectionAttribute.connectionStatus.subscribe({
  44. // // Check this....
  45. // next: (element: any) => {
  46. // if (element.message == 'Local Server Communication Established') {
  47. // connectionStatus.localStatus = ConnectionStatus.GREEN
  48. // console.log(`Local alive`)
  49. // }
  50. // if (element.message == 'Remote Server Communication Established') {
  51. // connectionStatus.remoteStatus = ConnectionStatus.GREEN
  52. // console.log(`Remote alive`)
  53. // }
  54. // if (connectionStatus.localStatus == ConnectionStatus.GREEN && connectionStatus.remoteStatus == ConnectionStatus.GREEN) {
  55. // let report: ReportStatus = {
  56. // code: ColorCode.GREEN,
  57. // message: `Both Local and Remote are connected`
  58. // }
  59. // // reportSubject.next(report)
  60. // console.log(`Both local and remote alive`)
  61. // }
  62. // },
  63. // error: (err) => console.error(err),
  64. // complete: () => { }
  65. // })
  66. }
  67. }