//: TLink.java
package de.desy.tine.addrUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import de.desy.tine.client.TLink;
import de.desy.tine.csvUtils.*;
import de.desy.tine.dataUtils.*;
import de.desy.tine.definitions.*;
import de.desy.tine.server.logger.MsgLog;
import de.desy.tine.startup.TInitializerFactory;
import de.desy.tine.types.NAME32;
public final class TFecEntry
{
String fecName;
//
//// Not used (mkadunc)
//
// String srvLocation;
// String srvDescription;
//
csv fecFile;
public static final short BCAST_ID = 255;
public static final short ENS_ID = 4096;
public int fecPortOffset;
public boolean fecRegistered;
public InetAddress fecHost;
public FECInfo info = null;
private int tineProtocol = TTransport.DEFAULT_PROTOCOL_LEVEL;
public TFecEntry(SrvAddr srv)
{
try
{
fecName = srv.fecName;
fecHost = InetAddress.getByName(srv.ipAddr.trim());
fecPortOffset = srv.getPortOffset();
tineProtocol = srv.tineProtocol;
fecRegistered = true;
}
catch (UnknownHostException e)
{
MsgLog.log("TFecEntry","cannot find host : "+e.getMessage(),TErrorList.code_failure,e,0);
fecRegistered = false;
}
}
public TFecEntry(String name)
{
fecName = new String(name);
try
{ // acquire address from the initializer (if available) ...
if (name.compareTo("ENS") == 0)
{
String host = TInitializerFactory.getInstance().getInitializer().getENSAddress();
if (host != null)
{
fecHost = InetAddress.getByName(host);
fecPortOffset = 0;
fecRegistered = true;
}
}
if (name.compareTo("GENS") == 0)
{
String host = TInitializerFactory.getInstance().getInitializer().getENSAddress();
if (host != null)
{
fecHost = InetAddress.getByName(host);
fecPortOffset = 101;
fecRegistered = true;
}
}
}
catch (UnknownHostException e)
{
MsgLog.log("TFecEntry","cannot find host : "+e.getMessage(),TErrorList.code_failure,e,0);
fecRegistered = false;
}
}
// csv file FECNAME handler:
class fecNameHndlr implements csvHandler
{
String target;
fecNameHndlr(String name) { target = name; }
public int process(String strValue,int index)
{
if (strValue.compareTo(target) == 0)
{ // found it, halt iteration !
fecName = new String(target);
fecRegistered = true;
return -1;
}
return 0;
}
}
// csv file IP Address handler:
class ipAddrHndlr implements csvHandler
{
public int process(String strValue,int index)
{
try
{
fecHost = InetAddress.getByName(strValue);
}
catch (IOException e)
{
MsgLog.log("TFecEntry.ipAddrHndlr",e.getMessage(),TErrorList.code_failure,e,0);
}
return 0;
}
}
// csv file Port Offset Handler:
class portHndlr implements csvHandler
{
public int process(String strValue,int index)
{
fecPortOffset = (new Integer(strValue)).intValue();
return 0;
}
}
class protHndlr implements csvHandler
{
public int process(String strValue,int index)
{
tineProtocol = (new Integer(strValue)).intValue();
return 0;
}
}
public TFecEntry(BufferedReader in,String name)
{
if (in == null)
{
fecRegistered = false;
return;
}
// register the csv database structure:
csvColumn[] srvCols = new csvColumn[4];
srvCols[0] = new csvColumn("FEC_NAME","",new fecNameHndlr(name));
srvCols[1] = new csvColumn("IP_ADDR","",new ipAddrHndlr());
srvCols[2] = new csvColumn("PORT_OFFSET","0",new portHndlr());
srvCols[3] = new csvColumn("TINE_PROTOCOL","6",new protHndlr());
// open it
fecFile = new csv(in);
// read it
fecFile.readFile(srvCols);
// close it
fecFile.close();
}
public int getTineProtocol()
{
return tineProtocol;
}
public void setTineProtocol(int tineProtocol)
{
this.tineProtocol = tineProtocol;
}
public static enum fecImportance
{
NONE, IMPORTANT, ESSENTIAL, CRITICAL
}
public fecImportance getImportance()
{
return TFecEntry.getImportance(fecName);
}
public static synchronized fecImportance getImportance(String fecName)
{
String importance = null;
NAME32[] tgt = new NAME32[1];
tgt[0] = new NAME32(fecName);
NAME32[] imp = new NAME32[1];
TDataType din = new TDataType(tgt);
TDataType dout = new TDataType(imp);
TLink tl = new TLink("/SITE/ENS","IMPORTANCE",dout,din,TAccess.CA_READ);
int rc = tl.execute(500, true);
tl.close();
if (rc == 0)
{
importance = imp[0].getName();
if (importance.compareToIgnoreCase("ALL") == 0)
importance = "NONE";
}
else
{
importance = "NONE";
}
return fecImportance.valueOf(importance);
}
public int setImportance(fecImportance importance)
{
return TFecEntry.setImportance(fecName,importance);
}
public static synchronized int setImportance(String fecName,fecImportance importance)
{
NAME32[] tgt = new NAME32[2];
tgt[0] = new NAME32(fecName);
tgt[1] = new NAME32(importance.name());
TDataType din = new TDataType(tgt);
TLink tl = new TLink("/SITE/ENS","IMPORTANCE",null,din,TAccess.CA_WRITE);
int rc = tl.execute(500, true);
tl.close();
return rc;
}
}