summaryrefslogtreecommitdiff
path: root/java/netsvcs/Naming/NameReply.java
blob: 52ebb1115745974b9d631de038c9064e6de26c5d (plain)
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
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameReply.java
 *
 * Used by the naming server to give quick status messages
 * to the client.  This is only used to signal the success or
 * failure of bind and unbind requests.  The error number is
 * unused (same in C++ version?).
 *
 *@see netsvcs.Naming.NameHandler
 *
 *@author Everett Anderson
 *
 *************************************************/
package netsvcs.Naming;

import java.io.*;
import java.util.*;
import JACE.OS.*;
import JACE.Connection.*;
import JACE.Reactor.*;
import JACE.ASX.*;
import JACE.SOCK_SAP.*;

public class NameReply
{
  // Success and failure constants
  public final static int SUCCESS = 1;
  public final static int FAILURE = 2;

  /**
   * Default Constructor
   */
  public NameReply () 
    { 
      this.type_ = this.SUCCESS;
      this.errno_ = 0;
    }

  /**
   * Constructor
   *
   *@param type    Success or failure
   *@param err     Error number (unused)
   */
  public NameReply (int type, int err)
    {
      this.type_ = type;
      this.errno_ = err;
    }

  /**
   * Length accessor
   */
  int length() 
    { return this.length_; }

  /**
   * Type accessor -- success or failure
   */
  int type()
    { return this.type_; }

  /**
   * Error number accessor 
   */
  int errno()
    { return this.errno_; }

  /** 
   * Set type
   * @param type    New type
   */
  void type(int type)
    { this.type_ = type; }

  /**
   * Set error number
   * @param errno     New error number
   */
  void errno(int errno)
    { this.errno_ = errno; }

  /**
   * Send this data to the given SOCKStream
   *
   *@param sock     SOCKStream to send to
   */
  public void streamOutTo (JACE.SOCK_SAP.SOCKStream sock) throws IOException
    {
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(bout);
      
      dos.writeInt(this.length_);
      dos.writeInt(this.type_);
      dos.writeInt(this.errno_);

      dos.flush();

      byte[] array = bout.toByteArray();

      sock.sendN(array, 0, array.length);
    }

  /**
   * Fill the fields of this instance from data in the socket
   *
   *@param sock     SOCKStream to read from
   */
  public void streamInFrom (JACE.SOCK_SAP.SOCKStream sock) throws IOException
    {
      DataInputStream dis = new DataInputStream(sock.inputStream());

      this.streamInFrom(dis);
    }

  /**
   * Send this data to the given DataInputStream (which should be buffered)
   *
   *@param dis      DataInputStream to use
   */
  public void streamInFrom (DataInputStream dis) throws IOException
    {
      int length = dis.readInt();
      
      if (length != this.length_) 
	throw new IOException("Incorrect NameReply length");
      
      type_ = dis.readInt();
      errno_ = dis.readInt();
    }
  
  final static int length_ = 12;

  int type_;
  int errno_;
}