Wednesday, October 03, 2007

How to Reboot a Machine via PowerShell

I tried search online for this, however, most of the online answers will ask you to run the following command as an admin

$OperationSystemObject = Get-WmiObject Win32_OperatingSystem -Comp "yourcomputername"
$OperationSystemObject.Win32Shutdown(6)
or
$OperationSystemObject.Reboot()

However, I keep getting an error saying that "Privilege not held"



I even tried to use the credential switch but still not working. Finally, it turns out that it is not me not having the right privilege. It is the thread itself need to have the right permission to shut the machine down. So here is the correct way to do this.

$OperationSystemObject = Get-WmiObject Win32_OperatingSystem -Comp "yourcomputername"
$OperationSystemObject.psbase.Scope.Options.EnablePrivileges = $true

$OperationSystemObject.Win32Shutdown(6)
or
$OperationSystemObject.Reboot()


Wednesday, April 19, 2006

Java Answering Machine for Skype (PC)

I recently developed a answering machine for Skype on PC. It can be downloaded in the download page of the FLE website. The addon will be activated if the incoming call is not picked up after a preset time interval. It plays a greeting message to the caller and then record the message. You will not hear anything while the message is recording. The message can be played later.

The application is developed in Java. Virtual Audio Cable (VAC) is required to since audio stream API for Skype is not provided. A trial version of VAC is included in the download package. However, you'll hear the watermarked voice.

Wednesday, April 12, 2006

Change Page Size in Latex

Goal: The output of the latex on my machine is alway A4, I want to change it to USletter format. There must be more than one way to do this. But I only find one. The solution was found here.

1. install the vmargin package.
2. In pdftex.cfg ($TEXBASE/texmf/pdftex/config/pdftex.cfg) remove or modify the lines:
page_width 210truemm
page_height 297truemm
So that they correspond to your desired default pagesize. An example entry for letter sized paper would read:
page_width 8.5truein
page_height 11.0truein

3. Additionally in the Vmargin style file ($TEXBASE/texmf/tex/latex/misc/Vmargin.sty) at or around line 225 you should change the following default settings so that they reflect the correct default paper size and margins:
\setpapersize{A4}
\setmarginsrb{35mm}{20mm}{25mm}{15mm}{12pt}{11mm}{0pt}{11mm}

Change it to :

\setpapersize{USletter}
\setmarginsrb{1in}{1in}{1in}{1in}{0pt}{0mm}{0pt}{0mm}

Will set up the default page layout to be the standard USLetter (8.5x11.0in) with 1 inch margins and no headers or footers.

It should be noted that for some reason calls to \setpapersize{} that are made within the document seem to be ignored by pdflatex. Even so, it is advised that you include a proper \setpagesize{} and \setmarginsrb{} call so that pagesize can be controlled when building documents with the standard latex package.

Thursday, March 30, 2006

Java: Capture Using Specified Audio Device

The original tutorial is here

At the end of the tutorial, it give the source code that can capture audio using a specified device. You can do similar thing to change the output device.

      //Get and display a list of
// available mixers.
Mixer.Info[] mixerInfo =
AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for(int cnt = 0; cnt < mixerInfo.length;
cnt++){
System.out.println(mixerInfo[cnt].
getName());
}//end for loop



//Select one of the available
// mixers.
Mixer mixer = AudioSystem.
getMixer(mixerInfo[3]);





Friday, March 24, 2006

Skype Java API 2.0 Learning Notes (2) -- Sample Code Found

Finally, some sample codes for Java Skype API 2.0 are found.

1. Print out all the text chat message that received.

public class Main {
public static void main(String[] args) throws Exception {
final Object lock = new Object();
Skype.addMessageReceivedListener(new MessageReceivedListener() {
public void messageReceived(Message message) {
try {
System.out.println(message.getMessage());
} catch (SkypeException e) {
}
synchronized (lock) {
lock.notify();
}
}
});

synchronized (lock) {
lock.wait();
}
}

}

Note: Right now, I'm not sure why it need to aquire a lock in the code.

2. Code segment for answer the incoming call automatically.

Skype.addCallReceivedListener(new CallReceivedListener() {
public void callReceived(Call call) {
try {
call.answer();
} catch (SkypeException e) {
}
}
});

Thursday, March 23, 2006

Skype Java API 2.0 Learning Notes (1) -- Overview

The Java API for Skype is develope by Hisano using eclipse SWT library. It can be downloaded here (http://sourceforge.jp/projects/skype/files/). Unfortunately, I'm not able to find sufficient documentation for it. Here is some note made by Terry who tried this API

01. The Skype API on Windows.

These notes assume the reader has:

a. solid experience with the most current JDK;
b. at least browsed the documentation on the Skype 2.0 API; and
c. has looked at the examples from the Skype API Guide.

The notes DO NOT assume that the reader has:

a. actually tried to build an application using any of those other
wrappers or interfaces to the Skype API;
b. an in-depth understanding of the Windows Win32 API; nor
c. an in-depth understanding of SWT.

The key characteristics of the Skype API are:

a. it uses a Windows interprocess communication protocol based on Messages;
b. the protocol is basically asynchronous and stateless.

There is no particular send-receive sequence that your Java Client application can use in 'blocking mode' to obtain a known copy of the Skype Client's state. The Skype client must effectively be treated as a 'black box' that sends messages to your client either in response
to specific requests for information or as unsolicited notification events.

02. The Java Skype Interface Architecture.

A Connector class object is used to encapsulate the communication between the Skype Client and your Java Client. The Windows version of that abstract class is W32Connector. That is where the SWT wrapper for Microsoft's Win32 API is used to establish and maintain a path by
which message requests can be sent to the Skype Client and messages received from the Skype Client can be passed back to your application as simple Strings. This 'Loop Forever' listening code runs as a separate Thread, "Win32EventDispatcher".

NOTE # 1 for SWT Programmers: The loop used is the standard SWT event processing loop. If you are using this library in a true SWT application, you already have a well-known place where you invoke this loop logic. If this library's use of that loop causes your application to fail, it might be better to replace it with Thread.wait logic that could be notified when your application is ready to shutdown. If your application does not use any SWT U/I, it is a daemon-like service,
this method of loop-waiting should work just fine.

NOTE # 2 for SWT Programmers: Messages sent from your Java Client application to the Skype Client are scheduled through the SWT event-handling thread. I don't think this approach is really necessary as the sendCommand code does not 'touch' any Display-managed code. If your use of this library involves a lot message sending, you may want to reduce some of the overhead
by removing the dependence upon 'Display.asychExec'; otherwise, there is probably no harm in this 'protective' programming approach.

03. Java Skype Callbacks.

The 'hooks' for your Java Client application to respond to what is going on with the Skype Client are constructed as Listeners. Depending upon what Skype Client activity you want to handle,
you will add one of these Listeners:

a. Connector.
b. Call.
c. CallReceived.
d. MessageReceived.
e. Application.
f. Stream.

The Connector Listener Interface is the most import one. Its messageReceived method is called back for every message sent by the Skype Client. However, because it is so important, the Java Skype library performs some automatic processing on the messages in addition to whatever processing your application might require.

One such 'automatic processing' is debugging. If you turn on debugging, incoming message traffic is written to stdout. I recommend you change that to write to stderr. Output to stdout is buffered, so what you write does not always immediately make it to your screen. When trying to understand low-level message flows, if input and output debugging text is sometimes buffered, it is almost impossible to understand the real sequence of events represented by the flow of control in the code.

Alternately, and probably better for gaining understanding, do not turn on debugging mode, instead catch all the incoming messages and examine them in your own code. It turns out,
at least with the present code, that you have to examine all incoming message traffic in order to know when a call has ended.

The Call Listener Interface provides a statusChanged callback method. At the present time, that call back never seems to be made. It is apparently intended as a convenience for keeping
track of Call Status without having to intercept and process messageReceived on the Connector Listener Interface.

The Call Received Listener Interface is clearly just such a convenience; the callReceived method is invoked when the Connector Listener Interface detects the RINGING state change.

The Message Received Listener Interface is a similar convenience whose messageReceived callback will only be invoked when a new Instant Message is received from the Skype Client.

The Application Listener Interface may be a little confusing if you think that you are writing an application. You are writing an application, but the 'attaching' and 'unattaching' of your Java Client to the Skype Client IS NOT handled here. The attach-unattach state changes are handled through the Connector Listener Interface. This interface's connected and disconnected methods are only called back when you try to use the APP2APP part of the Skype API.

The Stream Listener Interface's textReceived and datagramReceived methods are only called back when message traffic through the APP2APP protocol is received by your Java Client.

04. Java Skype Thread Issues.

As mentioned above, because the Java Skype library borrows its COM interface from SWT, there are already a couple of threads involved as soon as your Java Client application gets
a Connector instance. That multi-threading, however, is just a by-product of the way a typical SWT application needs to be built in order to have fast screen rendering along with fast event processing; it is not really germane to our concerns.

When the "Win32EventDispatcher" does receive some incoming traffic from the Skype Client, it does fork a new Thread, "Win32MessageSender", for each message.

Note that it DOES NOT fork a new Thread for each Listener queued up to handle messageReceived; all such callbacks are handled 'first-in-first-out' in a single Thread. So, if your application's processing of any incoming traffic incurs delays, you need to fork a separate Thread to handle that work with the least interference for other Listeners behind you in the queue.

Beware that Connector's listener List can grow in ways that that you probably would not expect. Every message sent to the Skype Client [Connector.execute] pushes a new Listener on to the List. So, for example, if your Call Received Listener does something as simple as ask for the Call Type, a message will have to be sent to the Skype Client to obtain that information. That will push another Listener on to the List. The Call object really only has one field, ID, that it can
provide without a round-trip through the message-processing protocol.

There are obviously some changes to the Skype API that would be beneficial, but without those enhancements, there is little that the Java Skype library can do. We could imagine some 'intelligence' in the message-receiving layer that would, for example, upon receipt of a new Call, cache that information, make additional requests of the Skype Client, and only 'release' a new call to our application after the Call object had been populated with the additional fields. That would definitely make our work easier, but it would also delay Call notification and increase the error handling logic.

So, for now, the important guideline is: Fork a separate Thread for non-trivial message handling code, and even then, be careful about generating additional message traffic in your message handling code.

Wednesday, October 12, 2005

A simple Java program to do Rectangle Placement

A simple java program to place some rectangles given their coodinates and sizes.

The input file should follow the format of

x1, y1 , width1, height1
x2, y2 , width2, height2
...


or if a label is attached to each rectangle, the format change to

label1, x1, y1 , width1, height1
lable2, x2, y2 , width2, height2
...

A simple test case

A 0 0 5 5
B 6 6 4 7
C 1 5 3 5


Output is




Try to increase the scale asked by the program if the graph squeezed together.

Download the executable jar file here.
Download source code here.


Hello Blogger

Hello world !