AModule.java :  » HTTP » http-bot » su » msk » dunno » httpbot » prototypes » Java Open Source

Java Open Source » HTTP » http bot 
http bot » su » msk » dunno » httpbot » prototypes » AModule.java
package su.msk.dunno.httpbot.prototypes;

import java.util.LinkedList;

import su.msk.dunno.httpbot.main.Bot;

public abstract class AModule 
{
  protected String name = "";
  
  protected LinkedList<String> immediateList = new LinkedList<String>();
  private LinkedList<Order> todoNow = new LinkedList<Order>();
  protected LinkedList<Order> orders = new LinkedList<Order>();  
    
  protected long lastActionTime = 0;
  protected long actionPeriod = 0;
  protected long min_period = 0; // min and max values of period in seconds between module actions
  protected long max_period = 2000;
  
  public abstract void doAction(); 
  
  protected void checkPublicOrders()
  {
    for(Order o: Bot.getPublicOrders())
    {
      if(name.equalsIgnoreCase(o.getTarget()))
      {
        if(isImmediate(o.getOrder()))todoNow.add(o);
        else orders.add(o);
        Bot.orderAccepted(o);
      }
    }
    for(Order o: todoNow) executeOrder(o);
    todoNow.clear();
    if(orders.size() > 0)
    {
      Order o = orders.getFirst();
      executeOrder(o);  // trying to execute - but without guarantee!
      if(o.wasExecuted)orders.remove(o);  // remove if the order was executed successfully (or it is unknown order)
    }
  }
  
  private boolean isImmediate(String order)
  {
    for(String imm: immediateList)
    {
      if(order.equalsIgnoreCase(imm))return true;
    }
    return false;
  }
  
  protected Order addOrder(String targetModule, String order)
  {
    return Bot.addOrder(name, targetModule, order);
  }
  
  protected Order addOrder(String targetModule, String order, String additionalInfo)
  {
    return Bot.addOrder(name, targetModule, order, additionalInfo);
  }
  
  protected abstract void executeOrder(Order currentOrder);

  public String getName() 
  {
    return name;
  }

  public long getActionPeriod() 
  {
    return actionPeriod;
  }

  public long getLastActionTime() 
  {
    return lastActionTime;
  }
  
  public LinkedList<Order> getOrders()
  {
    return orders;
  }
  
  public void dropActionPeriod()
  {
    actionPeriod = 0;
  }

  public void receiveSystemMessage(String s) 
  {
    
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.