summaryrefslogtreecommitdiff
path: root/java/JACE/netsvcs/Naming/NameReply.java
blob: d20c9ed05f0d5858ce6253768a9bc6146a96954b (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameReply.java
 *
 *************************************************/
package JACE.netsvcs.Naming;

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

/**
 * 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 seems
 * to be unused in the C++ version.
 *
 *@see NameHandler
 *@author Everett Anderson
 *
 */
public class NameReply
{
  /** Successful operation indicator */
  public final static int SUCCESS = 0;

  /** Failed operation indicator */
  public final static int FAILURE = -1;

  /**
   * Default Constructor (success, errno 0)
   */
  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
  {
    streamOutTo (sock.dataOutputStream ());
  }

  /**
   * Send this instance to the given DataOutputStream.
   */
  public void streamOutTo (DataOutputStream dos) throws IOException
  {
    dos.writeInt(this.length_);
    dos.writeInt(this.type_);
    dos.writeInt(this.errno_);
    
    dos.flush();
  }

  /**
   * Send this instance to the given OutputStream.
   */
  public void streamOutTo (OutputStream os) throws IOException
  {
    BufferedOutputStream bos = new BufferedOutputStream (os);
    DataOutputStream dos = new DataOutputStream (bos);

    streamOutTo (dos);
  }

  /**
   * 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
  {
    this.streamInFrom(sock.dataInputStream ());
  }

  /**
   * Fill this instance from the 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();
  }

  /**
   * Fill this instance from the given InputStream.
   */
  public void streamInFrom (InputStream is) throws IOException
  {
    BufferedInputStream bis = new BufferedInputStream (is);
    DataInputStream dis = new DataInputStream (bis);

    streamInFrom (dis);
  }

  final static int length_ = 12;

  int type_;
  int errno_;
}