Skip to content

Resource Example

Jim Schaad edited this page Jun 30, 2017 · 3 revisions

Resource Example

When writing a server, the core part of the project is to write the resource handlers. Most of the rest of the code is provided by this library. This example will go through all of the core parts of creating a resource object and adding it to a server.

Create the resource

A resource needs to derive from the IResource interface to be used in the system. However, deriving it from Resource class means that a lot of processing will be taken care of for you.

    class TimeResource : Resource {

When a resource is created, the name of the resource is required by the default Resource constructor. The resource is visible, so it provides some attributes to publish as part of the Discovery information. Specifically, we are going to add

  • A title for display by the client
  • A resource type for searchability
  • A content type - this is the content type(s) that are expected to be returned.

The resource is marked as being an observable resource.

The resource is marked as requiring security. The text returned is derived from the work in the IETF ACE working group that is currently under development.

    public TimeResource(String name)
        : base(name)
    {
        Attributes.Title = "GET the current time";
        Attributes.AddResourceType("CurrentTime");
        Attributes.AddContentType(0);

        Observable = true;

        RequireSecurity = true;
        RequireSecurityErrorText = "{ \"as_server\":\"coaps://example.com:5689/token\" }";

        _timer = new Timer(Timed, null, 0, 2000*30*30);
    }

Handle Methods

If deriving from the Resource class, then any method which is not overridden will automatically return a method not allowed (4.05).

Simple Get Method

This method is called when a Method.Get is in a response.

Since the resource was marked as being observable in the constructor, the CoAP stack in the server will automatically deal with the fact that a request is or is not tagged as an observe request and create the observe relationship and place it on the resource. ////QUESTION/// how do I cancel this.

For simple responses, using the Respond method on the exchange item is the easiest way to deal with things.

    protected override void DoGet(CoapExchange exchange)
    {
        exchange.MaxAge = 30*30;  // Number of seconds it is live
        exchange.Respond(StatusCode.Content, _now.ToString());
    }

Complicated Get Method

The following represents a more complicated version of a DoGet function that creates a Response on its own rather than use the Exchange class to create one.

  • The function looks at the accept options looking for one that it can create and returns and error if it cannot find one.

  • It will return the content either encoded in CBOR or as text.

  • It will look for a 'format' query parameter which contains a format string for the time and use it if it exists.

      protected override void DoGet(CoapExchange exchange)
      {
          Request request = exchange.Request;
    
          IEnumerable<Option> options =  request.GetOptions(OptionType.Accept);
          int useAccept = MediaType.Undefined;
          bool acceptFound = false;
    
          foreach (var acccept in options) {
              switch (acccept.IntValue) {
              case MediaType.TextPlain:
              case MediaType.ApplicationCbor:
                  useAccept = acccept.IntValue;
                  break;
    
              default:
                  acceptFound = true;
                  break;
                  
              }
    
              if (useAccept != MediaType.Undefined) break;
          }
    
          if (useAccept == MediaType.Undefined) {
              if (acceptFound) {
                  exchange.Respond(StatusCode.UnsupportedMediaType);
                  return;
              }
              useAccept = MediaType.TextPlain;
          }
    
          Response response = Response.CreateResponse(request, StatusCode.Content);
    
          switch (useAccept) {
              case MediaType.TextPlain:
                  string x = request.GetParameter("format");
                  if (String.IsNullOrEmpty(x)) {
                      response.PayloadString = _now.ToShortTimeString();
    
                  }
                  else {
                      response.PayloadString = _now.ToString(x);
                  }
                  request.ContentType = useAccept;
                  break;
    
              case MediaType.ApplicationCbor:
                  CBORObject obj = CBORObject.FromObject(_now);
                  request.Payload = obj.EncodeToBytes();
                  request.ContentType = useAccept;
                  break;
          }
    
          response.MaxAge = 30*30;  // Number of seconds it is live
    
          exchange.Respond(response);
      }
    

Clone this wiki locally