Skip to content

Commit

Permalink
agente de interface fazendo desconexões
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Jan 4, 2021
1 parent e815d21 commit a4168bd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
7 changes: 7 additions & 0 deletions UserAgentsServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<scope>mas</scope>
<organization>SACIP</organization>
</service>
<service>
<name>registerStudent</name>
<description>faz o login do estudante no sistema</description>
<entity>PedagogicalAgent</entity>
<scope>mas</scope>
<organization>SACIP</organization>
</service>

<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!-- SACIP Tracking Agent -->
Expand Down
Binary file modified board.bkp
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/java/sacip/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void main(String[] args) {
// for (String beanName : beanNames) {
// System.out.println(beanName);
// }
Manager.getInstance().disconnect(true);
//Manager.getInstance().disconnect(true);

} catch (IOException e) {
e.printStackTrace();
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/sacip/sti/agents/InterfaceAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sacip.sti.components.DBConnection;

import sacip.sti.dataentities.Content;
import sacip.sti.dataentities.Student;

Expand All @@ -41,15 +42,13 @@
public class InterfaceAgent extends Agent implements MessageListener{

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);
public int localport;
public int localport = 7102;
public final String serverport = "7100";
public final String serverAddress = "127.0.0.1";
private static Map<String, Student> usuariosConectados;

public InterfaceAgent() {
super();
this.localport = Integer.parseInt(super.recoverMetaInformation().getContainerPort());
this.localport++;
super();
usuariosConectados = new HashMap<>();
}

Expand Down Expand Up @@ -85,10 +84,22 @@ public String criaNovaConta(@RequestBody Student conta) {
}
}

@PostMapping("/logout/{agentPort}")
@ResponseBody
public String deslogar(@PathVariable String agentPort) {

try {
Manager.getInstance().disconnect(agentPort, true);
return "SUCESSO";
} catch (Exception e) {
LOG.error("Falhou criar conta em REST Interface", e);
return "Falhou criar conta: \n"+e.getLocalizedMessage();
}
}

@PostMapping("/login")
@ResponseBody
public String fazLogin(@RequestBody JsonNode credenciais) {

try {
String usuario = credenciais.get("usuario").asText();
String senha = credenciais.get("senha").asText();
Expand Down Expand Up @@ -117,14 +128,17 @@ public String fazLogin(@RequestBody JsonNode credenciais) {
do {
try
{
super.recoverMetaInformation().getOrganizationByName("SACIP"+setLocal).getEntityByName("PedagogicalAgent"+setLocal);
Catalog.getEntityByName(setLocal+"", "SACIP"+setLocal, "PedagogicalAgent"+setLocal);
break;
}
catch (Exception e) {
LOG.error("Entidade não encontrada", e);
Thread.sleep(1000);
}
} while (true);


require("SACIP"+setLocal, "registerStudent"+setLocal).run();

return setLocal+"";

} catch (Exception e) {
Expand Down Expand Up @@ -175,7 +189,7 @@ public void iniciandoAgentesUsuario(String instancia) throws IOException
//Inicializando Agentes do Usuário
String UserAgentsStructureXML = readFile("UserAgentsStructure.xml");
String UserAgentsServicesXML = readFile("UserAgentsServices.xml");
UserAgentsStructureXML = UserAgentsStructureXML.replace("$localport", localport+"").replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("</name>", instancia+"</name>");
UserAgentsStructureXML = UserAgentsStructureXML.replace("$localport", instancia+"").replace("$serverport", serverport).replace("$serverAddress", serverAddress).replace("</name>", instancia+"</name>");
UserAgentsServicesXML = UserAgentsServicesXML.replace("</name>", instancia+"</name>").replace("</entity>", instancia+"</entity>").replace("</organization>", instancia+"</organization>");
AgentServer.initialize(true, true, UserAgentsStructureXML, UserAgentsServicesXML);
}
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/sacip/sti/agents/PedagogicalAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,19 @@

public class PedagogicalAgent extends Agent implements MessageListener {

private final String port;
private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);
private Student student;
List<Content> trilha = new ArrayList<>();

public PedagogicalAgent() {
super();
this.port = super.recoverMetaInformation().getContainerPort();
registrarAluno();
//montarAlunoExemplo();
registrarConteudosDaTrilhaDoAluno();
}

private void registrarAluno()
{
try {
Map<String, Student> usuariosConectados = (Map<String, Student>) Board.getContextAttribute("conectedUsers");
this.student = usuariosConectados.get(this.port);
this.student = usuariosConectados.get(super.getPort());
} catch (Exception e) {
LOG.error("Problema no registro do usuario", e);
}
Expand Down Expand Up @@ -89,7 +84,7 @@ private void registrarConteudosDaTrilhaDoAluno() {

@Override
public void provide(String service, Map in, List out) throws ServiceException {
switch (service.replace(this.port, "")) {
switch (service.replace(super.getPort(), "")) {
case "getAluno":
out.add(getAluno());
break;
Expand All @@ -98,14 +93,31 @@ public void provide(String service, Map in, List out) throws ServiceException {
out.add(suggestContent());
break;

case "registerStudent":
registrarAluno();
//montarAlunoExemplo();
registrarConteudosDaTrilhaDoAluno();
break;

default:
throw new ServiceException("Serviço " + service + " não foi implementado no agente pedagógico.");
}
}

@Override
protected void lifeCycle() throws LifeCycleException, InterruptedException {

// while(alive)
// {
// try {
// if(super.getPort()!=null && this.student == null)
// {

// break;
// }
// } catch (Exception e) {
// //TODO: handle exception
// }
// }
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sacip/sti/agents/RecommenderAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RecommenderAgent extends Agent {
private static final int ANALISAR = 3;
private static final int AVALIAR = 4;
private static final int CRIAR = 5;

@Override
public void provide(String service, Map in, List out) throws ServiceException {
switch (service)
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/sacip/sti/agents/TrackingAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@
public class TrackingAgent extends Agent implements MessageListener {

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);
private final String port;

public TrackingAgent() {
super();
this.port = super.recoverMetaInformation().getContainerPort();
}
private String port;

@Override
public void provide(String service, Map in, List out) throws ServiceException {

JsonNode dados = (JsonNode) in.get("dados");
String nome = dados.get("nome").asText();

switch (service.replace(this.port, ""))
switch (service.replace(super.getPort(), ""))
{
case "storeData":
if (dados.has("cliques")) {
Expand Down

0 comments on commit a4168bd

Please sign in to comment.