Skip to content

Commit

Permalink
Changes to allow a refresh rate while probing the mind for the REST S…
Browse files Browse the repository at this point in the history
…erver. Also, MemoryGroups and CodeletGroups were included in the returned JSON
  • Loading branch information
rgudwin committed May 21, 2022
1 parent bbf6b53 commit d543164
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 21 deletions.
40 changes: 33 additions & 7 deletions src/main/java/br/unicamp/cst/io/rest/CodeletJson.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

import br.unicamp.cst.core.entities.Codelet;

import java.util.ArrayList;
import java.util.List;

public class CodeletJson {
public double activation;
public long timestamp;
public String name;
public List<MemoryJson> broadcast = new ArrayList<MemoryJson>();
public List<MemoryJson> inputs = new ArrayList<MemoryJson>();
public List<MemoryJson> outputs = new ArrayList<MemoryJson>();
private double activation;
private long timestamp;
private String name;
private String group;
private List<MemoryJson> broadcast = new ArrayList<MemoryJson>();
private List<MemoryJson> inputs = new ArrayList<MemoryJson>();
private List<MemoryJson> outputs = new ArrayList<MemoryJson>();

public CodeletJson(Codelet cod) {
this.activation = cod.getActivation();
Expand All @@ -27,4 +37,20 @@ public CodeletJson(Codelet cod) {
this.outputs.add(new MemoryJson(cod.getOutputs().get(i)));
}
}

public CodeletJson(Codelet cod, String group) {
this.activation = cod.getActivation();
this.timestamp = System.currentTimeMillis();
this.name = cod.getName();
this.group = group;
for (int i = 0; i < cod.getBroadcast().size(); i++) {
this.broadcast.add(new MemoryJson(cod.getBroadcast().get(i)));
}
for (int i = 0; i < cod.getInputs().size(); i++) {
this.inputs.add(new MemoryJson(cod.getInputs().get(i)));
}
for (int i = 0; i < cod.getOutputs().size(); i++) {
this.outputs.add(new MemoryJson(cod.getOutputs().get(i)));
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/br/unicamp/cst/io/rest/MemoryJson.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

import br.unicamp.cst.core.entities.Memory;
Expand All @@ -11,6 +21,7 @@ public class MemoryJson {
public volatile Double evaluation;
public volatile Object I;
public String name;
public String group;
public ArrayList<MemoryJson> memories = new ArrayList<MemoryJson>();

public MemoryJson(Memory memo) {
Expand All @@ -27,4 +38,21 @@ public MemoryJson(Memory memo) {
}

}

public MemoryJson(Memory memo, String group) {
timestamp = memo.getTimestamp();
evaluation = memo.getEvaluation();
I = memo.getI();
name = memo.getName();
this.group = group;
if (memo instanceof MemoryContainer) {
MemoryContainer memoAux = (MemoryContainer) memo;
List<Memory> memoList = memoAux.getAllMemories();
for (int i = 0; i < memoList.size(); i++) {
this.memories.add(new MemoryJson(memoList.get(i)));
}
}

}
}

48 changes: 43 additions & 5 deletions src/main/java/br/unicamp/cst/io/rest/MindJson.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.entities.Mind;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MindJson {
public List<MemoryJson> memories = new ArrayList<MemoryJson>();
public List<CodeletJson> codelets = new ArrayList<CodeletJson>();

public MindJson(List<Memory> memories, List<Codelet> cods) {
for (int i = 0; i < memories.size(); i++) {
this.memories.add(new MemoryJson(memories.get(i)));
public MindJson(Mind m) {
if (m.getMemoryGroupsNumber() == 0) {
List<Memory> mems = m.getRawMemory().getAllMemoryObjects();

for (int i = 0; i < mems.size(); i++) {
this.memories.add(new MemoryJson(mems.get(i)));
}
}
else {
ConcurrentHashMap<String, ArrayList> mems = m.getMemoryGroups();

for (String key : mems.keySet()) {
ArrayList<Memory> memoList = mems.get(key);
for (int i = 0; i < memoList.size(); i++) {
this.memories.add(new MemoryJson(memoList.get(i), key));
}
}
}
for (int i = 0; i < cods.size(); i++) {
this.codelets.add(new CodeletJson(cods.get(i)));
if (m.getCodeletGroupsNumber() == 0) {
List<Codelet> cods = m.getCodeRack().getAllCodelets();
for (int i = 0; i < cods.size(); i++) {
this.codelets.add(new CodeletJson(cods.get(i)));
}
}
else {
ConcurrentHashMap<String, java.util.ArrayList> cods = m.getCodeletGroups();
for (String key : cods.keySet()) {
ArrayList<Codelet> codList = cods.get(key);
for (int i = 0; i < codList.size(); i++) {
this.codelets.add(new CodeletJson(codList.get(i), key));
}
}
}
}
}
42 changes: 35 additions & 7 deletions src/main/java/br/unicamp/cst/io/rest/RESTServer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

import br.unicamp.cst.core.entities.Memory;
Expand All @@ -26,6 +31,10 @@
*/
public class RESTServer {

long refresh = 0; // A refresh of 0 means that every call will generate a new probe in mind
long lastaccess = 0;
String lastmessage = "";

/**
*
* @param m the mind to observe
Expand Down Expand Up @@ -53,6 +62,19 @@ public RESTServer(Mind m, int port, boolean pretty) {
* @param origin a pattern for users allowed to access the server - use "*" to allow everyone
*/
public RESTServer(Mind m, int port, boolean pretty, String origin) {
this(m,port,pretty,origin,0L);
}

/**
*
* @param m the mind to observe
* @param port the port to install the REST server
* @param pretty set this to true to generate pretty printing JSON in the REST server
* @param origin a pattern for users allowed to access the server - use "*" to allow everyone
* @param nrefresh the refresh period in milliseconds
*/
public RESTServer(Mind m, int port, boolean pretty, String origin, long nrefresh) {
refresh = nrefresh;
Express app = new Express();
Gson gson;
if (pretty)
Expand All @@ -65,8 +87,14 @@ public RESTServer(Mind m, int port, boolean pretty, String origin) {
corsOptions.setOrigin(origin);
app.use(Middleware.cors(corsOptions));
app.get("/", (req, res) -> {
MindJson myJson = new MindJson(m.getRawMemory().getAllMemoryObjects(),m.getCodeRack().getAllCodelets());
res.send(gson.toJson(myJson));
long currentaccess = System.currentTimeMillis();
long diff = currentaccess - lastaccess;
if (diff > refresh) {
MindJson myJson = new MindJson(m);
lastmessage = gson.toJson(myJson);
lastaccess = currentaccess;
}
res.send(lastmessage);
});
app.listen(port);
}
Expand Down
Loading

0 comments on commit d543164

Please sign in to comment.