Showing posts with label Cloud-X. Show all posts
Showing posts with label Cloud-X. Show all posts

Wednesday, October 19, 2011

Setting up a Cloud Print Printer using the Cloud-X API

The Cloud-X Java API makes it easy to set up a Google Cloud Print aware printer. In the latest API version Cloud Print push notifications are supported.


The below sample code shows the registration of a new printer (if not already present) and how to listen to push notifications.
For the sake of simplicity this is straight forward code, so there might be cleaner ways to do is ;)

Here is the sample code with comments:

/*
  * Process print job
  */
 public static void processJobs(CloudPrintConnection gcp, Printer printer) {
  try {
   List<Job> jobs = gcp.fetch(printer);
   if (jobs != null) {
    for (Job job : jobs) {
     System.out.println("Processing job " + job.getId() + " ("
       + job.getTitle() + ")");
     gcp.deletJob(job);
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  try {
   String printProxy = "testprinter-" + NetworkUtil.getMacAddress();

   /*
    * Our printer
    */
   final Printer printer = new Printer();
   printer.setProxy(printProxy);
   printer.setName("Test Printer");
   printer.setStatus("Online");

   final CloudPrintConnection gcp = new CloudPrintConnection();
   gcp.connect(username, password, "cloudprint",
     "Cloud Print Test Client", null);

   /*
    * Register printer if not already present
    */
   List<Printer> printers = gcp.list(printProxy);
   if (printers.size() == 0) {
    System.out.println("Registering Printer with Proxy "
      + printProxy);
    gcp.register(printer, null);
   } else {
    printer.setId(printers.get(0).getId());
   }

   /*
    * Refetch all printer info
    */
   gcp.printer(printer);

   System.out.println("Printer Name: " + printer.getName());
   System.out.println("Printer ID: " + printer.getId());
   System.out.println("---------------------------------------------");

   processJobs(gcp, printer);

   /*
    * Wait for push notifications
    */
   PushReceiver pr = new PushReceiver("Cloud-X API - Push Receiver");
   pr.addListener(new PushListener() {

    @Override
    public void onReceive(String printerId) {
     if (printerId.equals(printer.getId())) {
      processJobs(gcp, printer);
     }
    }

    @Override
    public void onConnect() {
      System.out.println("Connected to Google Talk!");
    }

    @Override
    public void onDisconnect() {
      // Do nothing
    }
   });

   pr.connectPlain(username, password);

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

Thursday, October 13, 2011

Cloud-X - Sharing Files through the Cloud

We just released the first alpha version of Cloud-X.
Cloud-X uses Google Cloud Print to share files between different users and devices. Along with the PC and Android app the Java API to access Google Cloud Print is also released.

This is a very early version, so there might be bugs.
The Android app and an online and stand alone PC version can be found at the following site:
http://cloud-x.fun2code.de

Comments welcome!

Update

The website is no longer online.
API with included sources can be downloaded from http://fun2code.de/downloads/GoogleCloudPrint.jar.