Thursday, September 20, 2007

About "no need to restart server for any changes" statement in Rails

which is actually not exactly true.....

If you have a helper function (which is basically a "ruby thread") running background jobs, please remember to restart the server if any changes are made to them.

I guess the reason why we need to do that is related to ruby thread implementation. I am not sure. I will update here if I find anything.

Monday, September 10, 2007

Two Equivalent Bitwise Exps

(a & b) | (a & c) | (b & c)

(a | b) & (a | c) & (b | c)

at each bit of a, b and c, at least two of them should have 1.


Updates: [tested]
in Java, this applies for types like int, char, Integer, long, etc
Doesn't work for float, String, double.

Swap Variables with Zero Space Requirement

void zero_space_swap(int* x, int *y){
     *(x) ^= *(y);
     *(y) ^= *(x);
     *(x) ^= *(y);
}

Sunday, September 9, 2007

Online selection algorithm. Random Selection in Linked Lists with Unknown Length

Question: Given a finite linked list with unknown length(in terms of nodes count), select one item(multiple nodes) with equal possibility for every node in the linked list.

* One item only.
Assuming the input list has k nodes. The possibility for each node to be selected is 1/k. We need to guarantee that after we visiting m nodes, the possibility for each node is 1/m.



Proof of correctness:
When i = 0, node 0 is selected with 100% possibility.
When i = 1, node 1 has 50% chance to replace node 0 as selection result. Both of node 1 and node 0 will have equal chance to be chosen.
When i = n(which means we are testing the (n+1)th node), before line 3 is executed, all nodes from node 0 to node n-1 have possibility of 1/n to be selected. The (n+1)th node has 1/(n+1) possibility to be selected at line 3. For the previous selected node, it has a possibility of n/(n+1) to survive this round. Multiplying with its chance to be selected: 1/n, we get 1/(n+1) as well,

*Extended: Multiple items.
Assuming we have k nodes in the list and we are targeting a result set of size m,
each node will have possibility of m/k to be added to the result set.


Sunday, July 8, 2007

[NesC] Message Format Sent Over Serial Ports [T2 Only]

* TinyOS 2.x

A message sent through serial ports from motes to PC will typically look like this:

00 FF FF 00 00 08 00 09 19 77 02 07 00 13 00 00



  • The first byte 00 is the leading zero.

  • After that, the whole packet is of the type "serial_packet_t", defined in tinyos-2.x/tos/lib/serial/Serial.h.

    • Inside the serial_packet_t structure, the first 7 bytes are of the type "serial_header_t", which is also defined in tinyos-2.x/tos/lib/serial/Serial.h.

      In the case above, it would be FF FF 00 00 08 00 09.

      Within the "serial_header_t", the following structure takes place:
      • nx_am_addr_t     dest     // Destination (2 bytes)
      • nx_am_addr_t     src       // Source (2 bytes)
      • nx_uint8_t            length // Payload Length (1 byte)
      • nx_am_group_t group  // Group ID (1 byte)
      • nx_am_id_t          type     // Active Message Handler Type (1 byte), some enum value that you can define yourself.

    • After the 7 byte header, here comes the payload, which can be up to TOS_DATA_LENGTH (28 bytes) long. The content could be anything.

      In the above example, the payload is: 19 77 02 07 00 13 00 00, which is of the length (8 bytes) denoted in the "Payload Length" field.



To note, besides the payload part, we need to fill out the destination address and payload length field by calling SerialActiveMessageC.AMSend(am_addr_t dest, message_t* msg, uint8_t len). It would be a good programming manner to fill the len with sizeof(). And we also need to specify the Active Message Handler Type by wiring the AMSend to SerialActiveMessage.AMSend[am_id_t id];

ref:
http://www.tinyos.net/tinyos-2.x/doc/html/tutorial/lesson4.html. The doc here doesn't seem to be up-to-date since it left out the source address from the header.


Tuesday, March 27, 2007

[NesC] Global Variables in NesC

It seems that there are not such support in NesC.

I've tried to apply the "extern" storage in C here but it does not work. The compiler complains about undefined reference to that variable during linking stage.

The only way I can think of is to store the data in components, and to provide get/set interfaces of those components.

Friday, March 9, 2007

[NesC] NesC syntax highlighting in UltraEdit

Attach the following section at the end of "wordfile.txt" file in your UltraEdit installation directory. Just some trivial modification from the original C/C++ syntax highlighting wordfile.

Say your UE currently supports n languages highlighting.

/L($n+1)"NesC" NesC_LANG Line Comment = // Block Comment On = /* Block Comment Off = */ Escape Char = \ String Chars = "' File Extensions = NC H
/Delimiters = ~!@%^&*()-+=|\/{}[]:;"'<> , .?
/Function String = "%^([a-zA-Z_0-9^[^]*]+^)[ ^t]+([^p*&, ^t^[^]a-zA-Z_0-9.!]++)[~;]"
/Function String 1 = "%[a-zA-Z_0-9*]*::^([a-zA-Z_0-9^~]+^)[ ^t^p]++([^p*&, ^t^[^]/*=:&amp;amp;amp;a-zA-Z_0-9./(!]++)[~;]"
/Function String 2 = "%[a-zA-Z_0-9^][a-zA-Z_0-9^[^]]+[ ^t*]+^([a-zA-Z_0-9]+^)[ ^t]++([^p*&, ^t^[^]a-zA-Z_0-9./(!]++)[~;]"
/Function String 3 = "%[a-zA-Z_0-9*&$^[^]*]+[ ^t]+[a-zA-Z_0-9*&amp;amp;amp;$^[^]]+[ ^t*]+^([a-zA-Z_0-9]+^)[ ^t]++([^p*&, ^t^[^]a-zA-Z_0-9./(!]++)[~;]"
/Function String 4 = "%[a-z_0-9^[^]*]++ [a-z_0-9*^[^]]+[ ^t]++[a-z_0-9*^[^]]+[ ^t]++^([*a-z_0-9]+^)[ ^t]++([^p*&, ^t^[^]a-z_0-9./(!]++)[~;]"
/Function String 5 = "%^([a-zA-Z_0-9^[^]*]+^)[ ^t]++([^p*&, ^t^[^]a-zA-Z_0-9./()!]++)[~;]"
/Indent Strings = "{" ":"
/Unindent Strings = "}"
/Open Brace Strings = "{" "(" "["
/Close Brace Strings = "}" ")" "]"
/Open Fold Strings = "{"
/Close Fold Strings = "}"
/C1"Keywords"
auto
break bool
case char const continue
default do double defined
else enum extern
float for
goto
if int
long
register return
short signed sizeof static struct switch
typedef
union unsigned
void volatile
while
__asm __based __cdecl __declspec __except __far __fastcall __finally __fortran __huge __inline __int16 __int32 __int64 __int8 __interrupt __leave __loadds __near __pascal __saveregs __segment __segname __self __stdcall __try __uuidof
#define #error #include #elif #if #line #else #ifdef #pragma #endif #ifndef #undef
/C2"NesC Keywords"
as async atomic
call command components configuration
default
event
implementation includes interface
module
post provides
signal
task
uses
/C3"Operators"
+
-
=
// /
*
~
%
&
>
<
^
!
|

The "dictionary" in it is sufficient for me but you can extend it as long as necessary.

Be aware: replace "($n+1)" with the actual index number.

Sunday, March 4, 2007

[NesC] Weird problem about typedef in header file

Environment:
Cygwin + Tinyos 1.15
gcc3.3.3

I was trying to compile a program with several structures defined by myself in a header file. And these structures are passed through some interfaces as parameters.

At first I didn't include the header file in the top level wiring configuration but only in the modules using it. However the compiler reported "conflicting types" on the parameters between my event handler definition and the "previous declaration" of that event in the interface definition file. This is weird since these two lines are exactly the same and to be perfectly cautious, I copy the definition to replace my event handler definition. It didn't work neither.

I tried to google it, no luck. Then I started to remove the parameters one by one. Finally after I removed the parameters of the structures defined by myself from the interface definition, it works. It seems although the compiler is able to translate that structure in the interface definition file into the correct type but unable to translate the ones in my application module although I included the header file as well. One solution to this problem is to replace that data structure with build-in types. That solved the problem, but it will become quite annoying.

Since it seems that the compiler have trouble in mapping the use of self-defined structures into correct definitions of those structures, I tried to include that file in the top level wiring configuration. Well, another weird thing happened. The compiler didn't recognize typedef in the header file at all.

It even complained about instructions like:

typedef uint8_t HANDLE;
The error appeared like this:

"parse error before "typedef"".

No better solutions so far. As the deadline of paper approaches, I have to give up trying on this single point and move on. Any suggestions are welcome and appreciated! Thanks!



Friday, February 23, 2007

[Dev Tools] How to setup CVS server on Ubuntu

  1. Variables that might need to be geared towards your own installation:
    • INSTALLDIR: the dir where cvsd is installed.
    • USERNAME: the name for cvs user to connect to the server.

  2. install CVS files:
    sudo apt-get install cvs

    install CVS server:
    sudo apt-get install cvsd

    (it is also OK to use aptitude)
    When prompted in the cvsd installation process for Repository, type in “/cvsroot”.

  3. Configure CVS
    goto /var/lib/cvsd and build the cvsroot:
    sudo cvsd-buildroot /var/lib/cvsd

    create the folder cvsroot:
    sudo mkdir cvsroot

    and then initilize the repository
    sudo cvs -d /var/lib/cvsd/cvsroot init
    sudo chown -R cvsd:cvsd cvsroot

    create a user and password:
    sudo cvsd-passwd /var/lib/cvsd/cvsroot +$USERNAME

    and then change the AUTH type:
    sudo vi /var/lib/cvsd/cvsroot/CVSROOT/config
    uncomment the “SystemAuto=no” line.

  4. Test it
    cvs -d :pserver:$USERNAME
    @localhost:/cvsroot login
    cvs -d :pserver:
    $USERNAME
    @localhost:/cvsroot checkout .

Reference: http://www.sofee.cn/blog/2006/06/14/5/ by Justin