server.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import argparse
  20. import gi
  21. gi.require_version('Notify', '0.7')
  22. from gi.repository import Notify
  23. import logging
  24. import logging.config
  25. import logging.handlers
  26. import sys
  27. # import mylibnetwork
  28. import threading
  29. import socket
  30. import traceback
  31. import time
  32. import yaml
  33. import blinker
  34. import curio
  35. # add the source dir to the path, python finds the beewatch module.
  36. sys.path.insert(0,"src")
  37. import beewatch
  38. # import beewatch
  39. import beewatch.server
  40. # import beewatch.server as beeserver
  41. import digilib.misc
  42. Notify.init("Bee Watch Server")
  43. log = logging.getLogger(__name__+"")
  44. parser = argparse.ArgumentParser(
  45. # usage="use this to play",
  46. description="BeeWatch Server",
  47. )
  48. parser.add_argument(
  49. "host",
  50. type=str,
  51. help="hostname/ip address/filename (only if --use-file was specified)"
  52. )
  53. parser.add_argument(
  54. "port",
  55. type=int,
  56. default=8054,
  57. nargs="?",
  58. help="the port on wich the connection will open up. ignored if --use-file \
  59. is given"
  60. )
  61. parser.add_argument(
  62. "-d","--debug",
  63. default=False,
  64. action="store_true",
  65. help="enables logging"
  66. )
  67. parser.add_argument(
  68. "--log-ip",
  69. dest="log_ip",
  70. default=False,
  71. action="store_false",
  72. help="WARNING: THIS FLAG ENABLES IP LOGGING!"
  73. )
  74. group_af_family = parser.add_mutually_exclusive_group()
  75. group_af_family.add_argument(
  76. "-u","--af-unix",
  77. dest="af_family",
  78. # default=False,
  79. action="store_const",
  80. const="AF_UNIX",
  81. help="""use an AF_UNIX socket (a file). can't be used in combination with
  82. --af-inet."""
  83. # " will be used \
  84. # instead of an AF_INET socket (hostname/ip address + port)."
  85. )
  86. group_af_family.add_argument(
  87. "-i","--af_inet",
  88. default="AF_INET",
  89. dest="af_family",
  90. action="store_const",
  91. const="AF_INET",
  92. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  93. with --af-unix"""
  94. # (hostname/ip address + port) will be used instead of an AF_INET \
  95. # socket (a file)."
  96. )
  97. args = parser.parse_args()
  98. if args.debug:
  99. digilib.misc.load_files("config/logging.yaml",logging.config.dictConfig)
  100. if args.af_family == "AF_UNIX":
  101. port = None
  102. elif args.af_family == "AF_INET":
  103. port = args.port
  104. config_files = [
  105. "config/controllers.yaml",
  106. "config/commands.yaml",
  107. ]
  108. digilib.misc.load_files(config_files,beewatch.Configure)
  109. bee_server = beewatch.server.BeeWatchServer(
  110. host=args.host,
  111. port=args.port,
  112. af_family=args.af_family,
  113. log_ip=args.log_ip,
  114. )
  115. sig_exit = blinker.signal("global-exit")
  116. try:
  117. bee_server.start()
  118. print(bee_server.run)
  119. except Exception as e:
  120. log.warn("an error occured, aborting")
  121. log.error(e, exc_info=True)
  122. except KeyboardInterrupt:
  123. print("\r ", end="\r")
  124. logging.warn("KeyboardInterrupt! aborting!")
  125. finally:
  126. bee_server.shutdown()
  127. sig_exit.send()
  128. log.info("have a good time :)")
  129. pass
  130. sys.exit()
  131. #