/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.applications.vitp.peer; import vnsim.applications.vitp.routing.*; import vnsim.applications.vitp.services.*; import vnsim.applications.vitp.ui.*; import vnsim.applications.vitp.utils.*; public class Peer { //a peer module, comunicating with a routing module (the layer below), //a user interface (the layer above) and a services module, in order to //properly serve VITP Requests public RoutingModule routingModule; private MessageProcessServices servicesModule; private UserInterface userInterfaceModule; public void attachModules(RoutingModule r, MessageProcessServices s, UserInterface ui){ this.routingModule=r; this.servicesModule=s; this.userInterfaceModule=ui; } public boolean postMessageFromBelow(VITPMessage msg){ //method to be called by the module below, in order to send a message //to this peer; returns true if the peer properly deals with the packet, //false otherwise //this peer has received a packet; there are several possibilities; //it could be a POST request or a reply; in that case, since it was //delivered to me by the routing layer, it is for the local node, //so it has to be delivered to the user interface //finally, it could be a GET request, in which case I have to try and satisfy //it, by using the services if(msg.isReply()) { return userInterfaceModule.postMessage(msg); } if(msg.isRequest()) { try{ VITPRequest req=(VITPRequest)msg; if(req.isPost()) { //return userInterfaceModule.postMessage(msg); userInterfaceModule.postMessage(msg); return routingModule.postMessageFromAbove(msg); } else { if(req.isGet()){ //use the services to try and satisfy this request VITPMessage mes=servicesModule.satisfy(req); return routingModule.postMessageFromAbove(mes); } else { System.out.println("ERROR! Unknown method:="+req.getMethod()); return false; } } } catch (Exception ex){ System.out.println("PEER_EXCEPTION:"+ex.toString()); ex.printStackTrace(); return false; } } System.out.println("Discarding! Peer.postMessage could not decide on the following message:"+msg); System.out.println("***"); return false; } public boolean postMessageFromAbove(VITPMessage msg){ //method to be called by the module above, in order to send a message //to this peer; returns true if the peer properly deals with the packet, //false otherwise //this peer has received a message from the user interface; it should be //a request if(msg.isRequest()) { msg.setMsgIdInval(); //invalidate the id; thus, the routing module will //see the invalid id, and provide it a new one return routingModule.postMessageFromAbove(msg); } return false; } }