Class RemoteConnection

Object
com.zebra.sdk.comm.ConnectionA
com.zebra.sdk.remote.comm.RemoteConnection
All Implemented Interfaces:
Connection, ConnectionWithWriteLogging

public class RemoteConnection extends ConnectionA
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 Details

    • DEFAULT_ZEBRA_WEBSERVICES_RMI_PORT

      public static final int DEFAULT_ZEBRA_WEBSERVICES_RMI_PORT
      The default Zebra Web services RMI Port (11995).
      See Also:
    • DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTE

      public static final int DEFAULT_MAX_TIMEOUT_FOR_READ_REMOTE
      The 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_REMOTE
      The default max time to wait for more data for a remote connection
      See Also:
  • Constructor Details

    • RemoteConnection

      public 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(). 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

      public RemoteConnection(String uniqueId, int rmiServerPort)
      Initializes a new instance of the 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.
      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 the 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.
      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

      public void open() throws ConnectionException
      Description copied from interface: Connection
      Opens the connection to a device. If the 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.
      Specified by:
      open in interface Connection
      Overrides:
      open in class ConnectionA
      Throws:
      ConnectionException - if the connection cannot be established.
      See Also:
    • close

      public void close() throws ConnectionException
      Close has no effect on a remote connection. The connection is maintained by the printer.
      Specified by:
      close in interface Connection
      Overrides:
      close in class ConnectionA
      Throws:
      ConnectionException - cannot be thrown for a remote connection.
      See Also:
    • write

      public void write(byte[] data) throws ConnectionException
      Description copied from interface: Connection
      Writes 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.
      Specified by:
      write in interface Connection
      Overrides:
      write in class ConnectionA
      Parameters:
      data - the data.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • write

      public void write(byte[] data, int offset, int length) throws ConnectionException
      Description copied from interface: Connection
      Writes 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.
      Specified by:
      write in interface Connection
      Overrides:
      write in class ConnectionA
      Parameters:
      data - the data.
      offset - the start offset in the data.
      length - number of bytes to write.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • read

      public byte[] read() throws ConnectionException
      Description copied from interface: Connection
      Reads all the available data from the connection. This call is non-blocking.
       byte[] data = printerConnection.read();
       
      Specified by:
      read in interface Connection
      Overrides:
      read in class ConnectionA
      Returns:
      the bytes read from the connection.
      Throws:
      ConnectionException - if an I/O error occurs.
      See Also:
    • read

      public byte[] read(int maxBytesToRead) throws ConnectionException
      Description copied from class: ConnectionA
      Reads maxBytesToRead of the available data from the connection. This call is non-blocking.
      Overrides:
      read in class ConnectionA
      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

      public int readChar() throws ConnectionException
      Description copied from interface: Connection
      Reads 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:
      readChar in interface Connection
      Overrides:
      readChar in class ConnectionA
      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: Connection
      Returns true if the connection is open.
      Specified by:
      isConnected in interface Connection
      Overrides:
      isConnected in class ConnectionA
      Returns:
      true if this connection is open.
      See Also:
    • bytesAvailable

      public int bytesAvailable() throws ConnectionException
      Description copied from interface: Connection
      Returns an estimate of the number of bytes that can be read from this connection without blocking.
      Specified by:
      bytesAvailable in interface Connection
      Overrides:
      bytesAvailable in class ConnectionA
      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

      public String 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

      public String toString()
      Description copied from interface: Connection
      See the classes which implement this method for the format of the description string.
      Specified by:
      toString in interface Connection
      Overrides:
      toString in class Object
      Returns:
      the connection description string.
      See Also:
    • getSimpleConnectionName

      public String 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: Connection
      Returns a ConnectionReestablisher which 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:
      getConnectionReestablisher in interface Connection
      Overrides:
      getConnectionReestablisher in class ConnectionA
      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: