message.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package node
  16. import (
  17. hub "github.com/mudler/edgevpn/pkg/hub"
  18. )
  19. // messageWriter is a struct returned by the node that satisfies the io.Writer interface
  20. // on the underlying hub.
  21. // Everything Write into the message writer is enqueued to a message channel
  22. // which is sealed and processed by the node
  23. type messageWriter struct {
  24. input chan<- *hub.Message
  25. c Config
  26. mess *hub.Message
  27. }
  28. // Write writes a slice of bytes to the message channel
  29. func (mw *messageWriter) Write(p []byte) (n int, err error) {
  30. return mw.Send(mw.mess.WithMessage(string(p)))
  31. }
  32. // Send sends a message to the channel
  33. func (mw *messageWriter) Send(copy *hub.Message) (n int, err error) {
  34. mw.input <- copy
  35. return len(copy.Message), nil
  36. }