1 /******************************************************************************* 2 3 Support for unix socket command handling. 4 5 Copyright: 6 Copyright (c) 2018 dunnhumby Germany GmbH. 7 All rights reserved. 8 9 License: 10 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 11 Alternatively, this file may be distributed under the terms of the Tango 12 3-Clause BSD License (see LICENSE_BSD.txt for details). 13 14 *******************************************************************************/ 15 16 module ocean.application.components.UnixSocketCommands; 17 18 /// ditto 19 public class UnixSocketCommands 20 { 21 import ocean.meta.types.Qualifiers; 22 import ocean.core.Enforce; 23 import ocean.io.select.EpollSelectDispatcher; 24 import ocean.text.convert.Integer; 25 import ocean.text.util.StringC; 26 import ocean.util.config.ConfigParser; 27 import ocean.net.server.unix.CommandRegistry; 28 import ocean.net.server.unix.UnixListener; 29 30 /// Path to create the unix socket. 31 private istring path; 32 33 /// Mode to apply to the unix socket after binding 34 private int mode = -1; 35 36 /// Command registry to handle the commands from the client 37 public CommandsRegistry commands; 38 39 /// Handler delegate 40 public alias CommandsRegistry.Handler Handler; 41 42 /// Interactive handler delegate 43 public alias CommandsRegistry.InteractiveHandler InteractiveHandler; 44 45 /// RawSocketHandler delegate 46 public alias CommandsRegistry.RawSocketHandler RawSocketHandler; 47 48 /// Unix listener with dynamic command handling. 49 private UnixSocketListener!(CommandsRegistry) unix_listener; 50 51 /*************************************************************************** 52 53 Constructor 54 55 ***************************************************************************/ 56 57 public this ( ) 58 { 59 this.commands = new CommandsRegistry; 60 } 61 62 /*************************************************************************** 63 64 Initializes the socket listener and registers it with the provided epoll 65 selector. 66 67 Params: 68 epoll = Epoll instance. 69 70 ***************************************************************************/ 71 72 public void startEventHandling ( EpollSelectDispatcher epoll ) 73 { 74 if (this.path.length) 75 { 76 this.unix_listener = new UnixSocketListener!(CommandsRegistry) 77 (this.path, epoll, this.commands, this.mode); 78 epoll.register(this.unix_listener); 79 } 80 } 81 82 /*************************************************************************** 83 84 Setup the unix socket listener if the config for the socket path exists. 85 86 Params: 87 config = configuration instance 88 89 ***************************************************************************/ 90 91 public void parseConfig ( ConfigParser config ) 92 { 93 this.path = config.get("UNIX_SOCKET", "path", ""); 94 95 istring modestr = config.get("UNIX_SOCKET", "mode", ""); 96 if (modestr.length) 97 { 98 enforce(toInteger(modestr, this.mode, 8), 99 "Invalid mode for UnixSocket"); 100 } 101 } 102 103 /*************************************************************************** 104 105 Shut down the unix listener. 106 107 ***************************************************************************/ 108 109 public void shutdown ( ) 110 { 111 if (this.unix_listener !is null) 112 this.unix_listener.shutdown(); 113 } 114 }