Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.60 KB | None | 0 0
  1. /******************************************************************************
  2.  * @title: CoapTreeBuilder
  3.  *  Used to build the tree of resources to reach a resource with "path-like"
  4.  *  name (e.g. "dev/mfg")
  5.  *
  6.  * @authors:
  7.  *  - Francesco Paolo Culcasi   <fpculcasi@gmail.com>
  8.  *  - Alessandro Martinelli     <a.martinelli1990@gmail.com>
  9.  *  - Nicola Messina            <nicola.messina93@gmail.com>
  10.  *
  11.  * @for: Advanced topics in Network Architectures and Wireless Systems
  12.  *  UNIPI (2016/2017)
  13.  *
  14.  *****************************************************************************/
  15.  
  16. package org.eclipse.californium.proxy;
  17.  
  18. import org.eclipse.californium.core.CoapResource;
  19. import org.eclipse.californium.core.coap.CoAP;
  20. import org.eclipse.californium.core.server.resources.CoapExchange;
  21. import org.eclipse.californium.core.server.resources.Resource;
  22.  
  23. public class CoapTreeBuilder<T extends ActiveCoapResource> {
  24.  
  25.     protected T root;
  26.  
  27.     // defaultVisibility is the visibility applied to a deleted/de-registered
  28.     // (by life-time expiration) resource
  29.     private VisibilityPolicy defaultVisibility;
  30.  
  31.     public CoapTreeBuilder(T root, VisibilityPolicy visibility) {
  32.         this.root = root;
  33.         this.defaultVisibility = visibility;
  34.     }
  35.  
  36.     public VisibilityPolicy getVisibility() {
  37.         return defaultVisibility;
  38.     }
  39.  
  40.     protected class InfoPath {
  41.         // Path that will be traversed
  42.         String remainingPath;
  43.         // resourceName is going to store, iteration after iteration,
  44.         // the part of the path taken into account in that single iteration
  45.         String current;
  46.  
  47.         public InfoPath(String Path) {
  48.             remainingPath = Path;
  49.         }
  50.  
  51.         public String getCurrent() {
  52.             return current;
  53.         }
  54.  
  55.         public boolean pathParsingFinished() {
  56.             return (remainingPath == null) ? true : false;
  57.         }
  58.  
  59.         public void removeFirst() {
  60.             // removes the first '/', it will be add by CoapResource
  61.             // setParent(),
  62.             // see reference [1]
  63.             remainingPath = remainingPath.substring(1);
  64.             // is resourceName a simple name or a path?
  65.             int separatorIndex = remainingPath.indexOf('/');
  66.  
  67.             if (separatorIndex > 0) {
  68.                 // resourceName contains a path. We only need the name
  69.                 current = remainingPath.substring(0, separatorIndex);
  70.                 // remaining_path takes the remaining part of the path
  71.                 remainingPath = remainingPath.substring(separatorIndex);
  72.             } else {
  73.                 // remainingPath contains only the last part of the path now,
  74.                 // i.e. it's only a simple name now.
  75.                 current = remainingPath;
  76.                 remainingPath = null;
  77.             }
  78.         }
  79.     }
  80.  
  81.     private boolean parametersAreValid(String path) {
  82.         // Method that check the correctness of the parameter. In
  83.         // case of problem, null must be returned
  84.         if (path.charAt(0) != '/') {
  85.             // TODO: idem se ci sono due '/' di seguito.
  86.             // bisogna vedere cosa accadrebbe
  87.             return false;
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     /**
  93.      * Builds a subtree of resources from the given path, basing on the given
  94.      * VisibilityPolicy, and adds it as child of the given root. The last part
  95.      * of the path is not added instead, and it is left to the user to create a
  96.      * last resource, using the name (i.e. the last name contained in the path)
  97.      * returned by subtreeBuilder, and to adds it as child of the resource
  98.      * returned by subtreeBuilder, i.e. the resource representing the
  99.      * penultimate part of the path.
  100.      *
  101.      * @param path
  102.      *            The path containing the name of the resources to be created or
  103.      *            traversed.
  104.      * @param root
  105.      *            The resource starting from which the subtree will be built. If
  106.      *            you would like the subtree to start from the root of the
  107.      *            CoapResver, use
  108.      *            org.eclipse.californium.core.CoapServer.getRoot() as root.
  109.      * @param vPolicy
  110.      *            The visibility policy. Possible values are:
  111.      *            VisibilityPolicy.ALL_VISIBLE or
  112.      *            VisibilityPolicy.ALL_INVISIBLE. If a CoapResource has to be
  113.      *            created, it will be created with the visibility specified by
  114.      *            the policy. TODO: modify return description
  115.      * @return a class containing the resource representing the penultimate part
  116.      *         of the path, i.e. the father of the last resource, that the user
  117.      *         is expected to add, and the name of such last resource, extracted
  118.      *         from the path.
  119.      */
  120.     public synchronized boolean add(T newResource, String path, VisibilityPolicy vPolicy) {
  121.         if (parametersAreValid(path) == false) {
  122.             return false;
  123.         }
  124.         // In each iteration, currentFather store the father of the resource
  125.         // it is going to be created or traversed in that iteration.
  126.         // Initially it coincides with the root of the subtree
  127.         Resource currentFather = root;
  128.         // Class used to memorize remaining path and name of
  129.         // the resource that is currently being traversed or created.
  130.         // It also consider if the algorithm is considering the last part
  131.         // of the path
  132.         InfoPath iPath = new InfoPath(path);
  133.         while (true) {
  134.             iPath.removeFirst();
  135.             if (iPath.pathParsingFinished()) {
  136.                 // The user, during newResource creation, should have
  137.                 // set all its fields, like visibility, so the only
  138.                 // fields still to be set is name
  139.                 newResource.setName(iPath.current);
  140.                 // In order to avoid breaking the subtree, if a resource
  141.                 // with the same name of the resource is being created,
  142.                 // its children have to be moved to the new resource
  143.                 Resource toDelete = (currentFather.getChild(iPath.current));
  144.                 if (toDelete != null) {
  145.                     for (Resource child : toDelete.getChildren()) {
  146.                         newResource.add(child);
  147.                     }
  148.                     currentFather.delete(toDelete);
  149.                 }
  150.                 currentFather.add(newResource);
  151.                 return true;
  152.             } else {
  153.                 // Does a resource with name resourceName already exists as
  154.                 // child of the Resource we are considering?
  155.                 Resource child = currentFather.getChild(iPath.getCurrent());
  156.  
  157.                 if (child != null) {
  158.                     if (!(child instanceof CoapResource)) {
  159.                         System.err.println("Resource " + child.getName() + " is not a CoapResource: its visibility"
  160.                                 + " will not be affected");
  161.                     } else {
  162.                         // The resource already exists. We may need to
  163.                         // change his visibility according to the policy
  164.                         handleExistingResource((CoapResource) child, vPolicy);
  165.                     }
  166.                     currentFather = child;
  167.                 } else {
  168.                     // The considered resource has to be created
  169.                     currentFather = handleResourceCreation(iPath.getCurrent(), currentFather, vPolicy);
  170.                 }
  171.             }
  172.         }
  173.     }
  174.  
  175.     /* If not specified, visibility is set to the default value for the tree */
  176.     public boolean add(T newResource, String path) {
  177.         return add(newResource, path, defaultVisibility);
  178.     }
  179.  
  180.     protected void handleExistingResource(CoapResource child, VisibilityPolicy vPolicy) {
  181.         System.out.println("traversing intermediate resource" + " " + child.getName() + ", visibility "
  182.                 + child.isVisible() + " and son of " + child.getParent().getName());
  183.         // The intermediate resource was already there
  184.         switch (vPolicy) {
  185.         case ALL_VISIBLE:
  186.             if (child.isVisible() == false) {
  187.                 child.setVisible(true);
  188.             }
  189.             break;
  190.         case ALL_INVISIBLE:
  191.             // nothing has to be done
  192.             break;
  193.         }
  194.     }
  195.  
  196.     protected T handleResourceCreation(String resourceName, Resource father, VisibilityPolicy vPolicy) {
  197.         // newResource is used to store the newly created resource
  198.         T newResource = null;
  199.         // Creation of an intermediate resource
  200.         switch (vPolicy) {
  201.         case ALL_VISIBLE:
  202.             newResource = (T) new ActiveCoapResource(resourceName, false, true);
  203.             break;
  204.         case ALL_INVISIBLE:
  205.             newResource = (T) new ActiveCoapResource(resourceName, false, false) {
  206.                 @Override
  207.                 public void handleGET(CoapExchange exchange) {
  208.                     exchange.respond(CoAP.ResponseCode.NOT_FOUND);
  209.                 }
  210.             };
  211.             break;
  212.         }
  213.         father.add(newResource);
  214.         System.out.println("Created intermediate resource" + " " + resourceName + ", visibility "
  215.                 + newResource.isVisible() + " and son of " + father.getName());
  216.         return newResource;
  217.     }
  218.  
  219.     public synchronized void remove(T child) {
  220.         if (child == null) {
  221.             return;
  222.         }
  223.         if (child.equals(root)) {
  224.             return;
  225.         }
  226.  
  227.         T parent = (T) child.getParent();
  228.  
  229.         if (!child.isActive()) {
  230.             // The resource is an internal resource
  231.             if (child.getChildren().isEmpty()) {
  232.                 /*
  233.                  * If the resource is an internal resource and it has no child,
  234.                  * it has to be removed
  235.                  */
  236.                 parent.delete(child);
  237.                 if (parent.isActive() == false) {
  238.                     // the parent is an internal resource
  239.                     remove(parent);
  240.                 }
  241.  
  242.             } else {
  243.                 /*
  244.                  * If the resource is an internal resource and it has some
  245.                  * children, it must not be removed
  246.                  */
  247.                 return;
  248.             }
  249.         } else {
  250.             // The resource is an active resource
  251.             if (child.getChildren().isEmpty()) {
  252.                 /*
  253.                  * The resource is an active resource with no children, thus we
  254.                  * can delete it and, iff the father is an internal resource
  255.                  */
  256.                 parent.delete(child);
  257.                 if (parent.isActive() == false) {
  258.                     // the parent is an internal resource
  259.                     remove(parent);
  260.                 }
  261.             } else {
  262.                 /*
  263.                  * The resource is an active resource with children, thus before
  264.                  * deliting it we have to assign his children to a new resource.
  265.                  * Then, we have to remove this resource from his parent and
  266.                  * replace it with the newly created resource.
  267.                  */
  268.                 boolean visibility = (defaultVisibility == VisibilityPolicy.ALL_VISIBLE) ? true : false;
  269.  
  270.                 T newInternalResource = (T) new ActiveCoapResource(child.getName(), false, visibility);
  271.  
  272.                 for (Resource son : child.getChildren()) {
  273.                     newInternalResource.add(son);
  274.                 }
  275.  
  276.                 parent.delete(child);
  277.                 parent.add(newInternalResource);
  278.  
  279.             }
  280.         }
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement