|
| 1 | +#! /usr/bin/env python3 |
| 2 | +################################################################## |
| 3 | +# fixtool |
| 4 | +# Copyright (C) 2017-2018, David Arnold. |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +# copy of this software and associated documentation files (the "Software"), |
| 8 | +# to deal in the Software without restriction, including without limitation |
| 9 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +# Software is furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +# DEALINGS IN THE SOFTWARE. |
| 23 | +# |
| 24 | +################################################################## |
| 25 | + |
| 26 | +"""FIX protocol simulator and control APIs.""" |
| 27 | + |
| 28 | +import logging |
| 29 | +import os |
| 30 | +import stat |
| 31 | +from .proxy import FixToolProxy |
| 32 | +from .version import VERSION |
| 33 | + |
| 34 | + |
| 35 | +def spawn_agent(): |
| 36 | + """Create a new agent, and associated proxy. |
| 37 | +
|
| 38 | + :returns: Reference to proxy, or None on error.""" |
| 39 | + |
| 40 | + # Spawned agents are spawned from the calling process, and usually |
| 41 | + # dedicated to it. It's possible to contact a spawned agent from |
| 42 | + # an independent process, but that's not the intended use case. |
| 43 | + # |
| 44 | + # When the agent starts, it is allocated an ephemeral port number. |
| 45 | + # That port number is reported on stdout from the process, and |
| 46 | + # can be read by the proxy which subsequently connects to it on |
| 47 | + # that port. |
| 48 | + # |
| 49 | + # The agent can be explicitly shutdown at any time by the proxy, |
| 50 | + # but will also be implicitly killed if the proxy instance is |
| 51 | + # deleted. |
| 52 | + |
| 53 | + # Look for fixtool-agent in PATH, then parent dirs of $CWD |
| 54 | + path = os.getenv("PATH", "") |
| 55 | + path_dirs = path.split(os.pathsep) |
| 56 | + cwd = os.path.realpath(os.curdir) |
| 57 | + while cwd != "/": |
| 58 | + path_dirs.append(cwd) |
| 59 | + cwd = os.path.dirname(cwd) |
| 60 | + |
| 61 | + fixtool_agent = None |
| 62 | + for dir_name in path_dirs: |
| 63 | + file_name = os.path.join(dir_name, "fixtool-agent") |
| 64 | + if os.path.exists(file_name): |
| 65 | + mode = os.stat(file_name).st_mode |
| 66 | + if mode & stat.S_IXUSR or \ |
| 67 | + mode & stat.S_IXGRP or \ |
| 68 | + mode & stat.S_IXOTH: |
| 69 | + fixtool_agent = file_name |
| 70 | + break |
| 71 | + |
| 72 | + if not fixtool_agent: |
| 73 | + logging.error("Unable to find agent executable") |
| 74 | + return None |
| 75 | + |
| 76 | + logging.info("Using agent: %s", fixtool_agent) |
| 77 | + |
| 78 | + agent = os.popen(fixtool_agent + ' start') |
| 79 | + status = agent.readline() |
| 80 | + agent.close() # Just the parent; the forked agent is still running |
| 81 | + |
| 82 | + logging.info("Agent output: %s", status) |
| 83 | + |
| 84 | + if status[:2] != "OK": |
| 85 | + logging.error("Failed to start agent: %s", status) |
| 86 | + return None |
| 87 | + |
| 88 | + try: |
| 89 | + port = int(status[3:]) |
| 90 | + except ValueError: |
| 91 | + logging.error("Unable to read port number from agent output") |
| 92 | + return None |
| 93 | + |
| 94 | + agent_proxy = FixToolProxy("localhost", port) |
| 95 | + return agent_proxy |
| 96 | + |
| 97 | + |
| 98 | +def connect_agent(host: str, port: int): |
| 99 | + """Create a proxy, and connect it to an existing agent. |
| 100 | +
|
| 101 | + :param host: String host name or IP address for the agent. |
| 102 | + :param port: Integer TCP port number for the agent.""" |
| 103 | + |
| 104 | + agent_proxy = FixToolProxy(host, port) |
| 105 | + return agent_proxy |
0 commit comments