Package com.zaranux.os.client.daemons

Source Code of com.zaranux.os.client.daemons.notificationd

package com.zaranux.os.client.daemons;

import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.java.io.BufferedReader;
import com.zaranux.client.java.io.FileInputStream;
import com.zaranux.client.java.io.InputStreamReader;
import java.util.Vector;

import com.zaranux.os.client.core.Daemon;
import com.zaranux.os.client.notifications.NotificationRecord;
import com.zaranux.os.client.util.Log;

public class notificationd extends Daemon<NotificationRecord> {

  private static notificationd daemon = null;
  public static notificationd getDaemon()
  {
    if(daemon == null)
    {
      daemon = new notificationd();
    }
    return daemon;
  }
 
  private static int INTERVAL = 30000; // ms
  private final static String NOTIFICATION_FILE = "/.zaranux/.notifications";
  private BufferedReader bufferedReader;
  private Vector<NotificationRecord> notifications = new  Vector<NotificationRecord>();

  private notificationd()
  {
    super(INTERVAL );
    FileInputStream fis = new FileInputStream(NOTIFICATION_FILE);
    InputStreamReader isr = new InputStreamReader(fis);
    bufferedReader = new BufferedReader(isr);
  }
 
  @Override
  protected void execute() {
    readNotificationRecords();
  }

  // read all the notification records recursively and put them in notifications list
  private void readNotificationRecords()
  {
      bufferedReader.readLine(new AsyncCallback<String>()
        {
          public void onSuccess(String line)
          {
            NotificationRecord notification = null;
            if(line != null)
            {
              //Notification record:  sender,time,status,type,message
              String[] record = line.split(",",5);
              if(record == null || record.length < 5)
              {
                Log.error("Wrong notification format (ignored)! " + line );
              }else
              {
                String sender   =  record[0];
                String time   =  record[1];
                String status   =  record[2];
                String type   =  record[3];
                String message   =  record[4];
               
                notification = new NotificationRecord(sender,time,status,type,message);
                notifications.add(notification);

              }
              // recurse
              readNotificationRecords();
            }else
            {
              // check later again ;
              timer.schedule(INTERVAL);
            }
            // send the read notification to registered handlers
            // null signals that we have reached the end of file ..
            notificationd.this.dispatch(notification);
          }
          public void onFailure(Throwable t)
          {
            Log.error("Error reading message file" + t);
          }
        });
     
}
TOP

Related Classes of com.zaranux.os.client.daemons.notificationd

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.