source: proiecte/ptvs/src/vnsim/applications/vitp/peer/Peer.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 3.1 KB
Line 
1/************************************************************************************
2 * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University
3 * All rights reserved.
4 * Refer to LICENSE for terms and conditions of use.
5 ***********************************************************************************/
6package vnsim.applications.vitp.peer;
7
8import vnsim.applications.vitp.routing.*;
9import vnsim.applications.vitp.services.*;
10import vnsim.applications.vitp.ui.*;
11import vnsim.applications.vitp.utils.*;
12
13public class Peer {
14
15        //a peer module, comunicating with a routing module (the layer below),
16        //a user interface (the layer above) and a services module, in order to
17        //properly serve VITP Requests
18
19        public RoutingModule routingModule;
20        private MessageProcessServices servicesModule;
21        private UserInterface userInterfaceModule;
22
23        public void attachModules(RoutingModule r, MessageProcessServices s, UserInterface ui){
24                this.routingModule=r;
25                this.servicesModule=s;
26                this.userInterfaceModule=ui;
27        }
28       
29        public boolean postMessageFromBelow(VITPMessage msg){
30                //method to be called by the module below, in order to send a message
31                //to this peer; returns true if the peer properly deals with the packet,
32                //false otherwise
33
34                //this peer has received a packet; there are several possibilities;
35
36                //it could be a POST request or a reply; in that case, since it was
37                //delivered to me by the routing layer, it is for the local node,
38                //so it has to be delivered to the user interface
39
40                //finally, it could be a GET request, in which case I have to try and satisfy
41                //it, by using the services
42
43                if(msg.isReply()) {
44                        return userInterfaceModule.postMessage(msg);
45                }
46                if(msg.isRequest()) {
47                        try{
48                                VITPRequest req=(VITPRequest)msg;
49                                if(req.isPost()) {
50                                        //return userInterfaceModule.postMessage(msg);
51                                        userInterfaceModule.postMessage(msg);
52                                        return routingModule.postMessageFromAbove(msg);
53                                } else {
54                                        if(req.isGet()){
55                                                //use the services to try and satisfy this request
56                                                VITPMessage mes=servicesModule.satisfy(req);
57                                                return routingModule.postMessageFromAbove(mes);
58                                        } else {
59                                                System.out.println("ERROR! Unknown method:="+req.getMethod());
60                                                return false;
61                                        }
62                                }
63                        } catch (Exception ex){
64                                System.out.println("PEER_EXCEPTION:"+ex.toString());
65                                ex.printStackTrace();
66                                return false;
67                        }
68                }
69                System.out.println("Discarding! Peer.postMessage could not decide on the following message:"+msg);
70                System.out.println("***");
71                return false;
72        }
73
74        public boolean postMessageFromAbove(VITPMessage msg){
75                //method to be called by the module above, in order to send a message
76                //to this peer; returns true if the peer properly deals with the packet,
77                //false otherwise
78       
79                //this peer has received a message from the user interface; it should be
80                //a request
81                if(msg.isRequest()) {
82                        msg.setMsgIdInval(); //invalidate the id; thus, the routing module will
83                                                        //see the invalid id, and provide it a new one
84                        return routingModule.postMessageFromAbove(msg);
85                }
86                return false;
87        }
88}
Note: See TracBrowser for help on using the repository browser.