use of org.netxms.base.NXCPMessage in project netxms by netxms.
the class NXCSession method getImage.
/**
* Get an image from the library
*
* @param guid UUID of the image
* @return The image
* @throws IOException if socket I/O error occurs
* @throws NXCException if NetXMS server returns an error or operation was timed out
*/
public LibraryImage getImage(UUID guid) throws IOException, NXCException {
final NXCPMessage msg = newMessage(NXCPCodes.CMD_GET_IMAGE);
msg.setField(NXCPCodes.VID_GUID, guid);
sendMessage(msg);
final NXCPMessage response = waitForRCC(msg.getMessageId());
final ReceivedFile imageFile = waitForFile(msg.getMessageId(), 600000);
if (imageFile.isFailed())
throw new NXCException(RCC.IO_ERROR);
return new LibraryImage(response, imageFile.getFile());
}
use of org.netxms.base.NXCPMessage in project netxms by netxms.
the class NXCSession method deployPackage.
/**
* Deploy agent packages onto given nodes
*
* @param packageId package ID
* @param nodeList list of nodes
* @param listener deployment progress listener (may be null)
* @throws IOException if socket or file I/O error occurs
* @throws NXCException if NetXMS server returns an error or operation was timed out
*/
public void deployPackage(long packageId, Long[] nodeList, PackageDeploymentListener listener) throws IOException, NXCException {
final NXCPMessage msg = newMessage(NXCPCodes.CMD_DEPLOY_PACKAGE);
msg.setFieldInt32(NXCPCodes.VID_PACKAGE_ID, (int) packageId);
msg.setFieldInt32(NXCPCodes.VID_NUM_OBJECTS, nodeList.length);
msg.setField(NXCPCodes.VID_OBJECT_LIST, nodeList);
sendMessage(msg);
waitForRCC(msg.getMessageId());
if (listener != null)
listener.deploymentStarted();
while (true) {
final NXCPMessage response = waitForMessage(NXCPCodes.CMD_INSTALLER_INFO, msg.getMessageId(), 600000);
final int status = response.getFieldAsInt32(NXCPCodes.VID_DEPLOYMENT_STATUS);
if (status == PackageDeploymentListener.FINISHED)
break;
if (listener != null) {
// irrelevant message texts for statuses other then FAILED
if (status == PackageDeploymentListener.FAILED) {
listener.statusUpdate(response.getFieldAsInt64(NXCPCodes.VID_OBJECT_ID), status, response.getFieldAsString(NXCPCodes.VID_ERROR_MESSAGE));
} else {
listener.statusUpdate(response.getFieldAsInt64(NXCPCodes.VID_OBJECT_ID), status, "");
}
}
}
if (listener != null)
listener.deploymentComplete();
}
use of org.netxms.base.NXCPMessage in project netxms by netxms.
the class NXCSession method connect.
/**
* Connect to NetMS server. Establish connection with the server and set up encryption if required.
* Versions of protocol components given in *componentVersions* will be validated. Login must be
* performed before using session after successful connect.
*
* @param componentVersions The versions of the components
* @throws IOException if socket I/O error occurs
* @throws UnknownHostException if the host is unknown
* @throws NXCException if NetXMS server returns an error or operation was timed out
* @throws IllegalStateException if the state is illegal
*/
public void connect(int[] componentVersions) throws IOException, UnknownHostException, NXCException, IllegalStateException {
if (connected)
throw new IllegalStateException("Session already connected");
if (disconnected)
throw new IllegalStateException("Session already disconnected and cannot be reused");
encryptionContext = null;
Logger.info("NXCSession.connect", "Connecting to " + connAddress + ":" + connPort);
try {
socket = new Socket();
socket.connect(new InetSocketAddress(connAddress, connPort), connectTimeout);
msgWaitQueue = new NXCPMsgWaitQueue(commandTimeout);
recvThread = new ReceiverThread();
housekeeperThread = new HousekeeperThread();
new NotificationProcessor();
// get server information
Logger.debug("NXCSession.connect", "connection established, retrieving server info");
NXCPMessage request = newMessage(NXCPCodes.CMD_GET_SERVER_INFO);
sendMessage(request);
NXCPMessage response = waitForMessage(NXCPCodes.CMD_REQUEST_COMPLETED, request.getMessageId());
protocolVersion = new ProtocolVersion(response);
if (!ignoreProtocolVersion) {
if (!protocolVersion.isCorrectVersion(ProtocolVersion.INDEX_BASE) || ((componentVersions != null) && !validateProtocolVersions(componentVersions))) {
Logger.warning("NXCSession.connect", "connection failed (" + protocolVersion.toString() + ")");
throw new NXCException(RCC.BAD_PROTOCOL, protocolVersion.toString());
}
} else {
Logger.debug("NXCSession.connect", "protocol version ignored");
}
serverVersion = response.getFieldAsString(NXCPCodes.VID_SERVER_VERSION);
serverId = response.getFieldAsInt64(NXCPCodes.VID_SERVER_ID);
serverTimeZone = response.getFieldAsString(NXCPCodes.VID_TIMEZONE);
serverTime = response.getFieldAsInt64(NXCPCodes.VID_TIMESTAMP) * 1000;
serverTimeRecvTime = System.currentTimeMillis();
serverChallenge = response.getFieldAsBinary(NXCPCodes.VID_CHALLENGE);
tileServerURL = response.getFieldAsString(NXCPCodes.VID_TILE_SERVER_URL);
if (tileServerURL != null) {
if (!tileServerURL.endsWith("/"))
tileServerURL = tileServerURL.concat("/");
} else {
tileServerURL = "http://tile.openstreetmap.org/";
}
dateFormat = response.getFieldAsString(NXCPCodes.VID_DATE_FORMAT);
if ((dateFormat == null) || (dateFormat.length() == 0))
dateFormat = "dd.MM.yyyy";
timeFormat = response.getFieldAsString(NXCPCodes.VID_TIME_FORMAT);
if ((timeFormat == null) || (timeFormat.length() == 0))
timeFormat = "HH:mm:ss";
shortTimeFormat = response.getFieldAsString(NXCPCodes.VID_SHORT_TIME_FORMAT);
if ((shortTimeFormat == null) || (shortTimeFormat.length() == 0))
shortTimeFormat = "HH:mm";
int count = response.getFieldAsInt32(NXCPCodes.VID_NUM_COMPONENTS);
long fieldId = NXCPCodes.VID_COMPONENT_LIST_BASE;
for (int i = 0; i < count; i++) serverComponents.add(response.getFieldAsString(fieldId++));
// Setup encryption if required
if (connUseEncryption) {
request = newMessage(NXCPCodes.CMD_REQUEST_ENCRYPTION);
request.setFieldInt16(NXCPCodes.VID_USE_X509_KEY_FORMAT, 1);
sendMessage(request);
waitForRCC(request.getMessageId());
}
Logger.debug("NXCSession.connect", "Connected to server version " + serverVersion);
connected = true;
} finally {
if (!connected)
disconnect(SessionNotification.USER_DISCONNECT);
}
}
use of org.netxms.base.NXCPMessage in project netxms by netxms.
the class NXCSession method getCollectedDataInternal.
/**
* Get collected DCI data from server. Please note that you should specify
* either row count limit or time from/to limit.
*
* @param nodeId Node ID
* @param dciId DCI ID
* @param instance instance value (for table DCI only)
* @param dataColumn name of column to retrieve data from (for table DCI only)
* @param from Start of time range or null for no limit
* @param to End of time range or null for no limit
* @param maxRows Maximum number of rows to retrieve or 0 for no limit
* @param includeRawValues if true raw DCI values will be included into set
* @return DCI data set
* @throws IOException if socket I/O error occurs
* @throws NXCException if NetXMS server returns an error or operation was timed out
*/
private DciData getCollectedDataInternal(long nodeId, long dciId, String instance, String dataColumn, Date from, Date to, int maxRows, boolean includeRawValues) throws IOException, NXCException {
NXCPMessage msg;
if (// table DCI
instance != null) {
msg = newMessage(NXCPCodes.CMD_GET_TABLE_DCI_DATA);
msg.setField(NXCPCodes.VID_INSTANCE, instance);
msg.setField(NXCPCodes.VID_DATA_COLUMN, dataColumn);
} else {
msg = newMessage(NXCPCodes.CMD_GET_DCI_DATA);
}
msg.setFieldInt32(NXCPCodes.VID_OBJECT_ID, (int) nodeId);
msg.setFieldInt32(NXCPCodes.VID_DCI_ID, (int) dciId);
msg.setField(NXCPCodes.VID_INCLUDE_RAW_VALUES, includeRawValues);
DciData data = new DciData(nodeId, dciId);
int rowsReceived, rowsRemaining = maxRows;
int timeFrom = (from != null) ? (int) (from.getTime() / 1000) : 0;
int timeTo = (to != null) ? (int) (to.getTime() / 1000) : 0;
do {
msg.setMessageId(requestId.getAndIncrement());
msg.setFieldInt32(NXCPCodes.VID_MAX_ROWS, maxRows);
msg.setFieldInt32(NXCPCodes.VID_TIME_FROM, timeFrom);
msg.setFieldInt32(NXCPCodes.VID_TIME_TO, timeTo);
sendMessage(msg);
waitForRCC(msg.getMessageId());
NXCPMessage response = waitForMessage(NXCPCodes.CMD_DCI_DATA, msg.getMessageId());
if (!response.isBinaryMessage())
throw new NXCException(RCC.INTERNAL_ERROR);
rowsReceived = parseDataRows(response.getBinaryData(), data);
if (((rowsRemaining == 0) || (rowsRemaining > MAX_DCI_DATA_ROWS)) && (rowsReceived == MAX_DCI_DATA_ROWS)) {
// adjust boundaries for next request
if (rowsRemaining > 0)
rowsRemaining -= rowsReceived;
// retrieve additional data, we should update timeTo limit
if (to != null) {
DciDataRow row = data.getLastValue();
if (row != null) {
// There should be only one value per second, so we set
// last row's timestamp - 1 second as new boundary
timeTo = (int) (row.getTimestamp().getTime() / 1000) - 1;
}
}
}
} while (rowsReceived == MAX_DCI_DATA_ROWS);
return data;
}
use of org.netxms.base.NXCPMessage in project netxms by netxms.
the class NXCSession method findNodesByHostname.
/**
* Find all nodes that contain the primary hostname
*
* @param zoneId zone ID
* @param hostname Hostname to find
* @return List of nodes found
* @throws IOException if socket or file I/O error occurs
* @throws NXCException if NetXMS server returns an error or operation was timed out
*/
public List<AbstractNode> findNodesByHostname(int zoneId, String hostname) throws IOException, NXCException {
final NXCPMessage msg = newMessage(NXCPCodes.CMD_FIND_HOSTNAME_LOCATION);
msg.setFieldInt32(NXCPCodes.VID_ZONE_UIN, zoneId);
msg.setField(NXCPCodes.VID_HOSTNAME, hostname);
sendMessage(msg);
final NXCPMessage response = waitForRCC(msg.getMessageId());
int count = response.getFieldAsInt32(NXCPCodes.VID_NUM_ELEMENTS);
long base = NXCPCodes.VID_ELEMENT_LIST_BASE;
List<AbstractNode> nodes = new ArrayList<AbstractNode>();
for (int i = 0; i < count; i++) {
nodes.add((AbstractNode) findObjectById(response.getFieldAsInt32(base++)));
}
return nodes;
}
Aggregations