public class RemoteConnection extends ConnectionA
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();
}
}
}
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTE
The default max timeout for reading from a remote connection
|
static int |
DEFAULT_TIME_TO_WAIT_FOR_MORE_DATA_REMOTE
The default max time to wait for more data for a remote connection
|
static int |
DEFAULT_ZEBRA_WEBSERVICES_RMI_PORT
The default Zebra Web services RMI Port (11995).
|
Constructor and Description |
---|
RemoteConnection(String uniqueId)
Initializes a new instance of the
RemoteConnection class using the default Zebra Web services port
(11995).This constructor will use the default timeouts for Connection.read() . |
RemoteConnection(String uniqueId,
int rmiServerPort)
Initializes a new instance of the
RemoteConnection class using the specified
rmiServerPort . |
RemoteConnection(String uniqueId,
int rmiServerPort,
int maxTimeoutForRead,
int timeToWaitForMoreData)
Initializes a new instance of the
RemoteConnection class using the specified
rmiServerPort . |
Modifier and Type | Method and Description |
---|---|
int |
bytesAvailable()
Returns an estimate of the number of bytes that can be read from this connection without blocking.
|
void |
close()
Close has no effect on a remote connection.
|
ConnectionReestablisher |
getConnectionReestablisher(long thresholdTime)
Returns a
ConnectionReestablisher which allows for easy recreation of a connection which may have
been closed. |
int |
getRmiServerPort()
Returns the RMI server port.
|
String |
getSimpleConnectionName()
Return the remote connection unique id as the description.
|
String |
getUniqueId()
Returns the unique ID of the device.
|
boolean |
isConnected()
Returns true if the connection is open.
|
void |
open()
Opens the connection to a device.
|
byte[] |
read()
Reads all the available data from the connection.
|
byte[] |
read(int maxBytesToRead)
Reads
maxBytesToRead of the available data from the connection. |
int |
readChar()
Reads the next byte of data from the connection, similar to a Java InputStream.
|
void |
registerForAlerts(java.util.HashSet<AlertCondition> alertsConditionsToMonitor,
AlertMonitorI monitorCallback)
Register for alerts on the RemoteConnection which are being received by the Zebra Servlet Instance.
|
String |
toString()
See the classes which implement this method for the format of the description string.
|
void |
unregisterForAlerts(java.util.HashSet<AlertCondition> alertsConditionsToMonitor,
AlertMonitorI monitorCallback)
Un-Register an AlertMonitor from a RemoteConnection.
|
void |
write(byte[] data)
Writes
data.length bytes from the specified byte array to this output stream. |
void |
write(byte[] data,
int offset,
int length)
Writes
length bytes from data starting at offset . |
addWriteLogStream, getManufacturer, getMaxDataToWrite, getMaxTimeoutForRead, getTimeToWaitForMoreData, read, read, sendAndWaitForResponse, sendAndWaitForResponse, sendAndWaitForValidResponse, sendAndWaitForValidResponse, setMaxDataToWrite, setMaxTimeoutForRead, setReadTimeout, setTimeToWaitForMoreData, waitForData, write
public static final int DEFAULT_ZEBRA_WEBSERVICES_RMI_PORT
public static final int DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTE
public static final int DEFAULT_TIME_TO_WAIT_FOR_MORE_DATA_REMOTE
public RemoteConnection(String uniqueId)
RemoteConnection
class using the default Zebra Web services port
(11995).Connection.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.RemoteConnection(String, int, int, int)
uniqueId
- the UUID of the device.RemoteConnection(String, int)
to specify a non
default RMI port.
public RemoteConnection(String uniqueId, int rmiServerPort)
RemoteConnection
class using the specified
rmiServerPort
. This constructor will use the default timeouts for Connection.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.RemoteConnection(String, int, int, int)
uniqueId
- the UUID of the device.rmiServerPort
- the port the RMI server is running on.public RemoteConnection(String uniqueId, int rmiServerPort, int maxTimeoutForRead, int timeToWaitForMoreData)
RemoteConnection
class using the specified
rmiServerPort
. This constructor will use the specified timeouts for Connection.read()
. The
timeout is a maximum of maxTimeoutForRead
milliseconds for any data to be received. If no more data
is available after timeToWaitForMoreData
milliseconds the read operation is assumed to be complete.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.public void open() throws ConnectionException
Connection
open
method 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 call Connection.close()
to free up system resources.open
in interface Connection
open
in class ConnectionA
ConnectionException
- if the connection cannot be established.Connection.open()
public void close() throws ConnectionException
close
in interface Connection
close
in class ConnectionA
ConnectionException
- cannot be thrown for a remote connection.Connection.close()
public void write(byte[] data) throws ConnectionException
Connection
data.length
bytes from the specified byte array to this output stream. The connection must be
open before this method is called. If write
is called when a connection is closed, a
ConnectionException
is thrown.write
in interface Connection
write
in class ConnectionA
data
- the data.ConnectionException
- if an I/O error occurs.Connection.write(byte[])
public void write(byte[] data, int offset, int length) throws ConnectionException
Connection
length
bytes from data
starting at offset
. The connection must be
open before this method is called. If write
is called when a connection is closed, a
ConnectionException
is thrown.write
in interface Connection
write
in class ConnectionA
data
- the data.offset
- the start offset in the data
.length
- number of bytes to write.ConnectionException
- if an I/O error occurs.Connection.write(byte[], int, int)
public byte[] read() throws ConnectionException
Connection
byte[] data = printerConnection.read();
read
in interface Connection
read
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.read()
public byte[] read(int maxBytesToRead) throws ConnectionException
ConnectionA
maxBytesToRead
of the available data from the connection. This call is non-blocking.read
in class ConnectionA
maxBytesToRead
- number of bytes to readConnectionException
- if an I/O error occurs.ConnectionA.read(int)
public int readChar() throws ConnectionException
Connection
int singleCharacter = printerConnection.readChar();
readChar
in interface Connection
readChar
in class ConnectionA
ConnectionException
- if an I/O error occurs.ConnectionA.readChar()
public boolean isConnected()
Connection
isConnected
in interface Connection
isConnected
in class ConnectionA
true
if this connection is open.Connection.isConnected()
public int bytesAvailable() throws ConnectionException
Connection
bytesAvailable
in interface Connection
bytesAvailable
in class ConnectionA
ConnectionException
- if an I/O error occurs.Connection.bytesAvailable()
public void registerForAlerts(java.util.HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) throws ConnectionException
alertsConditionsToMonitor
- AlertConditions which will fire events for this specific listener.monitorCallback
- AlertMonitorI which provides the implementation for handling alerts.ConnectionException
public void unregisterForAlerts(java.util.HashSet<AlertCondition> alertsConditionsToMonitor, AlertMonitorI monitorCallback) throws ConnectionException
alertsConditionsToMonitor
- AlertConditions which will fire events for this specific listener.monitorCallback
- AlertMonitorI which provides the implementation for handling alerts.ConnectionException
public String getUniqueId()
public int getRmiServerPort()
public String toString()
Connection
toString
in interface Connection
toString
in class Object
Object.toString()
public String getSimpleConnectionName()
Connection.getSimpleConnectionName()
public ConnectionReestablisher getConnectionReestablisher(long thresholdTime) throws ConnectionException
Connection
ConnectionReestablisher
which allows for easy recreation of a connection which may have
been closed.
getConnectionReestablisher
in interface Connection
getConnectionReestablisher
in class ConnectionA
thresholdTime
- how long the Connection reestablisher will wait before attempting to reconnection to the
printerConnectionException
- if the ConnectionReestablisher could not be created.ConnectionA.getConnectionReestablisher(long thresholdTime)
© 2017 ZIH Corp. All Rights Reserved.