-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from CST-Group/rest-server
Rest server
- Loading branch information
Showing
10 changed files
with
1,208 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*********************************************************************************************** | ||
* 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 { | ||
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(); | ||
this.timestamp = System.currentTimeMillis(); | ||
this.name = cod.getName(); | ||
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))); | ||
} | ||
} | ||
|
||
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))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*********************************************************************************************** | ||
* 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; | ||
import br.unicamp.cst.core.entities.MemoryContainer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MemoryJson { | ||
public Long timestamp; | ||
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) { | ||
timestamp = memo.getTimestamp(); | ||
evaluation = memo.getEvaluation(); | ||
I = memo.getI(); | ||
name = memo.getName(); | ||
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))); | ||
} | ||
} | ||
|
||
} | ||
|
||
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))); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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(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)); | ||
} | ||
} | ||
} | ||
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)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*********************************************************************************************** | ||
* 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; | ||
import br.unicamp.cst.core.entities.MemoryContainer; | ||
import br.unicamp.cst.core.entities.MemoryObject; | ||
import br.unicamp.cst.core.entities.Mind; | ||
import br.unicamp.cst.support.InterfaceAdapter; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import express.Express; | ||
import express.middleware.CorsOptions; | ||
import express.middleware.Middleware; | ||
|
||
/** | ||
* This is the main class for using REST to monitor inner activities from CST | ||
* It is used to provide a REST server to monitor whatever is happening within a CST mind | ||
* Depending on the constructor, the user can set the mind, the port to be used, if | ||
* the JSON describing the inner details of the mind should be rendered with pretty printing | ||
* and a way to limit the access of external users to the server | ||
* @author rgudwin | ||
*/ | ||
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 | ||
* @param port the port to install the REST server | ||
*/ | ||
public RESTServer(Mind m, int port) { | ||
this(m,port,false); | ||
} | ||
|
||
/** | ||
* | ||
* @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 | ||
*/ | ||
public RESTServer(Mind m, int port, boolean pretty) { | ||
this(m,port,pretty,"*"); | ||
} | ||
|
||
/** | ||
* | ||
* @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 | ||
*/ | ||
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) | ||
gson = new GsonBuilder().registerTypeAdapter(Memory.class, new InterfaceAdapter<MemoryObject>()) | ||
.registerTypeAdapter(Memory.class, new InterfaceAdapter<MemoryContainer>()) | ||
.setPrettyPrinting().create(); | ||
else | ||
gson = new Gson(); | ||
CorsOptions corsOptions = new CorsOptions(); | ||
corsOptions.setOrigin(origin); | ||
app.use(Middleware.cors(corsOptions)); | ||
app.get("/", (req, res) -> { | ||
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); | ||
} | ||
|
||
} |
Oops, something went wrong.