cluster.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package cluster
  26. import (
  27. fmt "fmt"
  28. "net"
  29. )
  30. type NeuraNetworkCluster struct {
  31. address string
  32. activeUnits Units
  33. streams []NeuralNetworkCluster_RegisterServer
  34. unitsQueue chan *Unit
  35. }
  36. func NewNeuraNetworkCluster(address string) (cl *NeuraNetworkCluster) {
  37. cl = nil
  38. if address == "" {
  39. return
  40. }
  41. cl = &NewNeuraNetworkCluster{
  42. address: address,
  43. unitsQueue: make(chan *Unit, 10)
  44. }
  45. }
  46. func (cl *NeuraNetworkCluster) serveClients() {
  47. stream.Send(&cl.activeUnits)
  48. cl.activeUnits.List = append(cl.activeUnits.List, unit)
  49. go func() {
  50. for _, stream := range cl.streams {
  51. }
  52. }()
  53. }
  54. func (cl *NeuraNetworkCluster) Run() {
  55. grpcServer := grpc.NewServer()
  56. cluster.RegisterNeuralNetworkClusterServer(grpcServer, cl)
  57. lis, err := net.Listen("tcp", address)
  58. if err != nil {
  59. fmt.Printf("Failed to listen: %v\n", err)
  60. }
  61. fmt.Printf("Listen %v\n", address)
  62. if err := grpcServer.Serve(lis); err != nil {
  63. fmt.Printf("Failed to serve: %v\n", err)
  64. }
  65. }
  66. func (cl *NeuraNetworkCluster) Register(unit *Unit, stream NeuralNetworkCluster_RegisterServer) error {
  67. cl.unitsQueue <- unit
  68. return nil
  69. }