1
/* ``The contents of this file are subject to the Erlang Public License,
4
* Copyright Ericsson AB 2000-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
2
7
* Version 1.1, (the "License"); you may not use this file except in
3
8
* compliance with the License. You should have received a copy of the
4
9
* Erlang Public License along with this software. If not, it can be
5
* retrieved via the world wide web at http://www.erlang.org/.
10
* retrieved online at http://www.erlang.org/.
7
12
* Software distributed under the License is distributed on an "AS IS"
8
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
14
* the License for the specific language governing rights and limitations
10
15
* under the License.
12
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
* AB. All Rights Reserved.''
18
19
package com.ericsson.otp.erlang;
20
import java.net.InetAddress;
21
import java.net.UnknownHostException;
21
import java.io.BufferedReader;
23
22
import java.io.File;
24
23
import java.io.FileReader;
25
24
import java.io.IOException;
26
import java.io.BufferedReader;
25
import java.net.InetAddress;
26
import java.net.UnknownHostException;
29
* <p> Represents an OTP node. </p>
31
* <p> About nodenames: Erlang nodenames consist of two components, an
32
* alivename and a hostname separated by '@'. Additionally, there are
33
* two nodename formats: short and long. Short names are of the form
34
* "alive@hostname", while long names are of the form
35
* "alive@host.fully.qualified.domainname". Erlang has special
36
* requirements regarding the use of the short and long formats, in
37
* particular they cannot be mixed freely in a network of
38
* communicating nodes, however Jinterface makes no distinction. See
39
* the Erlang documentation for more information about nodenames. </p>
41
* <p> The constructors for the AbstractNode classes will create names
42
* exactly as you provide them as long as the name contains '@'. If
43
* the string you provide contains no '@', it will be treated as an
44
* alivename and the name of the local host will be appended,
45
* resulting in a shortname. Nodenames longer than 255 characters will
46
* be truncated without warning. </p>
48
* <p> Upon initialization, this class attempts to read the file
49
* .erlang.cookie in the user's home directory, and uses the trimmed
50
* first line of the file as the default cookie by those constructors
51
* lacking a cookie argument. If for any reason the file cannot be
52
* found or read, the default cookie will be set to the empty string
53
* (""). The location of a user's home directory is determined using
54
* the system property "user.home", which may not be automatically set
55
* on all platforms. </p>
57
* <p> Instances of this class cannot be created directly, use one of
58
* the subclasses instead. </p>
30
* Represents an OTP node.
34
* About nodenames: Erlang nodenames consist of two components, an alivename and
35
* a hostname separated by '@'. Additionally, there are two nodename formats:
36
* short and long. Short names are of the form "alive@hostname", while long
37
* names are of the form "alive@host.fully.qualified.domainname". Erlang has
38
* special requirements regarding the use of the short and long formats, in
39
* particular they cannot be mixed freely in a network of communicating nodes,
40
* however Jinterface makes no distinction. See the Erlang documentation for
41
* more information about nodenames.
45
* The constructors for the AbstractNode classes will create names exactly as
46
* you provide them as long as the name contains '@'. If the string you provide
47
* contains no '@', it will be treated as an alivename and the name of the local
48
* host will be appended, resulting in a shortname. Nodenames longer than 255
49
* characters will be truncated without warning.
53
* Upon initialization, this class attempts to read the file .erlang.cookie in
54
* the user's home directory, and uses the trimmed first line of the file as the
55
* default cookie by those constructors lacking a cookie argument. If for any
56
* reason the file cannot be found or read, the default cookie will be set to
57
* the empty string (""). The location of a user's home directory is determined
58
* using the system property "user.home", which may not be automatically set on
63
* Instances of this class cannot be created directly, use one of the subclasses
60
67
public class AbstractNode {
61
static String localHost = null;
66
static String defaultCookie = null;
69
static final int NTYPE_R6 = 110; // 'n' post-r5, all nodes
70
static final int NTYPE_R4_ERLANG = 109; // 'm' Only for source compatibility
71
static final int NTYPE_R4_HIDDEN = 104; // 'h' Only for source compatibility
73
// Node capability flags
74
static final int dFlagPublished = 1;
75
static final int dFlagAtomCache = 2;
76
static final int dFlagExtendedReferences = 4;
77
static final int dFlagDistMonitor = 8;
78
static final int dFlagFunTags = 16;
79
static final int dFlagExtendedPidsPorts = 256;
80
static final int dFlagBitBinaries = 1024;
81
static final int dFlagNewFloats = 2048;
84
int proto = 0; // tcp/ip
85
int distHigh = 5; // Cannot talk to nodes before R6
86
int distLow = 5; // Cannot talk to nodes before R6
88
int flags = dFlagExtendedReferences|dFlagExtendedPidsPorts
89
|dFlagBitBinaries|dFlagNewFloats;
91
/* initialize hostname and default cookie */
94
localHost = InetAddress.getLocalHost().getHostName();
95
/* Make sure it's a short name, i.e. strip of everything after first '.' */
96
int dot = localHost.indexOf(".");
97
if (dot != -1) localHost = localHost.substring(0,dot);
99
catch (UnknownHostException e) {
100
localHost = "localhost";
103
String dotCookieFilename = System.getProperty("user.home") + File.separator + ".erlang.cookie";
104
BufferedReader br = null;
107
File dotCookieFile = new File(dotCookieFilename);
109
br = new BufferedReader(new FileReader(dotCookieFile));
110
defaultCookie = br.readLine().trim();
112
catch (IOException e) {
117
if (br != null) br.close();
118
} catch (IOException e) {}
122
protected AbstractNode() {
126
* Create a node with the given name and the default cookie.
128
protected AbstractNode(String node) {
129
this(node,defaultCookie);
133
* Create a node with the given name and cookie.
135
protected AbstractNode(String name, String cookie) {
136
this.cookie = cookie;
138
int i = name.indexOf('@',0);
144
alive = name.substring(0,i);
145
host = name.substring(i+1,name.length());
148
if (alive.length() > 0xff) {
149
alive = alive.substring(0,0xff);
152
node = alive + "@" + host;
156
* Get the name of this node.
158
* @return the name of the node represented by this object.
160
public String node() {
165
* Get the hostname part of the nodename. Nodenames are composed of
166
* two parts, an alivename and a hostname, separated by '@'. This
167
* method returns the part of the nodename following the '@'.
169
* @return the hostname component of the nodename.
171
public String host() {
176
* Get the alivename part of the hostname. Nodenames are composed of
177
* two parts, an alivename and a hostname, separated by '@'. This
178
* method returns the part of the nodename preceding the '@'.
180
* @return the alivename component of the nodename.
182
public String alive() {
187
* Get the authorization cookie used by this node.
189
* @return the authorization cookie used by this node.
191
public String cookie() {
210
// package scope: useless information?
221
* Set the authorization cookie used by this node.
223
* @return the previous authorization cookie used by this node.
225
public String setCookie(String cookie) {
226
String prev = this.cookie;
227
this.cookie = cookie;
231
public String toString() {
68
static String localHost = null;
73
static String defaultCookie = null;
76
static final int NTYPE_R6 = 110; // 'n' post-r5, all nodes
77
static final int NTYPE_R4_ERLANG = 109; // 'm' Only for source compatibility
78
static final int NTYPE_R4_HIDDEN = 104; // 'h' Only for source compatibility
80
// Node capability flags
81
static final int dFlagPublished = 1;
82
static final int dFlagAtomCache = 2;
83
static final int dFlagExtendedReferences = 4;
84
static final int dFlagDistMonitor = 8;
85
static final int dFlagFunTags = 0x10;
86
static final int dFlagDistMonitorName = 0x20; // NOT USED
87
static final int dFlagHiddenAtomCache = 0x40; // NOT SUPPORTED
88
static final int dflagNewFunTags = 0x80;
89
static final int dFlagExtendedPidsPorts = 0x100;
90
static final int dFlagExportPtrTag = 0x200; // NOT SUPPORTED
91
static final int dFlagBitBinaries = 0x400;
92
static final int dFlagNewFloats = 0x800;
95
int proto = 0; // tcp/ip
96
int distHigh = 5; // Cannot talk to nodes before R6
97
int distLow = 5; // Cannot talk to nodes before R6
99
int flags = dFlagExtendedReferences | dFlagExtendedPidsPorts
100
| dFlagBitBinaries | dFlagNewFloats | dFlagFunTags
103
/* initialize hostname and default cookie */
106
localHost = InetAddress.getLocalHost().getHostName();
108
* Make sure it's a short name, i.e. strip of everything after first
111
final int dot = localHost.indexOf(".");
113
localHost = localHost.substring(0, dot);
115
} catch (final UnknownHostException e) {
116
localHost = "localhost";
119
final String dotCookieFilename = System.getProperty("user.home")
120
+ File.separator + ".erlang.cookie";
121
BufferedReader br = null;
124
final File dotCookieFile = new File(dotCookieFilename);
126
br = new BufferedReader(new FileReader(dotCookieFile));
127
defaultCookie = br.readLine().trim();
128
} catch (final IOException e) {
135
} catch (final IOException e) {
140
protected AbstractNode() {
144
* Create a node with the given name and the default cookie.
146
protected AbstractNode(final String node) {
147
this(node, defaultCookie);
151
* Create a node with the given name and cookie.
153
protected AbstractNode(final String name, final String cookie) {
154
this.cookie = cookie;
156
final int i = name.indexOf('@', 0);
161
alive = name.substring(0, i);
162
host = name.substring(i + 1, name.length());
165
if (alive.length() > 0xff) {
166
alive = alive.substring(0, 0xff);
169
node = alive + "@" + host;
173
* Get the name of this node.
175
* @return the name of the node represented by this object.
177
public String node() {
182
* Get the hostname part of the nodename. Nodenames are composed of two
183
* parts, an alivename and a hostname, separated by '@'. This method returns
184
* the part of the nodename following the '@'.
186
* @return the hostname component of the nodename.
188
public String host() {
193
* Get the alivename part of the hostname. Nodenames are composed of two
194
* parts, an alivename and a hostname, separated by '@'. This method returns
195
* the part of the nodename preceding the '@'.
197
* @return the alivename component of the nodename.
199
public String alive() {
204
* Get the authorization cookie used by this node.
206
* @return the authorization cookie used by this node.
208
public String cookie() {
227
// package scope: useless information?
238
* Set the authorization cookie used by this node.
240
* @return the previous authorization cookie used by this node.
242
public String setCookie(final String cookie) {
243
final String prev = this.cookie;
244
this.cookie = cookie;
249
public String toString() {