Parameter passing to the TinyOS Timer. - tinyos

I am completely new to the tinyos and related API. I have defined a timer and starting it as below.
uses interface Timer<TMilli> as DelayTimer;
call DelayTimer.startOneShot(TIMER_PERIOD_MILLI);
Also defined a timer expiry handler as below,
event void DelayTimer.fired() {
//...
}
My requirement is that to pass an argument to this timer so that same can be used in the the timer handler function.
Can some one provide how it can be done?

There is no way to pass any parameter to the Timer directly. You need to save it in your component's state before calling startOneShot:
implementation {
uint16_t parameter;
// ...
void function(uint16_t value) {
parameter = value;
call DelayTimer.startOneShot(TIMER_PERIOD_MILLI);
}
event void DelayTimer.fired() {
// use variable parameter
}
}
However, if your case is simple and you need only to distinct between various "reasons" of calling Timer, you may use different Timer instances for different purposes:
uses interface Timer<TMilli> as LogTimer;
uses interface Timer<TMilli> as SendTimer;
And then, in an implementation:
void someFunction() {
call LogTimer.startPeriodic(5000);
// ...
}
void anotherFunction() {
call SendTimer.startOneShot(SEND_DELAY);
// ...
}
event void LogTimer.fired() {
// perform logging
}
event void SendTimer.fired() {
// send a packet
}

Related

Qt library design with signals

I am trying to design a Qt library which gives the output back to the client code using signals, but I can't quite get my head around it, I think something is wrong.
Say the library exposes a single class A as follows:
class A {
public:
void request(int data);
signals:
void response(int res);
}
So the client code instantiates an A, connects its signal to a slot, and calls request(). I initially chose to use a signal to return the output because A takes some time to elaborate the response, so I want that call to be non-blocking.
My problem is: what if I need to call request() in many different places in my code, and do different things after I receive my response? I think the question is fundamentally on the correct use of signal/slot design of Qt.
To give a concrete example, and hopefully explain myself further, I temporarily solved the issue setting a boolean before the request() to "remind" me what path of execution to take later:
void doingThis() {
doingThis = true;
request(data);
}
...
void doingThat() {
doingThis = false;
request(data);
}
...
public mySlot(int res) {
if (dointThis) {
...
} else {
...
}
}
This is hideous. What am I doing wrong?
I agree with Ludo who commented on your question.
If you pass some random number (identifier) into the request, then A can emit that same random number back with the response signal. Even if you have a bunch of slots connected to that signal, you would make them only handle the signal if the identifier was familiar to them.
class A {
public:
void request(int data, int id);
signals:
void response(int res, int id);
}
void doingThis() {
request(data, 0xaaaa);
}
...
void doingThat() {
request(data, 0xbbbb);
}
...
public mySlotA(int res, int id) {
if (id == 0xaaaa) {
...
}
}
public mySlotB(int res, int id) {
if (id == 0xbbbb) {
...
}
}
In the case above, the id is hard-coded to represent where the call came from. However, you could also randomly generate the ID. If you did that, then you'd need to save the randomly generated ID. The advantage is that you could send several different requests from doingThis() and be able to understand which response belongs to each request when they arrive back in your slot.

QTimer timeout and QMutex interaction

Let's say we have some basic timer and a slot which is invoked periodically.
SomeObject::SomeObject()
{
QTimer *timer = new QTimer;
connect(timer , SIGNAL(timeout()), this, SLOT(updateState()));
}
void SomeObject::updateState()
{
// some operations leading to update of updatedValue
}
I also have function from same object which forces updates and returns some value.
bool SomeObject::getLatestState()
{
updateState();
return updatedValue;
}
This function may be directly called from different threads. This brings question of thread safety to mind. Simple mutex lock in getLatestState will not help as in some rare cases getLatestState is called from another thread that starts updateState. And at the same time timer's timeout may occur. Can you help me handle this situation properly?
QMutexLocker can be used in such situations
this my exampl
static QMutex mainMutex;
.....
MainController* MainController::s_instance = NULL;
.....
MainController* MainController::getInstance()
{
if(!s_instance){
QMutexLocker lock(&mainMutex);
if(!s_instance){
s_instance = new MainController;
}
}
return s_instance;
}
getInstance() function directly called from different threads.

Java FX - How to terminate a ScheduledService

I wrote a little code to download some files from internet..if user click on cancel button this must be stopped..i use the cancel() method for do it..but it didn't work.
ScheduledService<Object> service = new ScheduledService<Object>() {
protected Task<Object> createTask() {
return new Task<Object>() {
protected Object call() {
if (checkinternet()) {
downloadFiles();
}
return null;
}
};
}
};
service.start();
In buttons action event handler i called cancel method for stop service..
but it wasn't successful..
service.cancel();
How do i do that...
There is no automatic way to cancel a task or service.
From the documentation (https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm):
Cancelling the Task.
There is no reliable way in Java to stop a thread in process. However, the task must stop processing whenever cancel is called on the task. The task is supposed to check periodically during its work whether it was cancelled by using the isCancelled method within the body of the call method.
The example referenced in the above block looks like this:
Task<Integer> task = new Task<Integer>() {
#Override protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 100000; iterations++) {
if (isCancelled()) {
break;
}
System.out.println("Iteration " + iterations);
}
return iterations;
}
};
So you will have to implement the cancel logic by yourself, unfortunately.

synchronizing slots in QThread -?

I have 2 slot handlers in QThread- derived class: one is timer handler and another is just asynchronous callback handler. Both have to modify the same data.
struct somedata {
int max;
int min;
double avg;
}
...
class MyThread: QThread {
private:
somedata m_data;
private Q_SLOTS:
void asyncCallback(int a, int b) {
m_data.max += a;
m_data.min += b;
}
void timer() {
m_data.avg =(m_data.a + m_data.b)/2;
}
}
Should the access to m_data be serialized in some fashion, although both method are in the same thread?
Thanks,
As long as you can guarantee that your data is only ever being accessed or modified by a single thread at any time, then you don't need to work about synchronizing access to that data via thread-safety constructs.
One way to verify this is to check the return value of QThread's static currentThread() function when your functions are called.
If both functions are called by same thread then you dont need to worry about the data getting changes when the other call is in the second slot.If you are not sure then the best option is to use a mutex in both the slots so that only one process or changes the value of m_data.

Call function after remote call in Flex

I have a function in Flex which has three function in it.
public function update():void
{
A(); \\Dispatches a event with Remote Call
B(); \\Dispatches another event with Remote Call
C();
}
I wan't to call C() after both A() and B() have returned from their call and set particular variables. How can i do that?
If you find yourself doing this often (esp. with more than 2 remote calls), you might want to write your own custom class that handles AsyncTokens from remote calls with a final result() function that is invoked when all remote calls end in success.
If not, since ActionScript is single-threaded, you can just use a local variable to track whether both calls have succeeded.
private var resultFromRemoteCallA:Object;
private var resultFromRemoteCallB:Object;
private function handleRemoteCallA(event:ResultEvent):void {
resultFromRemoteCallA = event.result;
C();
}
private function handleRemoteCallB(event:ResultEvent):void {
resultFromRemoteCallB = event.result;
C();
}
private function C():void {
if (resultFromRemoteCallA && resultFromRemoteCallB) {
// do some thing with the results.
}
}
private function update():void {
resultFromRemoteCallA = resultFromRemoteCallB = null;
A(); B();
}
If you expect null values, you might want to use a boolean variable to track the invocation of the result handler instead.
EDIT: since the author indicated that the dispatch happens in another class, another way to do it would be to pass along a responder and attach it to the AsyncToken like so (in the callee):
private function dispatchRemoteCall(resultHandler:Function, faultHandler: Function): void {
var resultToken: AsyncToken = remoteObject.foo('bar'); // remoteObject may or may not have a result/fault handler
resultToken.addResponder(new mx.rpc.Responder(resultHandler, faultHandler));
}
Then, you can pass along listeners to be invoked when the remote call finishes (at which point you can choose to let the dispatching class store the result or handle it in the caller), like so:
private function update(): void {
classA.dispatchRemoteCall(handleRemoteCallA, handleRemoteCallAFault);
}
If you find yourself doing this a lot, you may also want to look into having a framework do global event routing, like Parsley or Spring Actionscript.

Resources