OpenTCS打造移動機器人交通管制系統(八)

       上一篇文章記錄了下如何在OpenTCS框架下拓展數據庫(點此查看上一篇),本篇來繼續深入的探討下如何拓展Kernel。OpenTCS框架主要的作用是提供一個交通管制的框架,但是通常來說,一個完整的調度系統(這裏指的是廣義的調度系統),是不僅僅只有交通管制的,通常還會有業務層、邏輯層等相關模塊一起和OpenTCS構成一個實際可用的系統。一般來說實際項目的架構至少應該是下面的樣子:

 

那麼很顯然,要想系統更緊湊,OpenTCS-Kernel需要拓展,其實OpenTCS在設計之初就預留了拓展的接口(爲OpenTCS的設計者點個贊)。這個接口的定義如下:

//以下代碼片段摘取來自OpenTCS.
public interface KernelExtension
    extends Lifecycle {
  
  /**
   * Returns a name/brief human-readable description for this extension.
   *
   * @return The name/brief description.
   * @deprecated Unused. Will be removed.
   */
  @Deprecated
  @ScheduledApiChange(when = "5.0", details = "Will be removed.")
  default String getName() {
    return getClass().getName();
  };
}

很顯然,這個接口又是拓展的Lifecycle接口,Liftcycle接口定義的接口如下:

//以下代碼片段摘取來自OpenTCS.
public interface Lifecycle {

  /**
   * (Re-)Initializes this component before it is being used.
   */
  void initialize();
  
  /**
   * Checks whether this component is initialized.
   *
   * @return <code>true</code> if, and only if, this component is initialized.
   */
  boolean isInitialized();

  /**
   * Terminates the instance and frees resources.
   */
  void terminate();
}

因此,我們要使用拓展接口,那就應該提供一個實現體,實現KernelExtention接口,如下:

//以下代碼來自白色冰激凌的博客https://blog.csdn.net/xlh145
public class UserKernelExtension implements KernelExtension {
  private volatile boolean enabled;
  @Override
  public boolean isInitialized() {
    return enabled;
  }

  @Override
  public void initialize() {
    if (enabled) {
      return;
    }
	//TODO 此處做你想做的任何事情,OpenTCS會自動加載你的代碼
  }

  @Override
  public void terminate() {
    if (!enabled) {
      return;
    }
    enabled = false;
	//TODO 此處應該做善後工作	
  }
}

實際上,OpenTCS的TCP接口,HTTP接口全部是基於本文所述的拓展接口實現的,搞清楚了這些,實現自己的拓展也就清楚明瞭了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章