awlsim-server 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - Server interface
  5. #
  6. # Copyright 2013-2016 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. import sys
  24. import getopt
  25. from socket import AF_INET, AF_INET6
  26. from awlsim_loader.common import *
  27. from awlsim_loader.core import *
  28. from awlsim_loader.coreclient import *
  29. from awlsim_loader.coreserver import *
  30. import awlsim_loader.cython_helper as cython_helper
  31. def usage():
  32. print("awlsim-server version %s" % VERSION_STRING)
  33. print("")
  34. print("Usage: awlsim-server [OPTIONS] <project.awlpro>")
  35. print("")
  36. print("<project.awlpro> is an optional project file that will be loaded.")
  37. print("If -w is also given, all project changes are written back to that file.")
  38. print("")
  39. print("Options:")
  40. print(" -l|--listen HOST[:PORT] Listen on the specified HOST:PORT")
  41. print(" Defaults to %s:%d" %\
  42. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  43. print(" The special values 'all', 'any' or an empty host")
  44. print(" can be used to listen on any interface.")
  45. print(" -4|--force-ipv4 Force the use of IPv4.")
  46. print(" -6|--force-ipv6 Force the use of IPv6.")
  47. print(" -B|--background Fork a background process")
  48. print(" -w|--rw-project Enable project file writing")
  49. print(" -L|--loglevel LVL Set the log level:")
  50. print(" 0: Log nothing")
  51. print(" 1: Log errors")
  52. print(" 2: Log errors and warnings")
  53. print(" 3: Log errors, warnings and info messages (default)")
  54. print(" 4: Verbose logging")
  55. print(" 5: Extremely verbose logging")
  56. def main():
  57. global opt_listen
  58. global opt_background
  59. opt_project = None
  60. opt_rwProject = False
  61. opt_listen = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  62. opt_family = None
  63. opt_background = False
  64. opt_loglevel = Logging.LOG_INFO
  65. try:
  66. (opts, args) = getopt.getopt(sys.argv[1:],
  67. "hl:46BwL:",
  68. [ "help", "listen=", "force-ipv4", "force-ipv6",
  69. "background", "rw-project",
  70. "loglevel=", ])
  71. except getopt.GetoptError as e:
  72. printError(str(e))
  73. usage()
  74. return ExitCodes.EXIT_ERR_CMDLINE
  75. for (o, v) in opts:
  76. if o in ("-h", "--help"):
  77. usage()
  78. return ExitCodes.EXIT_OK
  79. if o in ("-l", "--listen"):
  80. try:
  81. host, port = parseNetAddress(v)
  82. if not host.strip() or\
  83. host in {"any", "all"}:
  84. host = ""
  85. if port is None:
  86. port = AwlSimServer.DEFAULT_PORT
  87. opt_listen = (host, port)
  88. except AwlSimError as e:
  89. printError("-l|--listen: %s" % e.message)
  90. sys.exit(1)
  91. if o in ("-4", "--force-ipv4"):
  92. opt_family = AF_INET
  93. if o in ("-6", "--force-ipv6"):
  94. opt_family = AF_INET6
  95. if o in ("-B", "--background"):
  96. opt_background = True
  97. if o in ("-w", "--rw-project"):
  98. opt_rwProject = True
  99. if o in ("-L", "--loglevel"):
  100. try:
  101. opt_loglevel = int(v)
  102. except ValueError:
  103. printError("-L|--loglevel: Invalid log level")
  104. sys.exit(1)
  105. if len(args) not in (0, 1):
  106. usage()
  107. return ExitCodes.EXIT_ERR_CMDLINE
  108. if args:
  109. opt_project = args[0]
  110. exitCode = ExitCodes.EXIT_OK
  111. try:
  112. Logging.setLoglevel(opt_loglevel)
  113. if opt_background:
  114. interpreter = sys.executable
  115. assert(interpreter)
  116. serverProcess = AwlSimServer.start(listenHost = opt_listen[0],
  117. listenPort = opt_listen[1],
  118. listenFamily = opt_family,
  119. forkInterpreter = interpreter,
  120. commandMask = 0,
  121. projectFile = opt_project,
  122. projectWriteBack = opt_rwProject)
  123. printInfo("Started awlsim server process (PID: %d)" %\
  124. serverProcess.pid)
  125. else:
  126. if cython_helper.shouldUseCython():
  127. printInfo("*** Using accelerated CYTHON core "
  128. "(AWLSIM_CYTHON environment variable is set)")
  129. exitCode = AwlSimServer.start(listenHost = opt_listen[0],
  130. listenPort = opt_listen[1],
  131. listenFamily = opt_family,
  132. forkInterpreter = None,
  133. commandMask = 0,
  134. projectFile = opt_project,
  135. projectWriteBack = opt_rwProject)
  136. except AwlSimError as e:
  137. printError(e.getReport())
  138. return ExitCodes.EXIT_ERR_SIM
  139. return exitCode
  140. if __name__ == "__main__":
  141. sys.exit(main())