Home /Modules/advantage/SLAdvBridge.sleela

SLAdvBridge.sleela

lib/advantage/modules/SLAdvBridge.sleela · 150 line(s) · sleela

<< lib/advantage/modules/SLAdvBridge.sleela
   SLeeLa Standard Library Definition.
   Definition: The Advantage bridge: the middle proponent of communication, both ways. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Two bounded queues: toward the modules and toward the user defined program. >
class SLAdvBridge {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs. Values are faithful
     to the subject real origin where publicly known; unknown values use none. >>
  String metaVersion = "1.0.0";
  int metaId = 39521;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-13";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "Advantage™";
  String metaCorporation = "none";
  < metaOrigin: National | nation | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-13";
  < metaScienceRating: maturity on a 1..10 scale; courseware grade module host >
  int metaScienceRating = 6;
  < metaNationalSchedule: a real citable instrument or none >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  < Messages addressed to program flow up; every other address flows down to >
  < the medium, which routes it to a module. Both queues are bounded (1024). >
  < A refused post returns -1 and is counted: the bridge fails closed. >
  SLAdvMessage[] toMod;
  SLAdvMessage[] toProg;
  int mHead = 0;
  int mCount = 0;
  int pHead = 0;
  int pCount = 0;
  int cap = 1024;
  int nextId = 1;
  int posted = 0;
  int dropped = 0;
  int up = 0;
  int down = 0;
  boolean isOpen = false;
  boolean built = false;

  void build() {
    if (built) { return; }
    toMod = new SLAdvMessage[1024];
    toProg = new SLAdvMessage[1024];
    built = true;
  }

  void openBridge() { build(); isOpen = true; }
  void shut() { isOpen = false; }
  boolean opened() { return isOpen; }
  int getPosted() { return posted; }
  int getDropped() { return dropped; }
  int getUp() { return up; }
  int getDown() { return down; }
  int pendingModules() { return mCount; }
  int pendingProgram() { return pCount; }

  << Post a message. Returns its id, or -1 when refused: closed bridge, null
     message, empty address or topic, or a full queue. >>
  int post(SLAdvMessage m) {
    if (isOpen == false || m == null) { dropped = dropped + 1; return -1; }
    if (m.to == "" || m.topic == "") { dropped = dropped + 1; return -1; }
    if (m.id == 0) { m.id = nextId; nextId = nextId + 1; }
    if (m.to == "program") {
      if (pCount >= cap) { dropped = dropped + 1; return -1; }
      int idx = (pHead + pCount) % cap;
      toProg[idx] = m;
      pCount = pCount + 1;
      up = up + 1;
    } else {
      if (mCount >= cap) { dropped = dropped + 1; return -1; }
      int idx2 = (mHead + mCount) % cap;
      toMod[idx2] = m;
      mCount = mCount + 1;
      down = down + 1;
    }
    posted = posted + 1;
    return m.id;
  }

  SLAdvMessage nextForModules() {
    if (mCount == 0) { return null; }
    SLAdvMessage m = toMod[mHead];
    toMod[mHead] = null;
    mHead = (mHead + 1) % cap;
    mCount = mCount - 1;
    return m;
  }

  SLAdvMessage nextForProgram() {
    if (pCount == 0) { return null; }
    SLAdvMessage m = toProg[pHead];
    toProg[pHead] = null;
    pHead = (pHead + 1) % cap;
    pCount = pCount - 1;
    return m;
  }

  << Take the reply to request id out of the program queue, leaving every other
     message in order. Returns null when it is not there. >>
  SLAdvMessage takeReply(int id) {
    int i = 0;
    int found = -1;
    while (i < pCount) {
      SLAdvMessage m = toProg[(pHead + i) % cap];
      if (m.inReplyTo == id && found < 0) { found = i; }
      i = i + 1;
    }
    if (found < 0) { return null; }
    SLAdvMessage hit = toProg[(pHead + found) % cap];
    int j = found;
    while (j < pCount - 1) {
      toProg[(pHead + j) % cap] = toProg[(pHead + j + 1) % cap];
      j = j + 1;
    }
    toProg[(pHead + pCount - 1) % cap] = null;
    pCount = pCount - 1;
    return hit;
  }
}