-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathnode.ex
More file actions
358 lines (277 loc) · 10.4 KB
/
node.ex
File metadata and controls
358 lines (277 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2021 The Elixir Team
# SPDX-FileCopyrightText: 2012 Plataformatec
defmodule Node do
@moduledoc """
Functions related to VM nodes.
Some of the functions in this module are inlined by the compiler,
similar to functions in the `Kernel` module and they are explicitly
marked in their docs as "inlined by the compiler". For more information
about inlined functions, check out the `Kernel` module.
"""
@type t :: node
@doc """
Turns a non-distributed node into a distributed node.
This functionality starts the `:net_kernel` and other related
processes.
This function is rarely invoked in practice. Instead, nodes are
named and started via the command line by using the `--sname` and
`--name` flags. If you need to use this function to dynamically
name a node, please make sure the `epmd` operating system process
is running by calling `epmd -daemon`, such as `System.cmd("epmd", ["-daemon"])`.
Invoking this function when the distribution has already been started,
either via the command line interface or dynamically, will return an
error.
## Examples
{:ok, pid} = Node.start(:example, name_domain: :shortnames, hidden: true)
## Options
Currently supported options are:
* `:name_domain` - determines the host name part of the node name. If `:longnames`,
fully qualified domain names will be used which also is the default.
If `:shortnames`, only the short name of the host will be used.
* `:net_ticktime` - The tick time to use in seconds. Defaults to the value of the
`net_ticktime` configuration under Erlang's `kernel` application.
See [the `kernel` documentation](https://www.erlang.org/doc/apps/kernel/kernel_app.html)
for more information.
* `net_tickintensity` - The tick intensity to use. Defaults to the value of the
`net_tickintensity` configuration under Erlang's `kernel` application.
See [the `kernel` documentation](https://www.erlang.org/doc/apps/kernel/kernel_app.html)
for more information.
* `:dist_listen` - Enable or disable listening for incoming connections.
Defaults to the value given to the `--erl` flag, otherwise it defaults to `true`.
Note that `dist_listen: false` implies `hidden: true`.
* `:hidden` - Enable or disable hidden node. Defaults to `true` if the `--hidden`
flag is given to `elixir`'s CLI (or via the `--erl` flag), otherwise it
defaults to `false`.
If `name` is set to `:undefined`, the distribution will be started to request a
dynamic node name from the first node it connects to. Setting `name` to
`:undefined` also implies options `dist_listen: false, hidden: true`.
"""
@spec start(node,
name_domain: :shortnames | :longnames,
net_ticktime: pos_integer(),
net_tickintensity: 4..1000,
dist_listen: boolean(),
hidden: boolean()
) :: {:ok, pid} | {:error, term}
def start(name, opts \\ [])
def start(name, opts) when is_list(opts) do
:net_kernel.start(name, Map.new(opts))
end
# TODO: Deprecate me on Elixir v1.23
def start(name, type) when is_atom(type) do
:net_kernel.start([name, type, 15_000])
end
# TODO: Deprecate me on Elixir v1.23
@doc false
def start(name, type, tick_time) do
:net_kernel.start([name, type, tick_time])
end
@doc """
Turns a distributed node into a non-distributed node.
For other nodes in the network, this is the same as the node going down.
Only possible when the node was started with `Node.start/2`, otherwise
returns `{:error, :not_allowed}`. Returns `{:error, :not_found}` if the
local node is not alive.
"""
@spec stop() :: :ok | {:error, :not_allowed | :not_found}
def stop() do
:net_kernel.stop()
end
@doc """
Returns the current node.
It returns the same as the built-in `node()`.
"""
@spec self :: t
def self do
:erlang.node()
end
@doc """
Returns `true` if the local node is alive.
That is, if the node can be part of a distributed system.
"""
@spec alive? :: boolean
def alive? do
:erlang.is_alive()
end
@doc """
Returns a list of all visible nodes in the system, excluding
the local node.
Same as `list(:visible)`.
Inlined by the compiler.
"""
@spec list :: [t]
def list do
:erlang.nodes()
end
@doc """
Returns a list of nodes according to argument given.
The result returned when the argument is a list, is the list of nodes
satisfying the disjunction(s) of the list elements.
For more information, see `:erlang.nodes/1`.
Inlined by the compiler.
"""
@type state :: :visible | :hidden | :connected | :this | :known
@spec list(state | [state]) :: [t]
def list(args) do
:erlang.nodes(args)
end
@doc """
Monitors the status of the node.
If `flag` is `true`, monitoring is turned on.
If `flag` is `false`, monitoring is turned off.
For more information, see `:erlang.monitor_node/2`.
For monitoring status changes of all nodes, see `:net_kernel.monitor_nodes/2`.
"""
@spec monitor(t, boolean) :: true
def monitor(node, flag) do
:erlang.monitor_node(node, flag)
end
@doc """
Behaves as `monitor/2` except that it allows an extra
option to be given, namely `:allow_passive_connect`.
For more information, see `:erlang.monitor_node/3`.
For monitoring status changes of all nodes, see `:net_kernel.monitor_nodes/2`.
"""
@spec monitor(t, boolean, [:allow_passive_connect]) :: true
def monitor(node, flag, options) do
:erlang.monitor_node(node, flag, options)
end
@doc """
Tries to set up a connection to node.
Returns `:pang` if it fails, or `:pong` if it is successful.
## Examples
iex> Node.ping(:unknown_node)
:pang
"""
@spec ping(t) :: :pong | :pang
def ping(node) do
:net_adm.ping(node)
end
@doc """
Forces the disconnection of a node.
This will appear to the `node` as if the local node has crashed.
This function is mainly used in the Erlang network authentication
protocols. Returns `true` if disconnection succeeds, otherwise `false`.
If the local node is not alive, the function returns `:ignored`.
For more information, see `:erlang.disconnect_node/1`.
"""
@spec disconnect(t) :: boolean | :ignored
def disconnect(node) do
:erlang.disconnect_node(node)
end
@doc """
Establishes a connection to `node`.
Returns `true` if successful, `false` if not, and the atom
`:ignored` if the local node is not alive.
For more information, see `:net_kernel.connect_node/1`.
"""
@spec connect(t) :: boolean | :ignored
def connect(node) do
:net_kernel.connect_node(node)
end
@doc """
Returns the PID of a new process started by the application of `fun`
on `node`. If `node` does not exist, a useless PID is returned.
For the list of available options, see `:erlang.spawn/2`.
Inlined by the compiler.
"""
@spec spawn(t, (-> any)) :: pid
def spawn(node, fun) do
:erlang.spawn(node, fun)
end
@doc """
Returns the PID of a new process started by the application of `fun`
on `node`.
If `node` does not exist, a useless PID is returned.
For the list of available options, see `:erlang.spawn_opt/3`.
Inlined by the compiler.
"""
@spec spawn(t, (-> any), Process.spawn_opts()) :: pid | {pid, reference}
def spawn(node, fun, opts) do
:erlang.spawn_opt(node, fun, opts)
end
@doc """
Returns the PID of a new process started by the application of
`module.function(args)` on `node`.
If `node` does not exist, a useless PID is returned.
For the list of available options, see `:erlang.spawn/4`.
Inlined by the compiler.
"""
@spec spawn(t, module, atom, [any]) :: pid
def spawn(node, module, fun, args) do
:erlang.spawn(node, module, fun, args)
end
@doc """
Returns the PID of a new process started by the application of
`module.function(args)` on `node`.
If `node` does not exist, a useless PID is returned.
For the list of available options, see `:erlang.spawn_opt/5`.
Inlined by the compiler.
"""
@spec spawn(t, module, atom, [any], Process.spawn_opts()) :: pid | {pid, reference}
def spawn(node, module, fun, args, opts) do
:erlang.spawn_opt(node, module, fun, args, opts)
end
@doc """
Returns the PID of a new linked process started by the application of `fun` on `node`.
A link is created between the calling process and the new process, atomically.
If `node` does not exist, a useless PID is returned (and due to the link, an exit
signal with exit reason `:noconnection` will be received).
Inlined by the compiler.
"""
@spec spawn_link(t, (-> any)) :: pid
def spawn_link(node, fun) do
:erlang.spawn_link(node, fun)
end
@doc """
Returns the PID of a new linked process started by the application of
`module.function(args)` on `node`.
A link is created between the calling process and the new process, atomically.
If `node` does not exist, a useless PID is returned (and due to the link, an exit
signal with exit reason `:noconnection` will be received).
Inlined by the compiler.
"""
@spec spawn_link(t, module, atom, [any]) :: pid
def spawn_link(node, module, fun, args) do
:erlang.spawn_link(node, module, fun, args)
end
@doc """
Spawns the given function on a node, monitors it and returns its PID
and monitoring reference.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, (-> any)) :: {pid, reference}
def spawn_monitor(node, fun) do
:erlang.spawn_monitor(node, fun)
end
@doc """
Spawns the given module and function passing the given args on a node,
monitors it and returns its PID and monitoring reference.
Inlined by the compiler.
"""
@doc since: "1.14.0"
@spec spawn_monitor(t, module, atom, [any]) :: {pid, reference}
def spawn_monitor(node, module, fun, args) do
:erlang.spawn_monitor(node, module, fun, args)
end
@doc """
Sets the magic cookie of `node` to the atom `cookie`.
The default node is `Node.self/0`, the local node. If `node` is the local node,
the function also sets the cookie of all other unknown nodes to `cookie`.
This function will raise `FunctionClauseError` if the given `node` is not alive.
"""
@spec set_cookie(t, atom) :: true
def set_cookie(node \\ Node.self(), cookie) when is_atom(cookie) do
:erlang.set_cookie(node, cookie)
end
@doc """
Returns the magic cookie of the local node.
Returns the cookie if the node is alive, otherwise `:nocookie`.
"""
@spec get_cookie() :: atom
def get_cookie() do
:erlang.get_cookie()
end
end