Google
 
Web unafbapune.blogspot.com

Thursday, November 24, 2011

 

Yet another Luhn check and digit generation

But this time it's faster:

/**
* @param s non-empty string that contains only digits
* @return true only if the given digits pass the luhn check
*/
boolean isLuhn(String s) {
char[] ca = s.toCharArray();
int sum = ca[ca.length-1] - '0';

for (int i=ca.length-2; i >= 0; i-=2) {
int x2 = (ca[i] - '0') << 1;
sum += x2 < 10 ? x2 : 1 + (x2 - 10);
}
for (int i=ca.length-3; i >= 0; i-=2)
sum += ca[i] - '0';
return sum % 10 == 0;
}

/**
* @param s non-empty string that contains only digits
* @return the generated Luhn digit
*/
int calculateLuhnDigit(String s) {
char[] ca = s.toCharArray();
int sum = 0;

for (int i=ca.length-1; i >= 0; i-=2) {
int x2 = (ca[i] - '0') << 1;
sum += x2 < 10 ? x2 : 1 + (x2 - 10);
}
for (int i=ca.length-2; i >= 0; i-=2)
sum += ca[i] - '0';
int mod = sum % 10;
return mod == 0 ? 0 : 10 - mod;
}

Saturday, November 19, 2011

 

Java 7 behaves differently on InetAddress.getHostName()

On my Mac, if I execute the following code in Java 6:
System.out.println(InetAddress.getLocalHost().getHostName());
I got:
5855pns93223
But when run in Java 7, I got:
5855pns93223.nznmba.com
This causes problem with some existing Java mail code, with exception like:
Caused by: javax.mail.MessagingException: 501 5.0.0 HELO requires domain address
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
This isn't good for unit testing when I need to run them locally.

Solution ?

Add two entries to my /etc/hosts:
127.0.0.1 5855pns93223
127.0.0.1 5855pns93223.nznmba.com
A hack, I know, but a quick fix at least for now :)

Friday, November 18, 2011

 

Scriptfu comparing memcached hit counts

If we have a text file "hosts" that contains all the host names in a memcached fleet, we can readily capture a snapshot of the memcached's "get_hits" counts of the entire fleet to a file "hits", like so:

for i in `cat hosts`
do
echo -ne 'stats\r\n' | nc -i1 $i:11211 | grep get_hits | cut -d ' ' -f3 | sed 's/[^0-9]//'
done | tee hits

(Do you see why we need to do the sed at the end ?) Anyhow, here is the fun part:

Given we have captured two files "hits.1" and "hits.2" at T1 and T2, how can we show the increments of the "get_hits" count of every host ? With a one-liner, that is :)

Solution:
paste -d '-' hits.2 hits.1 | xargs -I {} echo {} | bc -iq | paste hosts -
Neat, huh ?

Saturday, November 12, 2011

 

How to compile with Libevent on Mac ?

Assuming Libevent has been installed on Mac, and now try to compile something that uses it such as the toy low-level ROT13 server with Libevent:

$ gcc libeventBasedRot13Server.c
would failed with something like:
Undefined symbols:
"_evutil_make_socket_nonblocking", referenced from:
_do_accept in cc2B6M02.o
_run in cc2B6M02.o
"_event_new", referenced from:
_alloc_fd_state in cc2B6M02.o
_alloc_fd_state in cc2B6M02.o
_run in cc2B6M02.o
"_event_add", referenced from:
_do_read in cc2B6M02.o
_do_accept in cc2B6M02.o
_run in cc2B6M02.o
"_event_base_dispatch", referenced from:
_run in cc2B6M02.o
"_event_free", referenced from:
_alloc_fd_state in cc2B6M02.o
_free_fd_state in cc2B6M02.o
_free_fd_state in cc2B6M02.o
"_event_base_new", referenced from:
_run in cc2B6M02.o
"_event_del", referenced from:
_do_write in cc2B6M02.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Solution:
$ gcc libeventBasedRot13Server.c /usr/local/lib/libevent.a

Sunday, November 06, 2011

 

How to turn off class file verification in junit ant task ?

Example:

    <junit printsummary="yes" haltonfailure="yes" fork="true">
<jvmarg value="-noverify" />
...
</junit>
(Why ? This was necessary to get Cobertura to run in peace with Java 7)

This page is powered by Blogger. Isn't yours?