Thursday, June 25, 2009

PhpXmlRpc localhost problem

Found a problem using PhpXmlRpc library today:

Running XAMPP on Windows Vista machine, PHP ver 5.

I have an XML-RPC service running on my localhost.  I can access the service php page via browser.

Both my XML-RPC client and server resides on “localhost”.

However, when I create the XML-RPC client using “localhost” as the server address, the client can’t connect to the server. 

$client = new xmlrpc_client("/hs/service/users.php", "localhost", 80);

After replacing the “localhost” with “127.0.0.1”, the client works fine.

$client = new xmlrpc_client("/hs/service/users.php", "127.0.0.1", 80);

I’m still not sure what causes the issue.  Just take a note here for future references.

Tuesday, March 24, 2009

Port tinypy to Android

Full text can be found on my wiki:
http://www.caichoi.com/mediawiki/index.php?title=Porting_tinypy_to_Android_Project

  1. Write a native tinypy interpreter that
    • reads in Python script from stdin
    • prints execution results to stdout
  2. Create the tinypy process in Android Java application by calling Runtime.exec()
  3. Communicate with the native process on its I/O stream.

Sunday, January 11, 2009

[Flex] Control creation time

In my Flex gallery program, I have a custom MXML component 'DisplayImage' and a piece of program that popup the component.
The 'DisplayImage' component is just a with a in it.
I instantiate a 'DisplayImage' control and popup it using the following code in my main application:

...
var img:DisplayImage = new DisplayImage();
img.innerImage.source = file;
PopUpManager.addPopUp(img, this, true);
...

When I run the program, I'll get error information saying the Image control in my DisplayImage control is null.
I tried to change the object 'img' to mx:Image type and the program runs correctly.
If I add the DisplayImage to the popup manager before I set the image source, the program runs correctly, as well.
It seems to me a compound component like my DisplayImage is not instantiated until paint is required.  However, an simple  Image component gets instantiated right after being constructed.

Saturday, June 21, 2008

[ANTLR] Add children to derived Tree

I defined a Tree node class from org.antlr.Runtime.Tree.CommonTree, say MyTree. When I add a MyTree object as a child to another tree object, the MyTree node is not actually added.

The problem is that I didn't override Tree.isNil() method. When Tree.addChild() is called, it check to see if the node is an imaginary node by isNil(). If it is and it has no children, the tree node will not be added.

After forcing MyTree.isNil() to return true, the MyTree object can be added as a child.

Reference: http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1tree_1_1_base_tree.html#ffa585d68d73d750699da8fb311226b2

Wednesday, May 07, 2008

Syntax color for "label" in ANTLRWorks

One of my frequent mistake using ANTLRWorks is that I always forget the $ prefix of the labels.  By changing the syntax color setting and change the default black color for "labels" would help remind me of that.  
But the rule arguments and return values aren't yet recognized as "label" though they need a "$" prefix as well.  If these variables can be highlighted, that would be more helpful.

Monday, April 28, 2008

Use Scope definition instead of rule parameters in ANTLR3

In the SimpleC to LLVM translation project, I need to pass a parameter as a flag variable into a parser rule.  However, if I add a parameter to the rule declaration, I have to change all the references to that rule into rule[args].

As an alternative solution, I use Scope definition and put that flag variable in to the declared scope.  Whenever I need that flag variable, I can import that global scope within the parser rule to get reference to that variable.  

In this case:
grammar G;
assignment
scope {boolean isLeftHandSide;}
    :  {$assignment::isLeftHandSide=true;}ID '=' 
       {$assignment::isLeftHandSide=false;}ID
    ;
...
expression 
    :  ID {if($assignment::isLeftHandSide) doSomething();}
    ;

Reference: The Definitive ANTLR Reference, chapter 4.5

Friday, April 25, 2008

Java sound

After switched to MAC, I can't find a bemani emulator for MacOS. I want to write one myself.
I'm trying to make Java play wave audio. Found a sample code online. But the sound doesn't play in run mode. It only plays in debug mode. I found the Clip.drain() method can block the program till the audio data in I/O is empty/played.

import javax.sound.sampled.*;
import java.io.*;
import java.net.*;

public class EntryPoint {
public void playSound() {
try {
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("/Users/shaotingcai/Documents/USF/cs652/JavaSound/src/me.wav"));

// From URL
//stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));

// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}

// Create the clip
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);

// This method does not return until the audio file is completely loaded
clip.open(stream);
System.out.println("opened.");

// Start playing
clip.start();
clip.drain();
System.out.println("played");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
EntryPoint main = new EntryPoint();
main.playSound();
}
}