Task in Roku

Posted By : Prashant Grover | 12-Apr-2017

Task Node

 

A task is like a thread in roku, as the name suggest task so these are mainly used to implement any background task , if you are developing roku scene graph application then all the network realated task and accessing roRegistrationSection we need a task node. The Task node class prvides a mechanism to specify a function to be executed in a different thread, and which run's asynchronously with respect to both the scene rendering thread and the main application thread. 

With the help of task node we can fetch the data from server and create a ContentNode node to configure a Scene Graph node.

The basic 3 general idea of creating the task node listed below :

1. If there are many asynchronous operations to be performed in an application then for each operation a separate task node should be created, if any user input data data is required in task node then it can be achieved by defining interface in task node along with an observer of the output interface field data

Note : when the output data is returned from task node then the object of this node should not be used again.

2. A Task node observes its input interface fields using the port form of the ifSGNodeField observeField() method, and it returns the output on each time field gets changed. 

As the task node is used for accomplishment of asynchronous task, so there is no provision provided by roku for avoiding locks and mutexes, so while developing an application its must to take care of various situations like race, deadlock and other asynchronous thread errors, so below are some ways of using task node .

1. Objects should not be mixed - do not use the same object in two different thready simultaneously, please create separate objects for this.

2. In every task ndoe there is a function called init, which acts like a constructer which is maily used for initialization so if you want to initialize4 any object then you can do so in init() function, if there is any input value used in taks node whose value will be keep on changing so do not use that value object in init() function.

Note : this function is called when task node object is created.

3. Roku provides a mechanism to run n numbers of asynchronous threads on a single task node object by setting the function name field of the task node.

Following are the fields supported by TASK NODE in ROKU :

1. functionName - it is of string type and accepts arguments in string,  this field is used to mention the name of the function in the Task node component to be executed when the state field changes to RUN.

 

2. control - this field is optional and it is also of string type and accepts arguments in string,  this field is used to requests a change in the run state of the spawned task.

 

3. state  - it is of string type and accepts arguments in string, this field is used to inquires about the run state of the spawned task, the values are "STOP", "RUN","DONE".

 

Example :

<?xml version="1.0" encoding="utf-8" ?>
 
<component name = "rowListContent" extends = "Task" >
 
  <interface>
    <field id = "url" type = "string" />
    <field id = "content" type = "node" />
  </interface>
 
  <script type = "text/brightscript" >


    <![CDATA[
 
    sub init()


      m.top.functionName = "getData"


    end sub
 
    sub getData()


      rowListContent = createObject("RoSGNode","ContentNode")
 
      rowListXML = createObject("roXMLElement")
 
      readData = createObject("roUrlTransfer")


      readData.setUrl(m.top.url)


      rowListXML.parse(readData.GetToString())
 
      if rowListXML.getName()="RowGrid"


         for each poster in rowListXML.GetNamedElements("rowPoster")

               rowContent = rowListXML.createChild("ContentNode")

                rowContent.setFields(rowPoster.getAttributes())

          end for

 

      end if
 
      m.top.content = rowListContent


    end sub
 
    ]]>


  </script>

 
</component>

 

 

 

THANKS

About Author

Author Image
Prashant Grover

Prashant is a bright Java developer, his area of expertise are Core Java, Android , Spring . Grails etc. Apart from that he spends his time playing volleyball.

Request for Proposal

Name is required

Comment is required

Sending message..