Comparison

util/adminstream.lua @ 10875:09674bbb833f

util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Jun 2020 08:28:39 +0100
parent 10855:70ac7d23673d
child 10876:c01c39a2c7a2
comparison
equal deleted inserted replaced
10874:98c535531450 10875:09674bbb833f
134 end 134 end
135 end 135 end
136 136
137 --- Public methods 137 --- Public methods
138 138
139 local function new_connection(socket_path, listeners)
140 local have_unix, unix = pcall(require, "socket.unix");
141 if type(unix) ~= "table" then
142 have_unix = false;
143 end
144 local conn, sock;
145
146 return {
147 connect = function ()
148 if not have_unix then
149 return nil, "no unix socket support";
150 end
151 if sock or conn then
152 return nil, "already connected";
153 end
154 sock = unix.stream();
155 sock:settimeout(0);
156 local ok, err = sock:connect(socket_path);
157 if not ok then
158 return nil, err;
159 end
160 conn = server.wrapclient(sock, nil, nil, listeners, "*a");
161 return true;
162 end;
163 disconnect = function ()
164 if conn then
165 conn:close();
166 conn = nil;
167 end
168 if sock then
169 sock:close();
170 sock = nil;
171 end
172 return true;
173 end;
174 };
175 end
176
139 local function new_server(sessions, stanza_handler) 177 local function new_server(sessions, stanza_handler)
140 local listeners = {}; 178 local listeners = {};
141 179
142 function listeners.onconnect(conn) 180 function listeners.onconnect(conn)
143 log("debug", "New connection"); 181 log("debug", "New connection");
278 316
279 return client; 317 return client;
280 end 318 end
281 319
282 return { 320 return {
321 connection = new_connection;
283 server = new_server; 322 server = new_server;
284 client = new_client; 323 client = new_client;
285 }; 324 };