2016-05-18 7 views
2

EclipseのCaliforniumを使用して明示的にルートリソースパスのみを宣言するCoAPアプリケーションで作業しています。残りのリソースは、RESTのようにワイルドカード/root/*を使用して処理して解決する必要がありますAPIまたはサーブレット。Eclipse Californium CoAPワイルドカードをURLパスとして

これを達成する方法はありますか?

答えて

3

私はそれをやりました。

ソースコードを数時間掘り下げたところで、やってしまったのはここです。それは動作しますが、それはそれを行うことができる方法を示すためにのみだ

注意、それは私とすぐに、まだ進行中の作業(私は3時間でこれをやった)私はオブザーバーなどのようないくつかのコードを削除したとして...

ですしばらくして、私はカリフォルニアのApiにもっと踏み込んで、これを一般的かつ最適化して、Githubプロジェクトを作成してここにリンクします。

1:

public class ProxyRes { 

    public CoapResource coapRes; 
    public String path; 

    public ProxyRes() { 
    } 

    public CoapResource getCoapRes() { 
     return coapRes; 
    } 

    public void setCoapRes (CoapResource coapRes) { 
     this.coapRes = coapRes; 
    } 

    public String getPath() { 
     return path; 
    } 

    public void setPath (String path) { 
     this.path = path; 
    } 
} 

2モデルクラスを作成します。

public abstract class AbstractResource extends CoapResource { 

    private LinkedList<String> wildcards; 

    protected AbstractResource (String name) { 
     super (name); 
    } 

    protected AbstractResource (String name, boolean visible) { 
     super (name, visible); 
    } 

    public LinkedList<String> getWildcards() { 
     return wildcards; 
    } 

    public void setWildcards (LinkedList<String> wildcards) { 
     this.wildcards = wildcards; 
    } 
} 

3ワイルドカードリストを注入しなければならない抽象CoapResource作成:AbstractResource

public class TemperatureResource extends AbstractResource { 

    public TemperatureResource() { 
     super (ResourceSpecs.House.Sensors.Temperature); 

     getAttributes().setTitle ("Temperature resource !"); 
    } 

    @Override 
    public void handleGET (CoapExchange exchange) { 
     String response = "The temperature"; 
     if (getWildcard() != null) { 
      response += " of the " + getWildcard().get (0) + " on the " + getWildcard().get (1); 
     } 
     response += " is : 25 degree C"; 

     exchange.respond (response); 
    } 
} 
に延びる温度リソースを作成

4:eclipseプロジェクトのルートにresourcesディレクトリを作成します.j私のリソースの息子confのファイル

{ 
    "verb": "get", 
    "endpoint": "/houses/*/rooms/*/sensors/temperature", 

    "class": "com.wild.coap.resources.TemperatureResource" 
} 

5:リソースローダ(資源のスペックの定義をロードし、代わりに、サーバー上のツリーを作成するのでは独立してインスタンス化するクラス)

public class ResourcesLoader { 

    private final static String Path = new File (".").getAbsolutePath() + File.separator + "resources"; 

    private List<ProxyRes> resourcesList; 

    public ResourcesLoader() throws Exception { 
     resourcesList = new ArrayList<ProxyRes>(); 

     File resources = new File (Path); 
     for (String resName : resources.list()) { 
      File resFile = new File (resources, resName); 
      InputStream is = new FileInputStream (resFile); 
      JsonObject o = new JsonObject (is); 

      resourcesArr.add (o); 
      resourcesList.add (buildObject (o)); 
     } 
    } 

    private ProxyRes buildObject (JsonObject o) throws ClassNotFoundException, InstantiationException, IllegalAccessException { 
     ProxyRes r = new ProxyRes(); 
     r.setPath (o.getString ("endpoint")); 

     Class<?> clazz = Class.forName (o.getString ("class")); 
     CoapResource coapRes = (CoapResource)clazz.newInstance(); 
     r.setCoapRes (coapRes); 

     return r; 
    } 

    public List<ProxyRes> getResourcesList() { 
     return resourcesList; 
    } 
} 

を作成します。 6:カスタムMessageDelieverer

public class DynamicMessageDeliverer implements MessageDeliverer { 

    private final List<ProxyRes> resources; 

    public DynamicMessageDeliverer (List<ProxyRes> resources) { 
     this.resources = resources; 
    } 

    public void deliverRequest (final Exchange exchange) { 
     Request request   = exchange.getRequest(); 
     List<String> path  = request.getOptions().getUriPath(); 

     final Resource resource = registerResources (path);  
     if (resource != null) { 
      executeResource (exchange, resource);   
     } else { 
      exchange.sendResponse (new Response (ResponseCode.NOT_FOUND)); 
      throw new RuntimeException ("Did not find resource " + path.toString() + " requested by " + request.getSource()+":"+request.getSourcePort()); 
     } 
    } 

    private void executeResource (final Exchange exchange, final Resource resource) { 
     // Get the executor and let it process the request 
     Executor executor = resource.getExecutor(); 
     if (executor != null) { 
      exchange.setCustomExecutor(); 
      executor.execute (new Runnable() { 

       public void run() { 
        resource.handleRequest (exchange); 
       } 
      }); 
     } else { 
      resource.handleRequest (exchange); 
     } 
    } 

    private Resource registerResources (List<String> list) { 
     LinkedList<String> path   = new LinkedList<String> (list); 
     String flatRequestedEndpoint = Arrays.toString (path.toArray()); 
     LinkedList<String> wildcards = new LinkedList <String>(); 
     ProxyRes retainedResource  = null; 

     for (ProxyRes proxyRes : resources) { 
      String[] res = proxyRes.getPath().replaceFirst ("/", "").split ("/"); 

      int length = res.length; 
      if (length != path.size()) { 
       continue; 
      } 

      String flatResEndpoint = Arrays.toString (res); 
      if (flatResEndpoint.equals (flatRequestedEndpoint)) { 
       retainedResource = proxyRes; 
       break; 
      } 

      boolean match = true; 

      for (int i = 0; i < length; i ++) { 
       String str = res[i]; 
       if (str.equals ("*")) { 
        wildcards.add (path.get (i)); 
        continue; 
       } 

       if (!str.equals (path.get (i))) { 
        match = false; 
        break; 
       } 
      } 

      if (!match) { 
       wildcards.clear(); 
       continue; 
      } 

      retainedResource = proxyRes; 
      break; 
     } 

     if (retainedResource == null) { 
      return null; 
     } 

     ((AbstractResource)retainedResource.getCoapRes()).setWildcard (wildcards); 
     return retainedResource.getCoapRes(); 
    } 

    public void deliverResponse (Exchange exchange, Response response) { 
     if (response == null) throw new NullPointerException(); 
     if (exchange == null) throw new NullPointerException(); 
     if (exchange.getRequest() == null) throw new NullPointerException(); 
     exchange.getRequest().setResponse(response); 
     Request request   = exchange.getRequest(); 
     List<String> path  = request.getOptions().getUriPath(); 
     System.out.println ("Path retrieved : " + Arrays.toString (path.toArray())); 
    } 
} 

7作成:サーバーを作成する

public class WildCoapServer extends CoapServer { 

    private static final int COAP_PORT = NetworkConfig.getStandard ().getInt (NetworkConfig.Keys.COAP_PORT); 

    public WildCoapServer() throws Exception { 

     // add endpoints on all IP addresses 
     addEndpoints(); 

     ResourcesLoader resLoader = new ResourcesLoader(); 
     List<ProxyRes> resources = resLoader.getResourcesList(); 

     setMessageDeliverer (new DynamicMessageDeliverer (resources)); 
    } 

    @Override 
    protected Resource createRoot() { 
     return new WildRootResource(); 
    } 

    // Add individual endpoints listening on default CoAP port on all IPv4 addresses of all network interfaces. 
    private void addEndpoints() { 
     for (InetAddress addr : EndpointManager.getEndpointManager().getNetworkInterfaces()) { 
      // only binds to IPv4 addresses and localhost 
      if (addr instanceof Inet4Address || addr.isLoopbackAddress()) { 
       InetSocketAddress bindToAddress = new InetSocketAddress (addr, COAP_PORT); 
       addEndpoint (new CoapEndpoint (bindToAddress)); 
      } 
     } 
    } 
} 

8:

public class Main { 

    public static void main (String[] args) { 

     try { 
      WildCoapServer server = new WildCoapServer(); 
      server.start(); 
     } catch (Exception e) { 
      throw new RuntimeException (e.getMessage(), e); 
     } 
    } 
} 

9サーバーを起動します。誰かを助け、クライアント

public class Client { 

    public static void main (String[] args) { 

     URI uri = null; 
     try { 
      uri = new URI ("coap://192.168.200.1:5683/houses/house1/rooms/kitchen/sensors/temperature"); 
     } catch (URISyntaxException e) { 
      throw new RuntimeException (e.getMessage(), e); 
     } 

     CoapClient client  = new CoapClient (uri); 

     CoapResponse response = client.get(); 

     if (response != null) { 

      System.out.println (response.getCode()); 
      System.out.println (response.getOptions()); 
      System.out.println (response.getResponseText()); 

      System.out.println ("\nADVANCED\n"); 
      // access advanced API with access to more details through .advanced() 
      System.out.println (Utils.prettyPrint (response)); 

     } else { 
      System.out.println ("No response received."); 
     }  
    } 
} 

・ホープからの温度のリソースを消費します。

関連する問題