~statik/ubuntu/maverick/erlang/erlang-merge-testing

« back to all changes in this revision

Viewing changes to lib/jinterface/java_src/com/ericsson/otp/erlang/AbstractNode.java

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-01 10:14:38 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090501101438-6qlr6rsdxgyzrg2z
Tags: 1:13.b-dfsg-2
* Cleaned up patches: removed unneeded patch which helped to support
  different SCTP library versions, made sure that changes for m68k
  architecture applied only when building on this architecture.
* Removed duplicated information from binary packages descriptions.
* Don't require libsctp-dev build-dependency on solaris-i386 architecture
  which allows to build Erlang on Nexenta (thanks to Tim Spriggs for
  the suggestion).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ``The contents of this file are subject to the Erlang Public License,
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2000-2009. All Rights Reserved.
 
5
 * 
 
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/.
6
11
 * 
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.
11
16
 * 
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.''
15
 
 * 
16
 
 *     $Id$
 
17
 * %CopyrightEnd% 
17
18
 */
18
19
package com.ericsson.otp.erlang;
19
20
 
20
 
import java.net.InetAddress;
21
 
import java.net.UnknownHostException;
22
 
 
 
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;
27
 
  
 
25
import java.net.InetAddress;
 
26
import java.net.UnknownHostException;
 
27
 
28
28
/**
29
 
 * <p> Represents an OTP node. </p>
30
 
 * 
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>
40
 
 * 
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>
47
 
 *
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>
56
 
 *
57
 
 * <p> Instances of this class cannot be created directly, use one of
58
 
 * the subclasses instead. </p>
59
 
 **/
 
29
 * <p>
 
30
 * Represents an OTP node.
 
31
 * </p>
 
32
 * 
 
33
 * <p>
 
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.
 
42
 * </p>
 
43
 * 
 
44
 * <p>
 
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.
 
50
 * </p>
 
51
 * 
 
52
 * <p>
 
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
 
59
 * all platforms.
 
60
 * </p>
 
61
 * 
 
62
 * <p>
 
63
 * Instances of this class cannot be created directly, use one of the subclasses
 
64
 * instead.
 
65
 * </p>
 
66
 */
60
67
public class AbstractNode {
61
 
  static String localHost = null;
62
 
  String node;
63
 
  String host;
64
 
  String alive;
65
 
  String cookie;
66
 
  static String defaultCookie = null;
67
 
 
68
 
  // Node types
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
72
 
  
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;
82
 
 
83
 
  int ntype = NTYPE_R6;
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
87
 
  int creation = 0;
88
 
  int flags = dFlagExtendedReferences|dFlagExtendedPidsPorts
89
 
      |dFlagBitBinaries|dFlagNewFloats;
90
 
 
91
 
  /* initialize hostname and default cookie */
92
 
  static {
93
 
    try {
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);
98
 
    }
99
 
    catch (UnknownHostException e) {
100
 
      localHost = "localhost";
101
 
    }
102
 
 
103
 
    String dotCookieFilename = System.getProperty("user.home") + File.separator + ".erlang.cookie";
104
 
    BufferedReader br = null;
105
 
 
106
 
    try {
107
 
      File dotCookieFile = new File(dotCookieFilename);
108
 
 
109
 
      br = new BufferedReader(new FileReader(dotCookieFile));
110
 
      defaultCookie = br.readLine().trim();
111
 
    }
112
 
    catch (IOException e) {
113
 
      defaultCookie = "";
114
 
    }
115
 
    finally {
116
 
      try {
117
 
        if (br != null) br.close();
118
 
      } catch (IOException e) {}
119
 
    }
120
 
  }
121
 
 
122
 
  protected AbstractNode() {
123
 
  }
124
 
  
125
 
  /**
126
 
   * Create a node with the given name and the default cookie.
127
 
   **/
128
 
  protected AbstractNode(String node) {
129
 
    this(node,defaultCookie);
130
 
  }
131
 
  
132
 
  /**
133
 
   * Create a node with the given name and cookie.
134
 
   **/
135
 
  protected AbstractNode(String name, String cookie) {
136
 
    this.cookie = cookie;
137
 
 
138
 
    int i = name.indexOf('@',0);
139
 
    if (i<0) {
140
 
      alive = name;
141
 
      host = localHost;
142
 
    }
143
 
    else {
144
 
      alive = name.substring(0,i);
145
 
      host = name.substring(i+1,name.length());
146
 
    }
147
 
 
148
 
    if (alive.length() > 0xff) {
149
 
      alive = alive.substring(0,0xff);
150
 
    }
151
 
 
152
 
    node = alive + "@" + host;
153
 
  }
154
 
 
155
 
  /**
156
 
   * Get the name of this node.
157
 
   *
158
 
   * @return the name of the node represented by this object.
159
 
   **/
160
 
  public String node() {
161
 
    return node;
162
 
  }
163
 
 
164
 
  /**
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 '@'.
168
 
   *
169
 
   * @return the hostname component of the nodename.
170
 
   **/
171
 
  public String host() {
172
 
    return host;
173
 
  }
174
 
 
175
 
  /**
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 '@'.
179
 
   *
180
 
   * @return the alivename component of the nodename.
181
 
   **/
182
 
  public String alive() {
183
 
    return alive;
184
 
  }
185
 
 
186
 
  /**
187
 
   * Get the authorization cookie used by this node.
188
 
   *
189
 
   * @return the authorization cookie used by this node.
190
 
   **/
191
 
  public String cookie() {
192
 
    return cookie;
193
 
  }
194
 
 
195
 
  // package scope
196
 
  int type() {
197
 
    return ntype;
198
 
  }
199
 
  
200
 
  // package scope
201
 
  int distHigh() {
202
 
    return distHigh;
203
 
  }
204
 
 
205
 
  // package scope
206
 
  int distLow() {
207
 
    return distLow;
208
 
  }
209
 
 
210
 
  // package scope: useless information?
211
 
  int proto() {
212
 
    return proto;
213
 
  }
214
 
 
215
 
  // package scope
216
 
  int creation() {
217
 
    return creation;
218
 
  }
219
 
 
220
 
  /**
221
 
   * Set the authorization cookie used by this node.
222
 
   *
223
 
   * @return the previous authorization cookie used by this node.
224
 
   **/
225
 
  public String setCookie(String cookie) {
226
 
    String prev = this.cookie;
227
 
    this.cookie = cookie;
228
 
    return prev;
229
 
  }
230
 
 
231
 
  public String toString() {
232
 
    return node();
233
 
  }
 
68
    static String localHost = null;
 
69
    String node;
 
70
    String host;
 
71
    String alive;
 
72
    String cookie;
 
73
    static String defaultCookie = null;
 
74
 
 
75
    // Node types
 
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
 
79
 
 
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;
 
93
 
 
94
    int ntype = NTYPE_R6;
 
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
 
98
    int creation = 0;
 
99
    int flags = dFlagExtendedReferences | dFlagExtendedPidsPorts
 
100
            | dFlagBitBinaries | dFlagNewFloats | dFlagFunTags
 
101
            | dflagNewFunTags;
 
102
 
 
103
    /* initialize hostname and default cookie */
 
104
    static {
 
105
        try {
 
106
            localHost = InetAddress.getLocalHost().getHostName();
 
107
            /*
 
108
             * Make sure it's a short name, i.e. strip of everything after first
 
109
             * '.'
 
110
             */
 
111
            final int dot = localHost.indexOf(".");
 
112
            if (dot != -1) {
 
113
                localHost = localHost.substring(0, dot);
 
114
            }
 
115
        } catch (final UnknownHostException e) {
 
116
            localHost = "localhost";
 
117
        }
 
118
 
 
119
        final String dotCookieFilename = System.getProperty("user.home")
 
120
                + File.separator + ".erlang.cookie";
 
121
        BufferedReader br = null;
 
122
 
 
123
        try {
 
124
            final File dotCookieFile = new File(dotCookieFilename);
 
125
 
 
126
            br = new BufferedReader(new FileReader(dotCookieFile));
 
127
            defaultCookie = br.readLine().trim();
 
128
        } catch (final IOException e) {
 
129
            defaultCookie = "";
 
130
        } finally {
 
131
            try {
 
132
                if (br != null) {
 
133
                    br.close();
 
134
                }
 
135
            } catch (final IOException e) {
 
136
            }
 
137
        }
 
138
    }
 
139
 
 
140
    protected AbstractNode() {
 
141
    }
 
142
 
 
143
    /**
 
144
     * Create a node with the given name and the default cookie.
 
145
     */
 
146
    protected AbstractNode(final String node) {
 
147
        this(node, defaultCookie);
 
148
    }
 
149
 
 
150
    /**
 
151
     * Create a node with the given name and cookie.
 
152
     */
 
153
    protected AbstractNode(final String name, final String cookie) {
 
154
        this.cookie = cookie;
 
155
 
 
156
        final int i = name.indexOf('@', 0);
 
157
        if (i < 0) {
 
158
            alive = name;
 
159
            host = localHost;
 
160
        } else {
 
161
            alive = name.substring(0, i);
 
162
            host = name.substring(i + 1, name.length());
 
163
        }
 
164
 
 
165
        if (alive.length() > 0xff) {
 
166
            alive = alive.substring(0, 0xff);
 
167
        }
 
168
 
 
169
        node = alive + "@" + host;
 
170
    }
 
171
 
 
172
    /**
 
173
     * Get the name of this node.
 
174
     * 
 
175
     * @return the name of the node represented by this object.
 
176
     */
 
177
    public String node() {
 
178
        return node;
 
179
    }
 
180
 
 
181
    /**
 
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 '@'.
 
185
     * 
 
186
     * @return the hostname component of the nodename.
 
187
     */
 
188
    public String host() {
 
189
        return host;
 
190
    }
 
191
 
 
192
    /**
 
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 '@'.
 
196
     * 
 
197
     * @return the alivename component of the nodename.
 
198
     */
 
199
    public String alive() {
 
200
        return alive;
 
201
    }
 
202
 
 
203
    /**
 
204
     * Get the authorization cookie used by this node.
 
205
     * 
 
206
     * @return the authorization cookie used by this node.
 
207
     */
 
208
    public String cookie() {
 
209
        return cookie;
 
210
    }
 
211
 
 
212
    // package scope
 
213
    int type() {
 
214
        return ntype;
 
215
    }
 
216
 
 
217
    // package scope
 
218
    int distHigh() {
 
219
        return distHigh;
 
220
    }
 
221
 
 
222
    // package scope
 
223
    int distLow() {
 
224
        return distLow;
 
225
    }
 
226
 
 
227
    // package scope: useless information?
 
228
    int proto() {
 
229
        return proto;
 
230
    }
 
231
 
 
232
    // package scope
 
233
    int creation() {
 
234
        return creation;
 
235
    }
 
236
 
 
237
    /**
 
238
     * Set the authorization cookie used by this node.
 
239
     * 
 
240
     * @return the previous authorization cookie used by this node.
 
241
     */
 
242
    public String setCookie(final String cookie) {
 
243
        final String prev = this.cookie;
 
244
        this.cookie = cookie;
 
245
        return prev;
 
246
    }
 
247
 
 
248
    @Override
 
249
    public String toString() {
 
250
        return node();
 
251
    }
234
252
}