server-client.service.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { BehaviorSubject, Subject } from 'rxjs';
  2. import { ColorCode, Message, ReportStatus, ConnectionAttribute, ConnectionRequest, ConnectionState, MessageLog } from '../interfaces/general.interface';
  3. import { GrpcServiceMethod } from './grpc.service.method';
  4. import { FisRetransmissionService } from './fis.retransmission.service';
  5. import { BufferService } from './buffer.service';
  6. import * as dotenv from 'dotenv'
  7. dotenv.config()
  8. export class ServerClientManager {
  9. private connectionAttributes: ConnectionAttribute[] = []
  10. private request: ConnectionRequest | any
  11. constructor(private grpcService: GrpcServiceMethod) {
  12. }
  13. // putting it here for later use. to be used for testing
  14. public restartServerInDuration(time: number) {
  15. console.log(this.request)
  16. console.log(this.connectionAttributes)
  17. this.grpcService.shutDownServer().then((msg: string) => {
  18. console.log(msg)
  19. setTimeout(() => {
  20. this.generateConnection(this.request)
  21. }, time * 1000)
  22. })
  23. }
  24. public generateConnection(request: ConnectionRequest) {
  25. this.request = request
  26. let database: string
  27. if (request.database) {
  28. database = request.database
  29. } else {
  30. database = request.server.name + request.client.name
  31. }
  32. /* Inject retransmission here */
  33. let initialReport: ConnectionState = { status: 'BUFFER' }
  34. let reportSubject: BehaviorSubject<ConnectionState> = new BehaviorSubject(initialReport)
  35. let retransmission: BufferService = new BufferService(request.server.messageToBePublishedFromApplication, reportSubject, database)
  36. // let initialReport: ReportStatus = { code: ColorCode.RED, message: 'Initialization of the subject' }
  37. // let reportSubject: BehaviorSubject<ReportStatus> = new BehaviorSubject(initialReport)
  38. // let retransmission: FisRetransmissionService = new FisRetransmissionService(database, reportSubject)
  39. // let messageToBePublished: Subject<Message> = retransmission.handleMessage(request.server.messageToBePublishedFromApplication) ?? request.server.messageToBePublishedFromApplication
  40. let connectionAttribute: ConnectionAttribute = {
  41. ConnectionID: {
  42. local: '',
  43. remote: ''
  44. },
  45. outGoing: {
  46. MessageToBePublished: retransmission.getMessages(),
  47. MessageToBeReceived: null
  48. },
  49. inComing: {
  50. MessageToBePublished: null,
  51. MessageToBeReceived: request.client.messageToBeReceivedFromRemote
  52. },
  53. connectionStatus: reportSubject
  54. }
  55. // This is default connection
  56. if (!request.server.connectionType) {
  57. request.server.connectionType = 'GRPC'
  58. }
  59. // For each connection type:
  60. if (request.server.connectionType == 'GRPC') {
  61. this.grpcService.create(request, connectionAttribute)
  62. this.connectionAttributes.push(connectionAttribute)
  63. }
  64. }
  65. }