package com.sun.slp;
import java.util.*;
import java.io.*;
class CDAAdvert extends SrvLocMsgImpl {
ServiceURL URL = null;
long timestamp = 0;
Vector attrs = new Vector();
Hashtable authBlock = null;
String spis = null;
CDAAdvert(SrvLocHeader hdr, DataInputStream dis)
throws ServiceLocationException, IOException {
super(hdr, SrvLocHeader.DAAdvert);
this.initialize(dis);
}
protected void initialize(DataInputStream dis)
throws ServiceLocationException, IOException {
SLPHeaderV2 hdr = (SLPHeaderV2)getHeader();
byte[] tsBytes = new byte[4];
timestamp = getInt32(hdr, dis, tsBytes);
StringBuffer buf = new StringBuffer();
byte[] urlBytes = hdr.getString(buf, dis);
int lifetime = getDAURLLifetime();
String surl = buf.toString();
byte[] scopeBytes = hdr.getString(buf, dis);
hdr.scopes = hdr.parseCommaSeparatedListIn(buf.toString(), true);
hdr.unescapeScopeStrings(hdr.scopes);
DATable.validateScopes(hdr.scopes, hdr.locale);
byte[] attrBytes = hdr.parseAttributeVectorIn(attrs, dis, false);
byte[] spiBytes = hdr.getString(buf, dis);
spis = buf.toString();
Object[] message = new Object[9];
message[0] = tsBytes;
ByteArrayOutputStream abaos = new ByteArrayOutputStream();
hdr.putInteger(urlBytes.length, abaos);
message[1] = abaos.toByteArray();
message[2] = urlBytes;
abaos = new ByteArrayOutputStream();
hdr.putInteger(attrBytes.length, abaos);
message[3] = abaos.toByteArray();
message[4] = attrBytes;
abaos = new ByteArrayOutputStream();
hdr.putInteger(scopeBytes.length, abaos);
message[5] = abaos.toByteArray();
message[6] = scopeBytes;
abaos = new ByteArrayOutputStream();
hdr.putInteger(spiBytes.length, abaos);
message[7] = abaos.toByteArray();
message[8] = spiBytes;
authBlock = hdr.parseSignatureIn(message, dis);
if (authBlock != null) {
lifetime = AuthBlock.getShortestLifetime(authBlock);
}
try {
URL = new ServiceURL(surl, lifetime);
} catch (IllegalArgumentException ex) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"malformed_url",
new Object[] {ex.getMessage()});
}
ServiceType serviceType = URL.getServiceType();
if (!serviceType.equals(Defaults.DA_SERVICE_TYPE)) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"not_right_url",
new Object[] {URL, "DA"});
}
hdr.iNumReplies = 1;
}
static private long getInt32(SrvLocHeader hdr,
DataInputStream dis,
byte[] bytes)
throws ServiceLocationException, IOException {
bytes[0] = (byte)dis.read();
bytes[1] = (byte)dis.read();
bytes[2] = (byte)dis.read();
bytes[3] = (byte)dis.read();
long a = (long)((char)bytes[0] & 0xFF);
long b = (long)((char)bytes[1] & 0xFF);
long c = (long)((char)bytes[2] & 0xFF);
long d = (long)((char)bytes[3] & 0xFF);
long i = a << 24;
i += b << 16;
i += c << 8;
i += d;
hdr.nbytes += 4;
return i;
}
boolean isGoingDown() {
return (timestamp == 0);
}
boolean isUnsolicited() {
return (hdr.xid == 0);
}
void setIsUnsolicited(boolean flag) {
}
private int getDAURLLifetime() {
SLPConfig config = SLPConfig.getSLPConfig();
int disInt = config.getActiveDiscoveryInterval();
int granInt = config.getActiveDiscoveryGranularity();
if (disInt <= 0) {
return ServiceURL.LIFETIME_MAXIMUM;
} else {
int lifetime = disInt + granInt;
return
(lifetime > ServiceURL.LIFETIME_MAXIMUM ?
ServiceURL.LIFETIME_MAXIMUM:lifetime);
}
}
}