Package com.zebra.sdk.remote.comm
Class RemoteConnection
Object
com.zebra.sdk.comm.ConnectionA
com.zebra.sdk.remote.comm.RemoteConnection
- All Implemented Interfaces:
Connection,ConnectionWithWriteLogging
This class provides access to remotely connected devices. When implementing the API on a server which is running a
Zebra servlet instance the user may use this class to interact with remotely connected devices in the same manner
they would any other Connection type.
package test.zebra.sdk.comm.examples;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.remote.comm.RemoteConnection;
public class RemoteConnectionExample {
// This example assumes Link-OS printers are connected via Web Sockets to a server which is running a Zebra servlet
// instance.
public static void main(String[] args) throws Exception {
new RemoteConnectionExample().sendZplOverTcp("myPrinterUUID");
new RemoteConnectionExample().printConfigLabelUsingDnsName("myPrinterUUID");
}
private void sendZplOverTcp(String theUniqueId) throws ConnectionException {
// Instantiate a remote connection to the printer specified by UUID using the default Zebra Web services
// port (11995).
// Note: The printer must be connected to a server which is running a Zebra servlet instance.
Connection connection = new RemoteConnection(theUniqueId);
try {
// Open the connection - physical connection is established here.
connection.open();
// This example prints "This is a ZPL test." near the top of the label.
String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
// Send the data to printer as a byte array.
connection.write(zplData.getBytes());
// Close the connection to release resources.
connection.close();
} catch (ConnectionException e) {
// Handle communications error here.
e.printStackTrace();
} finally {
connection.close();
}
}
private void printConfigLabelUsingDnsName(String theUniqueId) throws ConnectionException {
Connection connection = new RemoteConnection(theUniqueId);
try {
connection.open();
ZebraPrinter p = ZebraPrinterFactory.getInstance(connection);
p.printConfigurationLabel();
connection.close();
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default max timeout for reading from a remote connectionstatic final intThe default max time to wait for more data for a remote connectionstatic final intThe default Zebra Web services RMI Port (11995). -
Constructor Summary
ConstructorsConstructorDescriptionRemoteConnection(String uniqueId) Initializes a new instance of theRemoteConnectionclass using the default Zebra Web services port (11995).
This constructor will use the default timeouts forConnection.read().RemoteConnection(String uniqueId, int rmiServerPort) Initializes a new instance of theRemoteConnectionclass using the specifiedrmiServerPort.RemoteConnection(String uniqueId, int rmiServerPort, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theRemoteConnectionclass using the specifiedrmiServerPort. -
Method Summary
Modifier and TypeMethodDescriptionintReturns an estimate of the number of bytes that can be read from this connection without blocking.voidclose()Close has no effect on a remote connection.getConnectionReestablisher(long thresholdTime) Returns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.intReturns the RMI server port.Return the remote connection unique id as the description.Returns the unique ID of the device.booleanReturns true if the connection is open.voidopen()Opens the connection to a device.byte[]read()Reads all the available data from the connection.byte[]read(int maxBytesToRead) ReadsmaxBytesToReadof the available data from the connection.intreadChar()Reads the next byte of data from the connection, similar to a Java InputStream.voidregisterForAlerts(HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) Register for alerts on the RemoteConnection which are being received by the Zebra Servlet Instance.toString()See the classes which implement this method for the format of the description string.voidunregisterForAlerts(HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) Un-Register an AlertMonitor from a RemoteConnection.voidwrite(byte[] data) Writesdata.lengthbytes from the specified byte array to this output stream.voidwrite(byte[] data, int offset, int length) Writeslengthbytes fromdatastarting atoffset.Methods inherited from class com.zebra.sdk.comm.ConnectionA
addWriteLogStream, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, read, read, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write
-
Field Details
-
DEFAULT_ZEBRA_WEBSERVICES_RMI_PORT
public static final int DEFAULT_ZEBRA_WEBSERVICES_RMI_PORTThe default Zebra Web services RMI Port (11995).- See Also:
-
DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTE
public static final int DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTEThe default max timeout for reading from a remote connection- See Also:
-
DEFAULT_TIME_TO_WAIT_FOR_MORE_DATA_REMOTE
public static final int DEFAULT_TIME_TO_WAIT_FOR_MORE_DATA_REMOTEThe default max time to wait for more data for a remote connection- See Also:
-
-
Constructor Details
-
RemoteConnection
Initializes a new instance of theRemoteConnectionclass using the default Zebra Web services port (11995).
This constructor will use the default timeouts forConnection.read(). The default timeout is a maximum of 5 seconds for any data to be received. If no more data is available after 1 second the read operation is assumed to be complete.
To specify timeouts other than the defaults, use:
RemoteConnection(String, int, int, int)- Parameters:
uniqueId- the UUID of the device.- See Also:
-
RemoteConnection
Initializes a new instance of theRemoteConnectionclass using the specifiedrmiServerPort. This constructor will use the default timeouts forConnection.read(). The default timeout is a maximum of 5 seconds for any data to be received. If no more data is available after 1 second the read operation is assumed to be complete.
To specify timeouts other than the defaults, use:
RemoteConnection(String, int, int, int)- Parameters:
uniqueId- the UUID of the device.rmiServerPort- the port the RMI server is running on.
-
RemoteConnection
public RemoteConnection(String uniqueId, int rmiServerPort, int maxTimeoutForRead, int timeToWaitForMoreData) Initializes a new instance of theRemoteConnectionclass using the specifiedrmiServerPort. This constructor will use the specified timeouts forConnection.read(). The timeout is a maximum ofmaxTimeoutForReadmilliseconds for any data to be received. If no more data is available aftertimeToWaitForMoreDatamilliseconds the read operation is assumed to be complete.- Parameters:
uniqueId- the UUID of the device.rmiServerPort- the port the RMI server is running on.maxTimeoutForRead- the maximum time, in milliseconds, to wait for any data to be received.timeToWaitForMoreData- the maximum time, in milliseconds, to wait in-between reads after the initial read.
-
-
Method Details
-
open
Description copied from interface:ConnectionOpens the connection to a device. If theopenmethod is called when this connection has already been opened, this call is ignored. When a handle to the connection is no longer needed, you must callConnection.close()to free up system resources.- Specified by:
openin interfaceConnection- Overrides:
openin classConnectionA- Throws:
ConnectionException- if the connection cannot be established.- See Also:
-
close
Close has no effect on a remote connection. The connection is maintained by the printer.- Specified by:
closein interfaceConnection- Overrides:
closein classConnectionA- Throws:
ConnectionException- cannot be thrown for a remote connection.- See Also:
-
write
Description copied from interface:ConnectionWritesdata.lengthbytes from the specified byte array to this output stream. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Overrides:
writein classConnectionA- Parameters:
data- the data.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
write
Description copied from interface:ConnectionWriteslengthbytes fromdatastarting atoffset. The connection must be open before this method is called. Ifwriteis called when a connection is closed, aConnectionExceptionis thrown.- Specified by:
writein interfaceConnection- Overrides:
writein classConnectionA- Parameters:
data- the data.offset- the start offset in thedata.length- number of bytes to write.- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
read
Description copied from interface:ConnectionReads all the available data from the connection. This call is non-blocking.byte[] data = printerConnection.read();
- Specified by:
readin interfaceConnection- Overrides:
readin classConnectionA- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
read
Description copied from class:ConnectionAReadsmaxBytesToReadof the available data from the connection. This call is non-blocking.- Overrides:
readin classConnectionA- Parameters:
maxBytesToRead- number of bytes to read- Returns:
- the bytes read from the connection.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
readChar
Description copied from interface:ConnectionReads the next byte of data from the connection, similar to a Java InputStream. The value byte is returned as an int in the range of 0 to 255. If no byte is available on the connection the value -1 is returned.int singleCharacter = printerConnection.readChar();
- Specified by:
readCharin interfaceConnection- Overrides:
readCharin classConnectionA- Returns:
- the next byte from the connection
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
isConnected
public boolean isConnected()Description copied from interface:ConnectionReturns true if the connection is open.- Specified by:
isConnectedin interfaceConnection- Overrides:
isConnectedin classConnectionA- Returns:
trueif this connection is open.- See Also:
-
bytesAvailable
Description copied from interface:ConnectionReturns an estimate of the number of bytes that can be read from this connection without blocking.- Specified by:
bytesAvailablein interfaceConnection- Overrides:
bytesAvailablein classConnectionA- Returns:
- estimated number of bytes available.
- Throws:
ConnectionException- if an I/O error occurs.- See Also:
-
registerForAlerts
public void registerForAlerts(HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) throws ConnectionException Register for alerts on the RemoteConnection which are being received by the Zebra Servlet Instance.- Parameters:
alertsConditionsToMonitor- AlertConditions which will fire events for this specific listener.monitorCallback- AlertMonitorI which provides the implementation for handling alerts.- Throws:
ConnectionException
-
unregisterForAlerts
public void unregisterForAlerts(HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) throws ConnectionException Un-Register an AlertMonitor from a RemoteConnection.- Parameters:
alertsConditionsToMonitor- AlertConditions which will fire events for this specific listener.monitorCallback- AlertMonitorI which provides the implementation for handling alerts.- Throws:
ConnectionException
-
getUniqueId
Returns the unique ID of the device.- Returns:
- the unique ID of the device.
-
getRmiServerPort
public int getRmiServerPort()Returns the RMI server port.- Returns:
- the rmiServerPort
-
toString
Description copied from interface:ConnectionSee the classes which implement this method for the format of the description string.- Specified by:
toStringin interfaceConnection- Overrides:
toStringin classObject- Returns:
- the connection description string.
- See Also:
-
getSimpleConnectionName
Return the remote connection unique id as the description.- Returns:
- a human-readable description of the connection.
- See Also:
-
getConnectionReestablisher
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException Description copied from interface:ConnectionReturns aConnectionReestablisherwhich allows for easy recreation of a connection which may have been closed.- This call should be made while there is still an active connection to the printer (prior to issuing a command which will make the printer non-responsive).
- Specified by:
getConnectionReestablisherin interfaceConnection- Overrides:
getConnectionReestablisherin classConnectionA- Parameters:
thresholdTime- how long the Connection reestablisher will wait before attempting to reconnection to the printer- Returns:
- ConnectionReestablisher
- Throws:
ConnectionException- if the ConnectionReestablisher could not be created.- See Also:
-