tag:blogger.com,1999:blog-199674532024-03-04T21:40:08.941-08:00Musings of a Software Developerby Krishna VangapanduKrishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.comBlogger195125tag:blogger.com,1999:blog-19967453.post-56504563157607010212014-06-11T12:50:00.002-07:002014-06-11T12:50:28.995-07:00Moved to GithubMoved my blog from here to <a href="http://krishnabhargav.github.io/">krishnabhargav.github.io</a>Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-67667732635066540962014-01-03T06:45:00.000-08:002014-01-03T06:46:29.768-08:00Few handy things to know in Go Library (Command line arguments, Reflection, Setting Bits, Hash functions)<h4>
Command line arguments in Go</h4>
<div>
In order to access the command line arguments passed to a Go executable, we work with the "flag" package from the standard Go library. Shown below is an example of passing command line arguments.<br />
<pre name="code">go run cla.go Krishna</pre>
Or if you have an executable obtained from go build, you can run it as<br />
<pre name="code">./cla Krishna </pre>
Shown below is an example go snippet that accesses the first argument passed & prints it.<br />
<pre class="c" name="code">package main
import (
"fmt"
"flag"
)
func main() {
//Without the call to Parse(), arguments may not be accessed
flag.Parse()
fmt.Printf("%s\n",flag.Arg(0))
}</pre>
Notice that you will have to invoke the call to flag.Parse() without which you will not be able to access the arguments.<br /><br />
<h4>
Reflection support in Go</h4>
</div>
<div>
We take the previous example, add some code for reflection which will help us understand how reflection library may be used. Just some simple example without having to go deep. For that, read this <a href="http://blog.golang.org/laws-of-reflection">post from the official Go blog</a>.
<br />
<br />
So what is the type returned by flag.Arg(0) ? We will start from there and go use some functions.<br />
<pre class="c" name="code">package main
import (
"fmt" //regular format package
"flag" //package from command line arguments
"reflect" //package for reflection
)
func main() {
//Without the call to Parse(), arguments may not be accessed
flag.Parse()
fmt.Printf("%s\n",flag.Arg(0))
passed := flag.Arg(0)
//TypeOf actually returns a Type type
fmt.Println("typeof : ",reflect.TypeOf(passed))
//ValueOf actually returns a Value
fmt.Println("valueof : ",reflect.ValueOf(passed))
//Example functions from the Value type
fmt.Println("type: ",reflect.ValueOf(passed).Type())
fmt.Println("kind: ", reflect.ValueOf(passed).Kind())
fmt.Println("interface:", reflect.ValueOf(passed).Interface())
}
</pre>
<h4>
Big Integer & Set Bits</h4>
</div>
<div>
In the future post, I would like to implement a Bloom filter in Go but for that I will need to know how to do the following:</div>
<div>
- Create a big number and set some bit "x" to 1 or 0.</div>
<div>
- Accept a string, convert it to byte array, then hash the byte array.</div>
<div>
<br /></div>
<div>
To support the above, I wanted to see how simple big numbers would work. I am only looking enough to support my purpose. </div>
<div>
<br /></div>
<div>
Big Numbers (multi-precision numbers) are supported using "math/big" package in Go library. Shown below is an example code that does that.</div>
<pre class="c" name="code">package main
import (
"fmt"
"math/big"</pre>
<pre class="c" name="code"> "unsafe"
)
func main() {
//create a new big integer
filter := big.NewInt(0)
fmt.Println("filter value: ",filter)
//accessing the sizeof from unsafe package
fmt.Println("size of : ",unsafe.Sizeof(filter))
//example of how to cast uintptr to int
filter.SetBit(filter,int(unsafe.Sizeof(filter)*8)-1,1)
fmt.Println("filter after setbit last to 1 : ",filter)
}
</pre>
<br />
The above example shows a few things:<br />
- Creating an instance of big.Int using the NewInt() call.<br />
- Accessing the size of the big.Int using unsafe package exported SizeOf() function<br />
- SizeOf() returns uintptr and we convert that to integer - that is a good example of casting<br />
- How to set bit on the big.Int using SetBit<br /><br />
<h4>
Working with Hash functions</h4>
<div>
Example below demonstrates the following</div>
<div>
<ul>
<li>how to convert a string to a byte array</li>
<li>how to use FNV Hash from the standard Go library</li>
</ul>
</div>
<pre class="c" name="code">package main
import (
"fmt"
"hash/fnv"
)
func main() {
//Create a hash fnv function
hash := fnv.New32()
name := "Krishna"
//converting string to byte array
namebytes := []byte(name)
fmt.Println("hash reset : ",hash.Sum32())
hash.Write(namebytes)
fmt.Println("hash of Krishna : ",hash.Sum32())
hash.Reset() //resets the hash
fmt.Println("hash reset : ",hash.Sum32())
//write again
hash.Write(namebytes)
fmt.Println("hash of Krishna : ", hash.Sum32())
}
</pre>
Hash libraries in the Go "hash" package implements the Hash interface. The implementations in the standard Go library as of this time are adler32, crc32, crc64, fnv-1/fnv-1a. The above example creates an instance of FNV hash, writes a byte array to it and then gets the value through Sum32() call. You can "forget" what is written to it by Reset() function which will allow you to reuse the hash instance.<br />
<br />
I am newly learning Go and along the way sharing some things that I learnt. If you know of a better way to accomplish what I have shared here, please feel free to comment.Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-18557229192946448142014-01-01T20:30:00.002-08:002014-01-02T05:40:54.493-08:00Working with "Packages" in Go languagePackages in Golang<br />
<br />
The specification states that all source files for a package be stored in the same directory.<br />
You can import a package as shown below.<br />
<br />
<pre name="code">import "fmt"
</pre>
<br />
In this case, the Printf method may be accessed as fmt.Printf(). All other identifiers exported by the package is also accessible as fmt.XXXX().<br />
<br />
You can also do the following.<br />
<br />
<pre>import f "fmt"</pre>
In this case, the qualifier is "f" instead of the default "fmt". So you access as "f.Printf()".<br />
<br />
One other way is the following.
<br />
<pre> import . "fmt" </pre>
In this case, you dont need a qualifier for the exported identifiers from the fmt package.
You may also use the blank identifier (underscore : _) as the qualifier name for cases where you wish the initialize the package but not necessarily import its exported identifiers.
<br />
Source files of a package have an init() function. See below for more details.<br />
The previous example of a simple Web Server is now split into two files
<br />
<pre class="c" name="code">//GoWebServer.go
package main
import f "fmt"
//init() is a special function that gets initialized.
func init(){
f.Printf ("Package Initialized\n")
}
func main() {
f.Printf("Web Server will be started at port 8080\n")
runWebServer()
}
</pre>
<pre class="c" name="code">//WebServerHelper.go
package main
import (
. "fmt"
"io"
web "net/http"
)
func init(){
Printf("Initializing WebServerHelper.go \n")
}
func runWebServer(){
rootHandler := web.HandlerFunc(func(w web.ResponseWriter, r *web.Request) {
io.WriteString(w, "Hi there, I love "+r.URL.Path)
})
srv := web.Server{
Handler: rootHandler,
Addr: ":8080",
}
srv.ListenAndServe()
}
</pre>
Few things that I learnt when trying to make this work.<br />
- You need to specify all the files required for your main program to build properly. So you may<br />
<pre>go build</pre>
Or you may run<br />
<pre>go build *.go</pre>
- You can also specify output to be generated as<br />
<pre>go build -o output/gowebserver</pre>
Or be more explicit<br />
<pre>go build -o output/gowebserver *.go</pre>
- Go complier does not like if a package is imported but not used. This is good. I cannot tell how annoying i find C# using statements that are just there but no one really uses them.<br />
<div>
<br /></div>
<div>
Notice that both the files have an init() function defined and when I build everything and run the program, the output is as follows:</div>
<pre>Package Initialized
Initializing WebServerHelper.go
Web Server will be started at port 8080
</pre>
<br />
Such init() functions may be defined multiple times even in the same source file & the execution order is unspecified/undefined. So we should not write our initialization logic based or the execution order.
<br />
And init() cannot be called by anyone else. Packages are initialized only once. So if you import packages P & Q but Q also imports P, then P is initialized only once.<br />
<br />
Package initialization is comprised of variable initialization (package level variables are all assigned initial values) & invocation of init() functions - performed one package at a time in a single go routine.<br />
<br />
See below for a basic program that indicates the above said sequence - variable initialization followed by init() method.
<br />
<pre class="c" name="code">package main
import "fmt"
//x := 2000
var x int = 2000
func init(){
var y int = x
fmt.Printf("X value is %d\n",y)
}
func main(){ }
</pre>
<br />
x is a global variable (which by the way cannot use the shortcut notation that is commented) that is initialized to 2000 and that is what gets printed when the program is executed.<br />
<h4>
Local Packages in Go</h4>
When you say
<br />
<pre>import "fmt" </pre>
the package "fmt" is looked for in the standard Go tree. Then it is looked for in "$GOPATH/pkg" directory where $GOPATH is the environment variable set to some directory on your machine. Commands such as "go get" also looks for this directory. You can read more about it <a href="http://golang.org/pkg/go/build/">here (look for Go path)</a>.
So what about local packages - the one you do not want to put in GOPATH but leave it in the current directory for your application? To make this work, I renamed the package declaration (which originally was main) for the WebServerHelper.go as
<br />
<pre>package ws</pre>
Then I created a folder with the same name as the package & placed the .go file in the folder. Then my import statements on the GoWebServer.go (the main file) has changed to
<br />
<pre class="c" name="code">import (
f "fmt"
ws "./ws"
)
</pre>
Notice the <b>"./ws"</b> declaration. So the import "XXX" statement says look for "XXX" file path. So without "./" it was looking in standard GO tree, then it looks at GOPATH. If there was a "./", it was just looking at the local directory. You may also give absolute paths. Also you don't need to build this directory specially. Just have your main file compile and you are good.
<br />
<br />
One other things that I had to change in the code to make the application work as before.<br />
- In packages, if your function is to be exported, then it should start with upper case. In general, a field or a function or other identifiers within a package that starts with upper case letter are exported.
<br />
<br />
So if you are trying to make the previous code run (after the directory changes as described above), you will get the following<br />
<br />
<b>./GoWebServer.go:15: cannot refer to unexported name ws.runWebServer</b><br />
<b>./GoWebServer.go:15: undefined: ws.runWebServer</b><br />
<div>
<br /></div>
So just change the name of the function to "RunWebServer" & it becomes exported and ready to use.<br />
<br />
That's it for now!Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-90809929005913987372013-12-30T21:36:00.000-08:002013-12-30T21:36:13.918-08:00Writing some Go! codeThis post is my notes when writing some code in Go lang. To start off, I will write a Hello World program in Go language. See below. <br />
<pre class="c" name="code">package main
import "fmt"
func main(){
messageToPrint := "Welcome to Go!"
fmt.Printf(messageToPrint + "\n")
}
</pre>
<br />
Now to run this, you can do one of the following :
<br />
<pre class="powershell" name="code">go run HelloWorld.go
</pre>
OR <br />
<br />
<pre class="powershell" name="code">go build HelloWorld.go
</pre>
In the first case, "go run" compiles the program and runs it. It does not create an executable as such. (It could be creating a temporary executable somewhere.) The second command actually builds an executable which you can run.
Go also has a tool called "gofmt" which you can run directly as gofmt or can run as
<br />
<pre class="powershell" name="code">go fmt HelloWorld.go </pre>
.
The formatting tool formats your source code so that it matches the Go-defined formatting. This is a good thing as each one of us need not invent our own coding conventions.
Gofmt can also be used to perform some code refactoring. In order to perform a rename variable, the following command does the trick.
<br />
<pre class="powershell" name="code">gofmt -l -w -r 'messageToPrint -> mesg' ./HelloWorld.go </pre>
The above command would rename the messageToPrint variable as mesg. In general, you pass 'pattern -> replaceWith' style of commands when using the -r option.
More information on what gofmt -r can be used for is available in this <a href="http://www2.gli.cas.cz/home/cejchan/go/gofmt-r.pdf">presentation</a>
<br />
<div>
<br />
<b>Writing a Web Server in Go!</b><br />
<br />
The following code snippet shows a Web Server that is written in Go! and also shows how to write anonymous functions in Go!<br />
<pre class="c" name="code">package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Printf("Web Server will be started at port 8080\n")
//See below for anonymous function
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path)
})
http.ListenAndServe(":8080", nil)
}
</pre>
<br />
Now a better way (in my opinion) to write the same code as above is shown below.<br />
<pre class="c" name="code">package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Printf("Web Server will be started at port 8080\n")
rootHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path)
})
srv := http.Server{
Handler: rootHandler,
Addr: ":8080",
}
srv.ListenAndServe()
}
</pre>
In this example, we are actually creating an instance of a Http Server as opposed to the previous example where the http global helper methods were used to start the Http Web Service.<br />
<br />
I was just curious to see how well this naive web service performs. Using apache bench, when I run 10000 requests at concurrency level of 100-700, the sample (stupid) program can serve around 5000 requests/second on my age old laptop. Just for kicks, the server in Go is atleast 5 times faster for each request as compared to the nodejs http server.<br />
<br />
Back to Go! ..<br />
<br />
<ol>
<li>Package has to be "main" for your main program. Otherwise, it does not work.</li>
<li>Passing -x flag to your helper commands such as "go run", "go fmt" or "go build" will display the complete output and the commands internally executed.</li>
<li>The "go build" has all compiler optimizations enabled.</li>
</ol>
</div>
Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-2865092387597391632012-11-13T20:22:00.001-08:002012-11-13T20:32:56.752-08:00Hello World! with Scala Web Application using Play 2.0 and Scalate<p>In this post, we will look at how to make a hello-world web application in scala using the Play 2.0 Web Framework and change the template engine to work with Scalate. We assume your machine is all set up with Java and Scala configured. </p> <h3>Step 1 : DOWNLOAD AND CONFIGURE Play 2.0</h3> <p>Download the Play Framework 2.0 from <a title="http://download.playframework.org/releases/play-2.0.4.zip" href="http://download.playframework.org/releases/play-2.0.4.zip">http://download.playframework.org/releases/play-2.0.4.zip</a>. Then extract the zip file to some location. I put mine at C:\Development\tools\play-2.0.4\. <em>It is usually a good idea to leave the version number intact so as to easily know what version of any tool you are currently having on your machine.</em></p> <p>The configuration portion is pretty simple – <strong>Update your PATH environment variable to include the root directory of the play-2.0.4 folder which has the “play.bat” (I am running windows).</strong></p> <p>To verify, launch your command line or powershell and type the command “play”. You should see something like shown below.</p> <p><a href="http://lh6.ggpht.com/-NBtVsyBS4Ek/UKMcbf7BYiI/AAAAAAAABe4/PKzWPxA1NqA/s1600-h/image%25255B3%25255D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-sCHoCGEaCcGBwrM_K8cctTc3RobI_kjJRY46jb-6hRr5ZI-Rus7P5Pk_FPrtJIUErskRwXbPsD6NfTUCZw6syq6ZkTQzWUlTBi4q2ZChSTkyO8sWdR4fhwrXhLq-5sijluAQSQ/?imgmax=800" width="369" height="144"></a></p> <h2></h2> <h3>Step 2 : CREATE A NEW PLAY 2.0 WEB APPLICATION</h3> <p>Using command line (change directory – cd), go to a folder where you wish to place your application. Run the following command</p> <p><strong>play new helloworld-scalate</strong></p> <p>Here helloworld-scalate will be my application name. </p> <p>Then from the command line go into the “helloworld-scalate” folder. And then run the following command.</p> <p><strong>play run</strong></p> <p>The first time you run, it takes a few seconds to initialize the project and upon success, you will see on-screen instructions that the application is hosted at 9000 port on localhost.</p> <p><a href="http://lh4.ggpht.com/-sMrzf9BRk1s/UKMcdH-wbTI/AAAAAAAABfI/8ftKwvYbjs4/s1600-h/image%25255B8%25255D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-grghe8p9c7I/UKMceHoxLoI/AAAAAAAABfQ/eA11NCgG-D0/image_thumb%25255B4%25255D.png?imgmax=800" width="603" height="381"></a></p> <p>Verify by opening your browser and loading <a href="http://localhost:9000">http://localhost:9000</a></p> <p>You should see a welcome page.</p> <h3></h3> <h3>Step 3 : INTEGRATING SCALATE INT YOUR APPLICATION</h3> <p>I found the proper instructions to do this <a href="http://aftnn.org/2012/oct/01/play-20-and-scalate/">here</a>. But I will repeat them again.</p> <p>Go to helloworld-scalate\project\Build.scala</p> <h4>3.a Add the following lines to <strong>appDependencies</strong>. </h4><pre class="scala" name="code">"org.fusesource.scalate" % "scalate-core" % "1.5.3"</pre><br /><p><strong>What did we do? </strong>What we are doing is to add a dependency on scalate-core in an SBT friendly style. Play 2.0 takes care of fetching this dependency through its repositories which is listed at helloworld-scalate\project\plugins.sbt</p><br /><p>To verify if things are fine, go back to command line and run the command </p><br /><p><strong>play run</strong></p><br /><p>You should see that SBT updates the dependencies. Just verify if your application can still be browsed from localhost:9000.</p><br /><h3></h3><br /><h4>3.b Add ScalaIntegration.scala to helloworld-scalate\app\lib</h4><br /><p>You will need to create a folder “lib” inside helloworld-scalate\app folder. And then from the URL given below copy the content into a new file – ScalaIntegration.scala</p><br /><p><a title="https://raw.github.com/janhelwich/Play-2-with-Scala-and-Scalate/master/app/controllers/ScalateIntegration.scala" href="https://raw.github.com/janhelwich/Play-2-with-Scala-and-Scalate/master/app/controllers/ScalateIntegration.scala">https://raw.github.com/janhelwich/Play-2-with-Scala-and-Scalate/master/app/controllers/ScalateIntegration.scala</a></p><br /><p>This does the necessary bootstrapping such that you can now use scalate in the current project.</p><br /><p>Run the “play run” command again and reload the page at localhost:9000 to make sure you haven’t made a mistake.</p><br /><h2></h2><br /><h3>Step 4 : UPDATE VIEWS & ACTION WITH TEMPLATE OF YOUR CHOICE</h3><br /><p>Scalate supports multiple template engines like Jade, Mustache, Scaml. The documentation indicates that Scaml gives the best performance given the strongly typed nature. But I might have misread the docs so <a href="http://scalate.fusesource.org/which.html">read and check yourself</a>.</p><br /><p>To do this, rename your files inside “helloworld-scalate\app\views” folder such that extension is changed from <strong>.scala.html</strong> to <strong>.scaml.</strong> Note that you will see two files – the layout file (main) and the view file (index).</p><br /><p>In the main.scaml, add the following code.</p><pre class="scala" name="code">-@ var body: String<br />-@ var title: String = "Page"<br /><br />%html<br /> %head<br /> %title= title<br /> %body<br /> != body<br /></pre><br /><p>In the index.scaml, add the following code. </p><pre class="scala" name="code">-@ var message: String = "Page"<br />/Layout template file given here<br />-attributes("layout") = "main.scaml"<br /><br />%h1= message<br /></pre><br /><p>Notice that in the index.scaml, we specified that we want to apply the <strong>main.scaml</strong> layout using the <strong>attributes(“layout”) </strong>declaration.</p><br /><p>You also need to change the application controller which is inside the helloworld-scala\app\controllers\application.scala</p><br /><p>The Application controller has an action called “index” which is the one that gets executed when you were loading the browser. We need to change this action such that it renders using Scalate instead of the default one.</p><pre class="scala" name="code">package controllers<br /><br />import play.api._<br />import play.api.mvc._<br /><br />object Application extends Controller {<br /><br /> def index = Action {<br /> //Ok(views.html.index("Your new application is ready."))<br /> Ok(Scalate("index.scaml").render(‘message -> "Hello World"))<br /> }<br />}<br /></pre><br /><p>As you can see in the index action, I commented the existing line added this one line.</p><br /><p><strong>Ok(Scalate("index.scaml").render(‘message-> "Hello World")) </strong></p><br /><p>Please pay attention to the ‘<strong> </strong>in front of the title. Here the <strong><u>‘message</u></strong> is a symbol literal. <a href="http://daily-scala.blogspot.com/2010/01/symbols.html">You can read more about them here.</a> </p><br /><p>Now run the “play run” command again and you should see plain html page that prints “Hello World”.</p><br /><p>I hope this step-by-step helped you get started with a new play 2.0 web application in Scala and also made you familiar with integrating the scalate engine. Note that the <a href="http://aftnn.org/2012/oct/01/play-20-and-scalate/">original url</a><strong> </strong>which I mentioned earlier has the same steps but step 4 where default Scalate template type is specified in the <application folder>\conf\application.conf<strong> </strong>is not performed. I skipped this step and it doesn’t appear that it will cause any problems. But again, after reading this tutorial, if you are a beginner, then you and I are at the same level so please feel free to explore more and learn. </p><br /><p>Thanks!</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-23333088577342844462012-11-05T19:13:00.001-08:002012-11-05T19:17:08.757-08:00Useless experiment with Asynchronous Procedure Calls from C#<p>While Hurricane Sandy hit the East Coast and gave me a full week of vacation without electricity and internet, I gathered enough motivation to read through the very good book – concurrent programming on Windows by Joe Duffy. So here, I came across the Asynchronous Procedure Calls and what they mean. So this post is just a journal about me trying to do something clever (actually stupid) and sharing my madness with others.</p> <p>From what I learnt, an asynchronous procedure call (APC) is just a procedure that you can queue and request the OS that this procedure be performed on a particular thread. But that particular thread may be executing something right? So the question is when will the APC be executed? The answer for that is “it will be executed on the thread when it enters an ‘alertable wait’ state”. So what is an alertable wait?</p> <p>Consider the code below.</p><pre class="csharp" name="code">void IExecuteOnAThread(){<br /> lock(syncLock){<br /> while(conditionIsNotMet)<br /> Monitor.Wait(syncLock);<br /> }<br />}<br /></pre><br /><p>In the above snippet, the thread is actually waiting on the <strong>syncLock</strong>. When some other thread that makes the conditionIsNotMet as false and does a Pulse() on the syncLock, this current thread shall wake up and proceed with its execution. </p><br /><p>Now back to “alertable wait” –> If a thread enters an alertable wait, then it can wake up properly (like in the case above) or can wake up for some other reason. This is how APC gets executed. When an APC gets queued on a thread, the OS will execute this APC the moment it finds that thread in an “alertable wait”.</p><br /><p>So while I think about this concept, it stuck to me (given that I am not that smart) to run a simple experiment. Usually, we queue work items into ThreadPool to get executed. Now the execution can happen in parallel so there is no order guarantee. In some cases, I would like to execute some asynchronous operations “in-order”; as queued; in the background (for whatever reason). In these cases, we typically use some kind of “executor” classes where a queue is almost always involved and some thread/threads wait for the work to be queued and execute them one by one – classic Producer/Consumer style. </p><br /><p>So what I wanted to try was to take advantage of APC and get away from not having to write any code to implement executors. </p><br /><ul><br /><li>I would simply create an “Executor” which has one thread which is always in an alertable wait state (Note that CLR takes care of making sure the Waits are resumed properly in case of any spurious wake-ups) <br /><li>The OS will take care of executing my operations one-by-one.</li></ul><br /><p>Note: For those who cannot wait till the end, do not do this – it is not worth it. I am just doing it because Sandy hit my head pretty hard.</p><pre class="csharp" name="code"> public class StupidExecutor<br /> {<br /> delegate void ApcProc(UIntPtr dwParam);<br /><br /> [DllImport("kernel32.dll")]<br /> static extern uint QueueUserAPC(ApcProc pfnApc, IntPtr hThread, UIntPtr dwData);<br /><br /> [DllImport("kernel32.dll")]<br /> static extern IntPtr GetCurrentThread();<br /><br /> private readonly AutoResetEvent _waiter = new AutoResetEvent(false);<br /> private IntPtr _threadId = IntPtr.Zero;<br /><br /> public StupidExecutor()<br /> {<br /> new Thread(WaitForApc).Start(null);<br /> }<br /><br /> public void Stop()<br /> {<br /> _waiter.WaitOne();<br /> }<br /><br /> private void WaitForApc(object none)<br /> {<br /> _threadId = GetCurrentThread();<br /> _waiter.WaitOne();<br /> }<br /><br /> public void QueueWork(Action action)<br /> {<br /> //while (Thread.VolatileRead(ref _threadId) == IntPtr.Zero)<br /> // Thread.Sleep(0);<br /> var localAction = action;<br /> ApcProc apcProc = ((z) => localAction());<br /> QueueUserAPC(apcProc, _threadId, UIntPtr.Zero);<br /> }<br /> }</pre><br /><p>When the StupidExecutor gets created, a thread is launched which when started waits for a signal on a Auto Reset Event. This will make the thread wait because it is non-signalled and will remain so until Stop is executed on the executor. The executor has one method “QueueWork” which takes a delegate.</p><br /><p>So the usage of the stupid executor is as shown:</p><pre class="csharp" name="code">static void Main(string[] args)<br /> {<br /> var executor = new StupidExecutor();<br /> double[] avg = {0.0};<br /> int size = 1000;<br /> for (int j = 0; j < size; j++)<br /> {<br /> var queued = DateTime.UtcNow;<br /> executor.QueueWork(() => avg[0] += (DateTime.UtcNow - queued).TotalMilliseconds);<br /> }<br /> Console.ReadLine();<br /> Console.WriteLine("Average Latency : "+avg[0]/size);<br /> }<br /></pre><br /><p>So the intention was to measure the latency (again not really the most efficient way). And I see an average latency of around 2 ms. So this must be a great way! Not really! First of all, for a <a href="http://www.bluebytesoftware.com/blog/2006/05/04/UsermodeAPCsAndManagedCode.aspx">lot of reasons like state corruption among other things</a> this should not be done. Moreover, you are passing managed delegates to native code. So GC can always kick in. So to prove it, I am changing the sample size from 1000 to 100000.</p><br /><p>Firstly, it wouldn’t work and you will be greeted with a nice dialog.</p><br /><p><a href="http://lh4.ggpht.com/-15Xbi2Ct0Hg/UJiAQsJJseI/AAAAAAAABeA/MhNxGx0dOmg/s1600-h/image%25255B7%25255D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-6vrQN11_V8I/UJiARJpXtGI/AAAAAAAABeI/OIdKDCXsqEg/image_thumb%25255B3%25255D.png?imgmax=800" width="344" height="205"></a></p><br /><p>So run it in debug mode and you will get a nice little MDA called “CallbackOnCollectedDelegate" shall be raised. This simply says that the delegate you passed to native code is GC’d already. </p><br /><p><a href="http://lh5.ggpht.com/-XKd7PqYRRFU/UJiARiXmKpI/AAAAAAAABeQ/IJD3nDmuAK4/s1600-h/image%25255B11%25255D.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih1XQqajtuQ6BUzkl90YhzfQ6OvFoQ8RFXbYj0yzOezm-f5Cw1CIHFcIsGpnBkLpfHuX7PDrPZFGNSIjlD57ITHxAIsy_JySPkZk1tFWsGXBeD4z6JoebJiyj13QpmE8pq2I-mNA/?imgmax=800" width="344" height="154"></a></p><br /><p>Or even this:</p><br /><p><a href="http://lh4.ggpht.com/-zu5wHmzhKrA/UJiASk7JXcI/AAAAAAAABeg/wSnfqah7VhA/s1600-h/image%25255B15%25255D.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-7fMASb9OyOA/UJiATS5yXII/AAAAAAAABeo/-hoNLcpfNKE/image_thumb%25255B6%25255D.png?imgmax=800" width="350" height="177"></a></p><br /><p>So the experiment which was doomed to fail did fail. Long story short – don’t mess with your threads.</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-62168184444991716002012-10-28T20:12:00.003-07:002012-10-28T20:12:54.012-07:00A 5 min surveyThose who are unfortunate to see this post, can you please take <a href="http://www.surveymonkey.com/s/5ZMQ9NZ">this quick survey</a> (7 questions)
Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-3751776390849539582011-12-12T18:20:00.001-08:002011-12-12T18:20:58.472-08:00Real World Example : Simple Publish/Subscribe Pattern with Reactive Extensions<p>Shown below is a requirement. <br><script src="https://gist.github.com/1470033.js?file=EventThrottlingTest.cs"></script><br>Let me explain a little about what I want to achieve. I have an entity called “Frequent” which implements INotifyPropertyChanged interface. So I can subscribe to the change notification and then upon firing the event, I simply increment a counter. Now, my requirement is that the PropertyChanged can fire way too quickly as simulated in the for-loop. So in UI applications, it doesn’t make sense for me to fire property changed events that frequently as in a real world intensive WPF application, this can become your biggest bottleneck (among several others things). </p> <p>So my requirement is that, within 1 second, I would like to receive only 1 change notification per property, if it has changed indeed. The simple implementation for this would be to do something like pushing the properties that have changed into a queue and process the queue once a second and then raise change notification event. This can be done very easily but then it can be a little tedious job for such a requirement to restrict an event from firing more than X times a second. </p> <p>So I was looking at the Reactive Extensions (Rx Framework) and it occurred to me that it should support Observer pattern out of the box. So I started playing with it but hit a road block immediately. There are numerous resources that shows how to generate observables from Timers, Time Intervals, Enumerables, etc but what I want to is to take advantage of extension methods supported by Rx Framework on IObserver such as Throttle(), Buffer(), Window(), etc on a simple Pub-Sub system. May be I did not look hard enough but I could not find a simple example. So I thought it would be helpful for rest of the people like me if I made a blog post.</p> <p>In my implementation, the Frequent object is by itself a Publisher and a Subscriber. In Rx terms – IObservable and IObserver. Instead of implementing these interfaces, I want to have some observable of strings where I will publish a property that has changed and the subscriber on the observable would receive it. So for that purpose of simple message passing between Observers and Observables, you can use a Subject<T>. There are other variants like ReplaySubject<T>, BehaviorSubject<T>, AsyncSubject<T>, etc. but that is out of scope of this post. I publish on Subject<T> and then the subscribers who subscribed earlier to it would be notified of these messages.</p> <p>Shown below is a the Frequent class implementation.</p> <p><script src="https://gist.github.com/1470125.js?file=Frequent.cs"></script></p> <p>Take a minute or two to go through the simple class, it is self explanatory. All I am doing is that when a property changes, instead of firing it immediately, I simply notify my throttler about the property that has changed. The throttler’s responsibility is to determine when to notify the subscribers about this change. Shown below is the implementation.</p> <p><script src="https://gist.github.com/1470141.js?file=StringMessageThrottler.cs"></script></p> <p>You can read my comments that I wrote out of frustrations. All I do in this simple façade is to wrap an Observable –> Subject<string> and buffer the messages received for 1/2 second and then get unique messages from the buffer and publish it to the subscribers. This is all done in the constructor. The other methods are simply my take on making things simple for the consumer of this class.</p> <p>Rx is complex and powerful but in my opinion it has a very steep learning curve. But it surely did save a good few days of implementation for my team. Again, I am very new to Rx, so if you have some better ways to do it, then please let me know.</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-13913397071076450382011-12-09T19:51:00.001-08:002011-12-09T19:51:24.592-08:00Hannspad Hannspree SN10T2 – Rooting, Android Market and Some Crazy stuff<p>Recently the Android Tablet bug bit me which led me to buy Hannspad Hannspree SN10T2 from AliExpress.com. The order was made on the day of Thanksgiving and I received the tablet within two weeks. The whole goal was to get a cheap tablet on which I can try installing custom ROM, may be try and develop some applications. But turns out it is not that simple. See the Slatedriod.com forums has custom ROM but for SN10T1 model but not SN10T2 model – which runs on two different processors. So all the cool stuff I read about that people do with SN10T1 is not applicable for SN10T2 – example being booting the tablet in recovery mode. There are not hard volume buttons that lets me boot into recovery console. I tried all different combinations instead of missing Volume + button but the recovery key sequence remains unknown.</p> <h2></h2> <h2>Connecting the Hannspad Tablet to Windows 7 x64</h2> <p>Hooking up the USB cable to the Windows 7 machine doesn’t really install the drivers. You can still be able to copy files around once you enable the USB File Transfer on the tablet. If you are new like me, just drag the notifications bar on top of the tablet and the instructions then are clear enough for you to know what to do. So first time you hook up the tablet, you get the message saying OMAP-3/4 driver is not found. Don’t even fight it, there is a really simple way to install the driver for the tablet.</p> <ol> <li>Download Super One Click from Shortfuse.org. The direct link to what I am using is <a href="http://www.multiupload.com/VGCN3ZTFM8" target="_blank">here.</a> It is version 2.3.1.</li> <li>Extract the zip file to some place you have your portable applications. I put it in temp but now I have a problem deleting the folder because driver was installed from this location??</li> <li>Launch SuperOneClick.exe and then click on Advanced Tab.</li> <li>Click on Check Driver.</li> <li>Then say OK for installation of the driver.</li> <li>Accept Windows warning about unsigned driver and move on.</li></ol> <p>What this driver lets you do is to let ADB (Android Debug Bridge or visualize like a remote shell access for your tablet unix system) identify that this device is hooked up and running.</p> <h3></h3> <h3></h3> <h3></h3> <h2>Rooting the SN10T2 (may work for other tablets too!)</h2> <p>If you search for information on how to root the SN10T2, the first thing you see is the article by <a href="http://tabletrepublic.com/forum/hannspad/hannspad-sn10t2-easy-rooting-google-apps-guide-143.html" target="_blank">Sir Shagsalot</a> (<img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://lh6.ggpht.com/-FCWo0OcWuXg/TuLXM444_NI/AAAAAAAABcs/RhwNRvmp0GY/wlEmoticon-winkingsmile%25255B2%25255D.png?imgmax=800">). It has precisely what you need to do in order to enable Super User access for the programs running on your Android Tablet. Since the information is not so eye-soothing, I will try to replicate the information. But be aware </p> <ol> <li>The information here is from the article I mentioned above plus my experience struggling with the Tablet.</li> <li>I am not an Android Expert – I am just a software developer who writes software for a living mostly on Windows Platform.</li> <li>Don’t blame me for any physical or mental or financial pain that may cause as a result of following this information.</li> <li>Rooting implies the warranty is void. You cannot bitch about your tablet to your vendor anymore. Not my problem again <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://lh5.ggpht.com/-B3_Y66zUzlE/TuLXNMkqz9I/AAAAAAAABc0/46RWP-R0B5o/wlEmoticon-smile%25255B2%25255D.png?imgmax=800"></li></ol> <h3></h3> <h3></h3> <h3>Option 1 : Using the same SuperOneClick</h3> <p>If you followed the previous steps, you can use SuperOneClick to Root your device. </p> <h3>Option 2 : Using <a href="http://forum.xda-developers.com/showthread.php?t=833953" target="_blank">z4Root</a> </h3> <ol> <li>Download the z4root.apk and place it in your SD Card on the tablet.</li> <li>On the tablet, go to Menu->Settings->Application Settings and check the option for Unknown Sources.</li> <li>Now go to Home->Applications->ES File Explorer.</li> <li>Navigate to the z4root.apk wherever you copied it.</li> <li>Install the APK file.</li> <li>Then open the z4root application.</li> <li>You will see two options –> Temporary Root or Permanent Root. If your application is already rooted, you will see the option to Unroot.</li> <li>I selected Permanent Root.</li> <li>Then wait until you see “Rebooting..”. If the tablet doesn’t reboot in say 10 minutes, I would not bother to wait. Just restart the tablet manually. It is fine!</li></ol> <h3></h3> <h3>The Android SDK Tools</h3> <p>Basically, rooting gives you Super user access and lets you access the shell from ADB console. The super one click application distributes the ADB console application if you poke inside the installation folder. </p> <p>Anyway for you to officially get the ADB application, you can download the Android SDK from <a href="http://developer.android.com/sdk/index.html" target="_blank">here.</a> </p> <ol> <li>Download the <a href="http://developer.android.com/sdk/index.html" target="_blank">Android SDK.</a></li> <li>Download the <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">Java Development Kit</a>, if you don’t have it already. You need JDK not JRE.</li> <li>Install JDK. Install Android SDK.</li> <li>Now in the Android folder (C:\Program Files(x86)\Android\android_sdk\), launch the SDK Manager application. See image below.</li> <li>You need to check “Android SDK Tools” and then click “Install Packages”.</li> <li>Once done, you will find a folder called “platform-tools”.</li></ol> <p><a href="http://lh4.ggpht.com/-juGMnplne4M/TuLXNYHUJ8I/AAAAAAAABc8/O7t83ulPnLw/s1600-h/image%25255B3%25255D.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-8YQOmn5pJFQ/TuLXNr8DPBI/AAAAAAAABdE/uFnJXm6yX94/image_thumb%25255B1%25255D.png?imgmax=800" width="352" height="253"></a></p> <h3></h3> <h3>Some fun with ADB.</h3> <ul> <li>Open command prompt and assuming platform-tools is in your PATH. <strong> <h3><strong>(Of course, the table is connected to the machine, USB Debugging enabled on the tablet).</strong></h3></strong></li> <li>Listing all the devices – <strong>adb devices</strong></li> <li>Opening the Unix Shell for your tablet – <strong>adb shell</strong></li> <li>Copying files from your PC to the device</li> <ul> <li>For SD Card, you don’t need to mount as it is already mounted for “Read-Write”. Just run the command - </li> <ul> <li><strong>adb push c:\myfiles\superdocument.pdf /sdcard/</strong></li></ul> <li>For copying to /system/app folder, which you may have to do for manual installation of Google Apps and Market</li> <ul> <li>Go to the shell of tablet using “adb shell”. Now inside the shell,</li> <li>Mount the /system using the command</li> <ul> <li><strong>mount -o remount,rw /dev/block/mmcblk0p5 /system</strong></li></ul> <li>Copy the files you want to push to /system/app, using</li> <ul> <li><strong>push c:\files\superapp.apk /system/app/</strong></li></ul> <li>Unmount the /system using the command</li> <ul> <li><strong>mount -o remount,ro /dev/block/mmcblk0p5 /system</strong></li></ul> <li>Make sure you unmount once you are done.</li></ul></ul> <li>Reboot the tablet from Windows – <strong>adb reboot</strong></li> <li>Copying files from your tablet to the PC – <strong>adb pull \sdcard\super.pdf c:\temp\goodbook.pdf</strong></li></ul> <h3></h3> <h3>Installing the Android Market on the SN10T2</h3> <p>You can follow the original instructions from top to bottom <a href="http://tabletrepublic.com/forum/hannspad/hannspad-sn10t2-easy-rooting-google-apps-guide-143.html" target="_blank">that Sir ShagsALot wrote here.</a> I will just use the ADB to install the Android Market application.</p> <ul> <li>Root your tablet either using SuperOneClick or Z4Root as described previously. Reboot your tablet.</li> <li>Download the required Google Apps distribution from the link mentioned in the article.</li> <li>Extract them to some place on your machine.</li> <li>Make sure ADB is in your PATH. Open the Command Prompt and navigate to the folder where you extracted the googleapps.rar archive.</li> <li>You should see something like shown below.</li></ul> <p align="center"><a href="http://lh6.ggpht.com/--K5PbEsjGPE/TuLXNyxwHjI/AAAAAAAABdM/OvNG4Unppdk/s1600-h/image%25255B7%25255D.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-n8nlnzr5QDw/TuLXOQTTgUI/AAAAAAAABdU/v1sQYmVsMgg/image_thumb%25255B3%25255D.png?imgmax=800" width="442" height="150"></a></p> <ul> <li>Navigate the command prompt into the “app” folder.</li> <li>Using the ADB shell, first mount the /system on your tablet. Then run the following commands to push APK files to /system/root/</li> <ul> <li><strong>c:\temp\Working Google Apps\app> adb push GoogleServicesFramework.apk /system/app</strong></li> <li><strong>c:\temp\Working Google Apps\app> adb push OneTimeInitializer.apk /system/app</strong></li> <li><strong>c:\temp\Working Google Apps\app> adb push SetupWizard.apk /system/app</strong></li> <li><strong>c:\temp\Working Google Apps\app> adb push com.android.vending.apk /system/app/Vending.apk </strong></li> <li><em>Notice the Application for Market is renamed to Vending.apk (don’t think you need to do this, but everyone else uses Vending.apk) <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://lh5.ggpht.com/-B3_Y66zUzlE/TuLXNMkqz9I/AAAAAAAABc0/46RWP-R0B5o/wlEmoticon-smile%25255B2%25255D.png?imgmax=800"></em></li> <li><em>Shown below is the command prompt snapshot for your reference</em></li></ul></ul> <p><a href="http://lh3.ggpht.com/-3ksg9WUaeKg/TuLXOjX6YqI/AAAAAAAABdc/XXfFRrvnt98/s1600-h/image%25255B17%25255D.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjoI1g8BDsD6RIPa8o8i0WzuNnp5J7ApX_GJeeEu9XzW1z9TeRh1qx9sDAaPKT1PX9HuMpgmenYl4a66vrbVD4YEFLLeC6uIrAJrEne2-Z8rfHCZB4RYmDkJcTf6YnEtFrwLZQMg/?imgmax=800" width="649" height="376"></a></p> <ul> <li>From the above you should have understand by now that I am only interested in making the Android Market work. </li> <li><strong>Don’t forget to un-mount the /system and also reboot the device as soon as you are done copying.</strong></li> <li>Once the tablet boots, you will be seeing a welcome screen.</li> <li>Just go through the welcome screen and add your google account.</li> <li>Now go to Market and make sure it runs.</li></ul> <h4></h4> <h3>Some issues that I had </h3> <ul> <li>ES File Explorer –> Menu –> Settings –> Root Options. Check the “Root Explorer” failed with message <strong>"sorry, test failed. This feature cannot run on your phone” </strong>message. So what is the alternative ? Use ADB, it might sound intimidating but it is just a simple Shell prompt. If you are messing around your tablet, most likely you are comfortable with the command prompt.</li> <li>There is a great tool called “File Expert”. It is one tool that impressed me a lot! It is similar to ES File Explorer but much better looking and much easier and intuitive to use. You can upload files from your PC without having to connect your tablet.</li> <ul> <li>Install File Expert. You can get it from the App Center that ships with the SN10T2 as well.</li> <li>Launch File Expert. Go to “Share my contents”.</li> <li>Just touch on the “Share via Web” option. Viola! </li> <li>Now you should see a brief message explaining how you can access your tablet SD Card from the Web Browser.</li></ul> <li>Uploading multiple files from File Expert Web UI. You cannot upload more than one file when you are in the root of the SD Card. But create one folder and then you would be able to upload multiple files simultaneously. </li> <li>MOUNT Options in File Expert does not work and can make you believe that it worked. So what is the alternative?? Use ADB.</li> <li>If you manually move some files to /system/app in File Expert of ES File Manager, you may believe it was successfully copied, but they aren’t really done. You don’t see an error that /system has to be mounted. </li> <li>If you are messing with different versions of Google Apps, the one in the link by Sir ShagsALot is the only one I found to be working. If you mess up, then remove the APK files from the /system/app manually. </li> <li>I copied bad APK files and was stuck with Emergency dial screen. So what I realized later was that SetupWizard was badly renamed. So I removed the APK files and copied them keeping the name intact.</li> <li>Again, if you are stuck with wizard screen and reboot doesn’t help, use ADB to delete the APK file.</li> <li>Android Market app is installed but launches and dies immediately. You need to make sure GoogleServicesFramework.apk is properly installed/copied into /system/app folder. When in doubt, repeat the process after taking some break.</li> <li>Android Market App launches but cannot download application. The application page seems to be stuck at “Starting download…”. This only happened when I installed incompatible version of Android App Market.</li></ul> <p>So these are all the things that I struggled with this week and I hope anyone like me who is new to Android Rooting may find this information helpful. By the way, don’t forget to check out <a href="http://www.slatedroid.com/" target="_blank">SlateDroid</a>. They are awesome!</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-12071773782769288762011-01-16T15:14:00.001-08:002011-01-16T15:14:16.216-08:00WPF Events to Command redirection using System.Windows.Interactivity<p><a href="http://krishnabhargav.blogspot.com/2011/01/executing-routedcommand-in-code-behind.html" target="_blank">As mentioned previously</a>, I recently used System.Windows.Interactivity library to make a command respond to an event on WPF controls without using any code-behind. In this post, I would give a brief overview and show some code on how to do it. I will try and keep the post to the point and not write anything about hooking up events with code or anything like that. Usual disclaimer applies – I am not entirely familiar with the internals but I know how to make it work and why it works.</p> <p>So lets start with my simple requirement. I have a text box and as I enter I want to fire a command which processes the text and displays it on a textblock. Of course you can hook up both the controls to the same property in the ViewModel and with .NET 4.0 you can be sure that the getter will fire again when NotifyPropertyChanged is fired. But that is not the point here. </p> <p>My XAML would simply have a textbox and a textblock. On textbox.TextChanged event fired, I would like to execute a command in my view model. The XAML is shown below.</p><pre class="xml" name="code"><UserControl x:Class="Buddi.Training.Advanced.Interactivity.EventToCommandDemo"<br /> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br /> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br /> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <br /> xmlns:d="http://schemas.microsoft.com/expression/blend/2008" <br /> xmlns:local="clr-namespace:Buddi.Training.Infra"<br /> xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"><br /> <Grid Background="Beige"><br /> <Grid.RowDefinitions><br /> <RowDefinition Height="100"/><br /> <RowDefinition Height="100"/><br /> </Grid.RowDefinitions><br /> <TextBox Text="{Binding SampleCommandParam,UpdateSourceTrigger=PropertyChanged}" Margin="20"><br /> <i:Interaction.Triggers><br /> <i:EventTrigger EventName="TextChanged"><br /> <local:EventToCommand Command="{Binding SampleCommand}"<br /> CommandParameter="{Binding SampleCommandParam}"/><br /> </i:EventTrigger><br /> </i:Interaction.Triggers><br /> </TextBox><br /> <TextBlock Text="{Binding Message}" Grid.Row="1" Padding="30" FontFamily="Consolas" FontWeight="14"/><br /> </Grid><br /></UserControl><br /></pre><br /><br /><p> You need to see how I am hooking up the event to the command in viewmodel which is the DataContext of the View (UserControl to be precise). Lets disect what we have here - We add an event trigger to the TriggersCollection on the Grid using the Interactivity.Triggers attached properties. An event trigger comes with the System.Windows.Interactivity.dll assembly. So add a reference to that library using the "Add Reference" dialog. The Event Trigger then expects an action that can be anything that derives from the <a href="http://msdn.microsoft.com/en-us/library/ff726548(v=Expression.40).aspx" target="_blank">TriggerAction<FrameworkElement></a> class. The TriggerAction<T> derived class should implement one method called "InvokeCommand(object parameter)". The implementation simply takes care of executing the command which are passed to the DependencyProperty we defined in the EventToCommand class. Note that TriggerAction<FrameworkElement> is a DependencyObject, thereby it allows you define Dependency Properties to take full advantage of the Binding, Styles, Animations and what not. So the trigger action is simple - (the following is a special implementation where I handle RoutedCommand different than the others, this is just my scenario and is typically bad - you are programming to the implementation which is not a good idea, but the plan here is to show how you can use the base.AssociatedObject).<br /><br /><pre name="code" class="csharp"><br />namespace Buddi.Training.Infra<br />{<br /> public class EventToCommand : TriggerAction<FrameworkElement><br /> {<br /> public ICommand Command<br /> {<br /> get { return (ICommand)GetValue(CommandProperty); }<br /> set { SetValue(CommandProperty, value); }<br /> }<br /><br /> public static readonly DependencyProperty CommandProperty =<br /> DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new UIPropertyMetadata(null));<br /><br /><br /><br /><br /> public object CommandParameter<br /> {<br /> get { return (object)GetValue(CommandParameterProperty); }<br /> set { SetValue(CommandParameterProperty, value); }<br /> }<br /><br /> // Using a DependencyProperty as the backing store for CommandParameter. This enables animation, styling, binding, etc...<br /> public static readonly DependencyProperty CommandParameterProperty =<br /> DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventToCommand), new UIPropertyMetadata(null));<br /><br /><br /><br /> protected override void Invoke(object parameter)<br /> {<br /> if (Command == null) return;<br /> if (Command is RoutedCommand)<br /> {<br /> var rc = Command as RoutedCommand;<br /> if (rc.CanExecute(CommandParameter, base.AssociatedObject))<br /> {<br /> rc.Execute(CommandParameter, base.AssociatedObject);<br /> }<br /> }<br /> else<br /> {<br /> if (Command.CanExecute(CommandParameter))<br /> Command.Execute(CommandParameter);<br /> }<br /> }<br /> }<br />}<br /></pre><br /><br /><p>That's it! you can now program to the events using the commands that you already have. This lets you keep your code-behind clean and write more testable code than ever. I hope this is useful inspite of it not being the best of the articles. By the way, almost every MVVM framework out there provides an implementation of Event To Command action -eg : Caliburn, Cinch, you name it ... but not always it is possible for us to use a third party framework just for this one reason. In such cases, I thought it is good to know that you can acheive it just by using Microsoft's assembly.</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com5tag:blogger.com,1999:blog-19967453.post-54228095607411557912011-01-15T20:57:00.001-08:002011-12-12T21:08:12.181-08:00Working (around) with MSpecsI just started playing with <a href="https://github.com/machine/machine.specifications" target="_blank">Machine.Specifications</a> (MSpecs). Overall I am very satisfied with the project but I did ran across some issues - dont get me wrong, it works great but it was more like limitations because I was lame and using Express edition.<br />
My specs are simple - I am trying to create a Dependency Injection Container that does absolutely very little, not as powerful as Unity or any other containers. I dont plan to use it anywhere - just am trying to create one for fun. So the specifications are simple (for now).<br />
<pre class="xml" name="code">
Mapping Interface to Type, MappingInterfaceToType
» should allow creating instance of type registered
In case of multiple constructors, pick the one with injection attribute, MultipleConstructorScenario
» should create instance whose Value property is 20
Container should support singleton instances, SingletonScenario
» should return the same instance for every invocation
Inner container should be supported, SupportForInnerContainer
» should return registration from parent and itself</pre>
<br />
<br />
Simple right? Those are the output for my specifications. It it still under works but the output itself explains a lot - thats the power of the MSpecs. Business Analysts write requirements, gives specifications - This can be changed - they give us the requirements and we "program" the specifications. I know it appears to be some rewired Unit Testing, but if unit tests can give me a bunch of specifications and tell me what failed, I would be more than happy to use that. <br />
<br />
<br />
Now lets write a specification from scratch. Look at the following simple C# code. <br />
<pre class="csharp" name="code"> public class Singleton { }
public class Transient
{
public Singleton SingletonInstance { get; private set; }
public Transient(Singleton ins)
{
SingletonInstance = ins;
}
}</pre>
<br />
I would like to register the Singleton class as - singleton and the transient class as - transient. So my specification is, <strong>instances of transient whose dependency is a singleton should get the same instance. </strong>So that becomes my "Subject" <br />
<br />
<br />
<pre class="csharp" name="code">[Subject("instances of transient whose dependency is a singleton should get the same instance.")]
public class InjectionOfSingletonForTransientResolution
{
}</pre>
<br />
Now to verify that specification, I need to <strong>"establish a context"</strong> which is that I need my container to be ready for use.<br />
<pre class="csharp" name="code"> Establish context = () =>
{
_builder = new TypeBuilder();
};</pre>
<br />
Now that context is established, I will just write my specification. I create two instances and both my instances should have the same instance of Singleton. <br />
<pre class="csharp" name="code">
It Should_Use_The_Same_Instance_when_creating_dependent_Components = () =>
{
var instance1 = _builder.Resolve<transient>();
var instance2 = _builder.Resolve<transient>();
instance1.ShouldNotEqual(instance2);
instance1.SingletonInstance.ShouldNotBeNull();
instance2.SingletonInstance.ShouldNotBeNull();
instance1.SingletonInstance.ShouldEqual(instance2.SingletonInstance);
};
</pre>
<br />
But you know this would not happen just like that. I have my context and I know what it should do. I need to tell it why it should do that - that specification would pass <strong>"because I am registering dependency as singleton and instance as transient".</strong> So I add my reasons on why (or when) the specification would pass.<br />
<pre class="csharp" name="code">
Because I_Am_Making_A_Dependency_Registration_As_Singleton_And_TestSubject_As_Transient = () =>
{
_builder.Register<singleton>(new Singleton());
_builder.Register<transient>();
};
</pre>
<br />
The whole specification would look like shown below. <br />
<pre class="csharp" name="code">[Subject("instances of transient whose dependency is a singleton should get the same instance.")]
public class InjectionOfSingletonForTransientResolution
{
static ITypeBuilder _builder;
public class Singleton { }
public class Transient
{
public Singleton SingletonInstance { get; private set; }
public Transient(Singleton ins)
{
SingletonInstance = ins;
}
}
Establish context = () =>
{
_builder = new TypeBuilder();
};
Because I_Am_Making_A_Dependency_Registration_As_Singleton_And_TestSubject_As_Transient =
() =>
{
_builder.Register<singleton>(new Singleton());
_builder.Register<transient>();
};
It Should_Use_The_Same_Instance_when_creating_dependent_Components = () =>
{
var instance1 = _builder.Resolve<transient>();
var instance2 = _builder.Resolve<transient>();
instance1.ShouldNotEqual(instance2);
instance1.SingletonInstance.ShouldNotBeNull();
instance2.SingletonInstance.ShouldNotBeNull();
instance1.SingletonInstance.ShouldEqual(instance2.SingletonInstance);
};
}
</pre>
<br />
When I first run this my specification failed because my container does not handle that yet. <br />
<pre class="xml" name="code"> instances of transient whose dependency is a singleton should get the same instance., InjectionOfSingletonForTransientResolution
» Should Use The Same Instance when creating dependent Components (FAIL)
Machine.Specifications.SpecificationException: Should be [not null] but is [null]
at Machine.Specifications.ShouldExtensionMethods.ShouldNotBeNull(Object anObject) in d:\BuildAgent-03\work\38fe83de684fd902\Source\Machine.Specifications\ExtensionMethods.cs:line 181
at Analytics.Specifications.Container.InjectionOfSingletonForTransientResolution.<.ctor>b__2() in C:\Users\bhargav\documents\visual studio 2010\Projects\Analytics\Analytics.Specifications\Container\TypeBuilderSpecs.cs:line 159
at Machine.Specifications.Model.Specification.InvokeSpecificationField() in d:\BuildAgent-03\work\38fe83de684fd902\Source\Machine.Specifications\Model\Specification.cs:line 75
at Machine.Specifications.Model.Specification.Verify() in d:\BuildAgent-03\work\38fe83de684fd902\Source\Machine.Specifications\Model\Specification.cs:line 53</pre>
<br />
I fix my container and now I see my specification pass.<br />
<br />
One thing to remember is what ever you are working off - your context - it should be static - otherwise the compilation would fail. when looking at others examples, I had a similar question - so here I am telling you upfront.<br />
<br />
<h3>
Running specifications without leaving Visual Studio - No test runners are required.</h3>
<br />
All my specifications are in a separate class library. I would like to run the specifications without leaving Visual Studio (btw, I am using Express). I remember once upon a time I could make a class library as a startup project and somehow linked NUnit gui runner with the project. I could not get it done with VC# 2010 Express anymore. May be I am missing something. Anyway the solution was to add the following Post-Build event<br />
<br />
<a href="http://lh3.ggpht.com/_Oy9HuksgrdU/TTJ6ruuy-ZI/AAAAAAAABbY/4OD3cJB2Sns/s1600-h/image%5B3%5D.png"><img alt="image" border="0" height="277" src="http://lh5.ggpht.com/_Oy9HuksgrdU/TTJ6sZXdyjI/AAAAAAAABbc/PhFKCTD1BB0/image_thumb%5B1%5D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="644" /></a><br />
<br />
Once my specifications project it built successfully, it automatically generates a nice output. You can use all command line arguments that mspec supports, for now I only care if they pass or fail, hence the simple one.<br />
<br />
<h3>
Debugging my specifications. It was painful</h3>
<br />
I tried all different strategies like using System.Diagnostics.Debugger.Break() and what not. ConsoleRunner that comes with mspec (mspec.exe) was crashing complaining that it encountered an user defined breakpoint in the code - yeah that was my intention. Anyway to work around that, I create a console program and in the console application, I added reference to my specifications library and from the Git Hub source code for the mspec.exe (thanks to OSS) the following C# code helped me overcome my limitation of not being able to use R# or TestDriven.Net.<br />
<pre class="csharp" name="code"> class ContainerSpecsRunner
{
static void Main(string[] args)
{
//Console.WriteLine(typeof(ITest).Assembly.Location);
Program prog = new Program(new DefaultConsole());
prog.Run(new[] { typeof(ITest).Assembly.Location });
}
}</pre>
<br />
ITest is a type that was defined in my specifications assembly. That way I need not worry about any arguments or hardcode path of my assembly. By the way, at work you might be killed for doing this, I am just doing it at my personal projects.<br />
<br />
<h3>
Some samples? </h3>
<br />
Ok the biggest problem I had was that I could not find some real world examples, the GIT HUB structure of the project was confusing as hell. I was hoping to find some examples, but I could not. As much as I like the project, I hate to see so little or almost non-existent guidance for new users. So here are some of my specifications. These are written by me - I am just learning the style of specs so forgive me if they are not what you wanted them to be. I am just trying to help. <br />
<pre class="csharp" name="code">public interface ITest{
}
public class Test: ITest{
[Injector]
public Test()
{
}
}
[Subject("Mapping Interface to Type")]
public class MappingInterfaceToType
{
static ITypeBuilder _builder;
Establish context = () =>
{
_builder = new TypeBuilder();
};
Because of = () =>
{
_builder.Register<itest ,="" test="">();
};
It should_allow_creating_instance_of_type_registered = () =>
{
var resolvedObject = _builder.Resolve<itest>();
resolvedObject.ShouldNotBeNull();
typeof(Test).ShouldEqual(resolvedObject.GetType());
};
}
[Subject("In case of multiple constructors, pick the one with injection attribute")]
public class MultipleConstructorScenario
{
public class Test2 : ITest
{
public int Value { get; private set; }
public Test2(ITest demo)
{
Value = 10;
}
[Injector]
public Test2()
{
Value = 20;
}
}
static ITypeBuilder _builder;
Establish context = () => {
_builder = new TypeBuilder();
};
Because of = () =>
{
_builder.Register<ITest,Test2>();
};
It should_create_instance_whose_Value_property_is_20 = ()=>
{
var instance = _builder.Resolve<ITest>();
instance.ShouldBeOfType<Test2>();
(instance as Test2).Value.ShouldEqual(20);
};
}
[Subject("Container should support singleton instances")]
public class SingletonScenario
{
static ITypeBuilder _builder;
Establish context = () =>
{
_builder = new TypeBuilder();
};
Because instance_Is_Registered = () =>
{
_builder.Register<ITest>(()=>new Test());
};
It should_return_the_same_instance_for_every_invocation = () =>
{
_builder.Resolve<ITest>().ShouldEqual(_builder.Resolve<ITest>());
};
}
[Subject("Inner container should be supported")]
public class SupportForInnerContainer
{
static ITypeBuilder _builder;
static ITypeBuilder _child;
Establish context = () =>
{
_builder = new TypeBuilder();
_child = _builder.CreateChildBuilder();
};
Because instance_is_registered_with_child_container = () =>
{
_builder.Register<IEnumerable>(new int[] { 0, 1, 2 });
_child.Register<ITest,Test2>();
};
It should_return_registration_from_parent_and_itself = () =>
{
var arr = (int[])_child.Resolve<IEnumerable>();
arr.SequenceEqual(new[] { 0, 1, 2 }).ShouldBeTrue();
_child.Resolve<ITest>().ShouldBeOfType<Test>();
};
}
</pre>
<br />
<br />
<h3>
Would be nice if it can print my "Because" field names</h3>
<br />
Just like the console runner prints my "It" fields, it would be nicer if my reasons are printed. I will see if i can do that myself to the project and may contribute a little.<br />
<br />
<br />
I hope the examples are useful. By no means they are perfect but they can get you started. You can see in the example I detailed in the beginning, I had a whole bunch of Should statements = that is plain wrong. Each specification should define one thing - otherwise it would be a big mess. Please look at this great project and I really am in love with MSpec.Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com3tag:blogger.com,1999:blog-19967453.post-19938205276667615562011-01-13T19:39:00.001-08:002011-01-13T19:42:18.265-08:00Executing RoutedCommand in Code-Behind<p>Recently I had this issue where we had a bunch of routed commands but those which were to be bound to an event instead of directly on a Button.Command. Anyway with all the event to command redirection (hint: use System.Windows.Interactivity EventTriggers and TriggerAction<FrameworkElement>), the requirement boiled down to executing a RoutedCommand in code behind.</p> <p>Consider the command had a base class which looks as shown below.</p><pre class="csharp" name="code">public abstract class CommandBase<br /> {<br /> public ICommand Command { get; private set; }<br /> public CommandBinding CommandBinding { get; private set; }<br /> public CommandBase()<br /> {<br /> Command = new RoutedCommand();<br /> CommandBinding = new CommandBinding(Command, HandlerExecute, HandlerCanExecute);<br /> }<br /><br /> private void HandlerExecute(object sender, ExecutedRoutedEventArgs args)<br /> {<br /> Execute(args.Parameter);<br /> }<br /><br /> private void HandlerCanExecute(object sender, CanExecuteRoutedEventArgs args)<br /> {<br /> args.CanExecute = CanExecute(args.Parameter);<br /> }<br /><br /> protected abstract void Execute(object parameter);<br /><br /> protected abstract bool CanExecute(object parameter);<br /><br /> public static void DoCommandBind(CommandBase command, FrameworkElement element)<br /> {<br /> element.CommandBindings.Add(command.CommandBinding);<br /> }<br /> }<br /></pre><br /><p> The base class simply provides everything that you need to make use of RoutedCommand. It wraps the command and its command binding whose execute and can execute handlers are redirected to the abstract methods. So the implementation would be as simple as the one shown below and you would have a full fledged RoutedCommand</p><br /><br /><pre name="code" class="csharp"><br />public class SampleCommand : CommandBase<br /> {<br /> protected override void Execute(object parameter)<br /> {<br /> MessageBox.Show(parameter.ToString());<br /> }<br /><br /> protected override bool CanExecute(object parameter)<br /> {<br /> return parameter != null;<br /> }<br /> }<br /></pre><br /><p>In the above command, I simply execute the command if there is a parameter sent and when executed display the parameter passed in a MessageBox. In order to use this command in XAML, the markup would be as simple as : </p><br /><pre name="code" class="xml"><br /><Button Command="{Binding Sample.Command}" CommandParameter="This is from XAML" Content="From XAML" /><br /></pre><br /><p>But for the RoutedCommand to work, the CommandBinding associated with it should be added to one of the elements up in the Visual Tree. So one of the parents for the Button should have the CommandBinding for the command to be registered with them. This is performed using the CommandBase.DoCommandBind() method. This is required because - the RoutedCommand - even though it implements ICommand interface, the Execute() and CanExecute() methods simply trigger the events that result in the CommandBinding execute the handlers that were specified when the command binding was being created. Read on MSDN for a much better english explanation. If the command binding cannot be found on any of the ancestors, the command would never fire! This is important to understand when we later look at the C# way to execute a RoutedCommand. </p><br /><br /><p> For now, look at the code behind. It is simple and what happens in the constructor is self explanatory. </p><br /><br /><pre name="code" class="csharp"><br /> private CommandBase sample = new SampleCommand();<br /> <br /> public CommandBase Sample<br /> {<br /> get { return sample; }<br /> }<br /><br /> public CommandDemo()<br /> {<br /> InitializeComponent();<br /> this.DataContext = this;<br /> //do a command binding on this UserControl itself.<br /> CommandBase.DoCommandBind(sample, this);<br /> }<br /></pre><br /><br /><h3>Gotcha 1 : RoutedCommand does not fire!</h3><br /><p>If this is the case, then make sure the CommandBinding for the RoutedCommand has been registered properly. You can do it in Code-Behind (like CommandBase.DoCommandBind() in my example) or in XAML (loads of examples online for that).</p><br /><br /><h3> How do I execute a RoutedCommand from code-behind? </h3><br /><p> Again, same rules apply. The command binding should be available to be found on the ancestors where the command will be fired. If that is the case, then you can do it in the following way.</p><br /><br /><pre name="code" class="csharp"><br />var rc = (sample.Command as RoutedCommand);<br />if (rc.CanExecute("This is command parameter", e.OriginalSource as Control))<br /> rc.Execute("This is command parameter", e.OriginalSource as Control);<br /></pre><br /><br /><P> Executing the ICommand.Execute(parameter) (eg: sample.Command.Execute("parameter")) would work, but if in any case it does not work, use the method above. </p><br /><h3>Gotcha 2 : RoutedCommand.CanExecute(parameter) does not fire when executing a RoutedCommand from code-behind!</h3><br /><p> Yes, ICommand.CanExecute() is just an interface method, its the job the command invoker (if done in code-behind, it is you who is the command invoker) to verify if the command can be executed using CanExecute(). </p><br /><br /><p> Well, thats all for now, I hope this helps some of us who were struggling with one place solution to execute RoutedCommands in code-behind. Hopefully, I will write one more brief article on using System.Windows.Interactivity.Triggers to redirect an event to a command, the MVVM way of executing actions. </p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-15985128541425984912010-07-19T21:08:00.001-07:002010-07-19T21:08:04.105-07:00Bing! Are you kidding me!<p><a href="http://lh6.ggpht.com/_Oy9HuksgrdU/TEUhHzpxbrI/AAAAAAAABaM/JVYQ5b43lN0/s1600-h/image%5B3%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijP79cKbeCJYKCAJtCV8PHI-Zh4ajbrdT1-l8VTr5ckbCtwGuTtOtLGP2hyphenhyphenWerrfuzvagkwjOIX1l1UW74MoriIofulLkVrA6mip6O7DoDjSa3K7AOD76vlhBw5EpXJ6ShY0ngOQ/?imgmax=800" width="644" height="440"></a></p> <p>Seriously?? If these things happen, Bing! would never be the search engine that I wish it would become.</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-26837951734356511482010-07-16T15:05:00.001-07:002010-07-16T15:05:36.989-07:00WPF Datagrid – Load and Performance<p>This post is not about performance numbers of WPF Datagrid but simply about what you should be aware of in order to make it perform well. I was not motivated enough to use profiler to show realistic numbers but instead used the Stopwatch class wherever applicable. This post does not go into techniques to handle large amounts of data such as Paging or how to implement paging, but focuses on how to make the datagrid work with large data.</p> <p>Here is the C# class that generates the data I want to load the Datagrid with.</p><pre class="csharp" name="code">public class DataItem<br /> {<br /> public long Id { get; set; }<br /> public string FirstName { get; set; }<br /> public string LastName { get; set; }<br /> public long Age { get; set; }<br /> public string City { get; set; }<br /> public string Designation { get; set; }<br /> public string Department { get; set; }<br /> }<br /><br /> public static class DataGenerator<br /> {<br /> private static int _next = 1;<br /> public static IEnumerable<dataitem> GetData(int count)<br /> {<br /> for (var i = 0; i < count; i++)<br /> {<br /> string nextRandomString = NextRandomString(30);<br /> yield return new DataItem<br /> {<br /> Age = rand.Next(100),<br /> City = nextRandomString,<br /> Department = nextRandomString,<br /> Designation = nextRandomString,<br /> FirstName = nextRandomString,<br /> LastName = nextRandomString,<br /> Id = _next++<br /> };<br /> }<br /> }<br /><br /> private static readonly Random rand = new Random();<br /><br /> private static string NextRandomString(int size)<br /> {<br /> var bytes = new byte[size];<br /> rand.NextBytes(bytes);<br /> return Encoding.UTF8.GetString(bytes);<br /> }<br /> }<br /></pre><br /><p>My ViewModel has been defined as shown below.</p><pre class="csharp" name="code"> public class MainWindowViewModel : INotifyPropertyChanged<br /> {<br /> private void Notify(string propName)<br /> {<br /> if (PropertyChanged != null)<br /> PropertyChanged(this, new PropertyChangedEventArgs(propName));<br /> }<br /> public event PropertyChangedEventHandler PropertyChanged;<br /><br /> private Dispatcher _current;<br /> public MainWindowViewModel()<br /> {<br /> _current = Dispatcher.CurrentDispatcher;<br /> DataSize = 50;<br /> EnableGrid = true;<br /> _data = new ObservableCollection<dataitem>();<br /> }<br /><br /> private int _dataSize;<br /> public int DataSize<br /> {<br /> get { return _dataSize; }<br /> set<br /> {<br /> LoadData(value - _dataSize);<br /> _dataSize = value;<br /> Notify("DataSize");<br /> }<br /> }<br /><br /> private ObservableCollection<dataitem> _data;<br /> public ObservableCollection<dataitem> Data<br /> {<br /> get { return _data; }<br /> set<br /> {<br /> _data = value;<br /> Notify("Data");<br /> }<br /> }<br /><br /> private bool _enableGrid;<br /> public bool EnableGrid<br /> {<br /> get { return _enableGrid; }<br /> set { _enableGrid = value; Notify("EnableGrid"); }<br /> }<br /><br /> private void LoadData(int more)<br /> {<br /> Action act = () =><br /> {<br /> EnableGrid = false;<br /> if (more > 0)<br /> {<br /> foreach (var item in DataGenerator.GetData(more))<br /> _data.Add(item);<br /> }<br /> else<br /> {<br /> int itemsToRemove = -1 * more;<br /> for (var i = 0; i < itemsToRemove; i++)<br /> _data.RemoveAt(_data.Count - i - 1);<br /> }<br /> EnableGrid = true;<br /> };<br /> //act.BeginInvoke(null, null);<br /> _current.BeginInvoke(act, DispatcherPriority.ApplicationIdle);<br /> }<br /> }<br /></pre><br /><p>As you can see, as the DataSize is changed, the data would be loaded. Currently I use a slider to change the load size. This is all pretty easy and fun stuff starts in the XAML.</p><br /><p>In order to apply this "Data" to my WPF datagrid, I apply this viewmodel instance to the DataContext of my class. See below for the code-behind that I have for my window</p><pre class="csharp" name="code"> public partial class MainWindow : Window<br /> {<br /> private MainWindowViewModel vm;<br /><br /> public MainWindow()<br /> {<br /> InitializeComponent();<br /> vm = new MainWindowViewModel();<br /> this.Loaded += (s, e) => DataContext = vm;<br /> }<br /> }<br /></pre><br /><p>Lets start with the following XAML.</p><br /><p></p><pre class="xml" name="code"><stackpanel><br /> <slider maximum="100" minimum="50" value="{Binding DataSize}" /><br /> <label grid.row="1" content="{Binding DataSize}"><br /> <datagrid grid.row="2" isenabled="{Binding EnableGrid}" itemssource="{Binding Data}"><br /> </datagrid><br /></stackpanel><br /></pre><br /><p>Now build the application and run. The result appear as shown below. </p><br /><p><a href="http://lh4.ggpht.com/_Oy9HuksgrdU/TEDXbtApboI/AAAAAAAABZc/3rdhYAAloCQ/s1600-h/image%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMoe702l6evCpj6iElrzm4IiinegopiEoVxjxJjTMxUnvQpqge1nH_MbDNYXIe0oBzV1c-x6TRpWz8JzYsp41YM7Fxsd2CULoMLK5DSXr3z9lfWGsQRrOIHewVmZ8ftgbOEyRYBA/?imgmax=800" width="644" height="342"></a></p><br /><p>As you can see above, I loaded 100 items yet <strong>I do not see the scrollbar.</strong> Lets change the slider’s Maximum property from 100 to 1000 and rerun the application. Dragging the slider to 1000 at once. So even for the 1000 items, the grid does not respond that well. </p><br /><p><a href="http://lh4.ggpht.com/_Oy9HuksgrdU/TEDXese1pQI/AAAAAAAABZk/Lf5WvtHpv-Y/s1600-h/image%5B7%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Oy9HuksgrdU/TEDXgOGsWcI/AAAAAAAABZo/B66G-Pnfbj8/image_thumb%5B3%5D.png?imgmax=800" width="463" height="300"></a> </p><br /><p>Let us look at the memory usage.</p><br /><p><a href="http://lh5.ggpht.com/_Oy9HuksgrdU/TEDXg-J5y0I/AAAAAAAABZs/qussD_l9bj4/s1600-h/image%5B13%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_Oy9HuksgrdU/TEDXiOXsUvI/AAAAAAAABZw/iU1PwM3QkSg/image_thumb%5B7%5D.png?imgmax=800" width="433" height="38"></a> </p><br /><p>This is pretty heavy for an application with just 1000 items of data loaded. So what is using all this memory? You can hook up a Memory Profiler or use Windbg to look at the memory content but since I already know what is causing this issue, I am not going through that. </p><br /><p>This issue is that the DataGrid has been placed inside a StackPanel. When vertically stacked, the StackPanel basically gives its children all the space that they ask for. This makes the DataGrid create 1000 rows (all the UI elements needed for each column of each row !!) and render it. The virtualization of the DataGrid did not come into play here.</p><br /><p>So let us make a simple change and put the DataGrid inside a grid. The XAML for which is shown below.</p><pre class="xml" name="code"><Grid><br /> <Grid.RowDefinitions><br /> <RowDefinition Height="30"/><br /> <RowDefinition Height="30"/><br /> <RowDefinition Height="*"/><br /> </Grid.RowDefinitions><br /> <Slider Value="{Binding DataSize}" Minimum="50" Maximum="1000"/><br /> <Label Content="{Binding DataSize}" Grid.Row="1"/><br /> <DataGrid ItemsSource="{Binding Data}" Grid.Row="2" IsEnabled="{Binding EnableGrid}"> <br /> </DataGrid><br /> </Grid><br /></pre><br /><p>When I run the application, you would notice that as I load 1000 items, the performance of the same application (no code changes, except that XAML one I just talked about) is a lot better than what it was. Moreover I see nice scrollbars.</p><br /><p></p><a href="http://lh6.ggpht.com/_Oy9HuksgrdU/TEDXklUvTrI/AAAAAAAABZ0/QRDfmfugJvc/s1600-h/image%5B19%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Oy9HuksgrdU/TEDXmBiLhWI/AAAAAAAABZ4/inpHNDHNOIY/image_thumb%5B11%5D.png?imgmax=800" width="644" height="342"></a> <br /><p>Let us look at the memory usage.</p><br /><p><a href="http://lh4.ggpht.com/_Oy9HuksgrdU/TEDXmkYFIJI/AAAAAAAABZ8/2Eip9xZ103Q/s1600-h/image%5B23%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQXghXqvZKQOE4v1Gy5FLjUHqgnJXkqQPbTLTEO0ZKuUZ2k5f5b_61CPB6yRWyRG69l8TTh42orbs14449qkAXTAdElxiABg3O9e8rQcJKqq5yPwZ81xM98jVrVINHx8EYuIxj5w/?imgmax=800" width="644" height="29"></a> </p><br /><p>Wow! 10 folds difference. This until now appears to be a re-talk about my <a href="http://krishnabhargav.blogspot.com/2008/10/wpf-performance-ui-virtualization.html">previous post on WPF Virtualization</a>. The same rules applies to DataGrid as well. Read this post if you are intertested.</p><br /><p>So what else am I talking here. </p><br /><ul><br /><li>If you notice the ViewModel code, you should be seeing that I disable the grid as I load data and enable it back once I am done. I have not really tested to see if this technique helps, but I did use this technique in HTML pages where loads of items in a listbox were all to be selected and this technique was very useful. <br /><li>In all the screenshots I showed, the grid is sorted. So as the data changes, the grid has to keep sorting the data and show based on what you chose to sort. This, I believe, is a big overhead. Consider removing sort of the datagrid before you change the data if it is a viable option and does not impact the end user. Have not tested this, but the same should apply to the groupings as well (which most of the time cannot be simply removed).</li></ul><br /><p>With a simple point of loading the DataGrid into any other panel like Grid, instead of a StackPanel you get to see a lot of difference. The WPF datagrid performs just fine, as long as you keep the viewable region of the grid small. </p><br /><p>Shown below is my grid with almost 1 Million data items loaded. The footprint is pretty small compared to the amount of data loaded. This means – either WPF Controls are memory intensive or WPF UI Virtualization is a boon.</p><br /><h3>Impact of sorting on the DataGrid</h3><br /><ul><br /><li>With no sorting applied on the datagrid, it took almost 20 seconds to load 1 Million items into my collection. <br /><li>With sorting enabled, loading half those items iteself took over 2 minutes and the complete items took over 5 minutes and I killed the application because it was a pain. This matters because the application keeps the CPU busy with all the sort that has to keep happening as your data changes. So for every item added, the sort might be triggered, since I am placing it directly into an observable collection. <br /><li><strong>Instead consider sorting on the backend and not using the datagrid.</strong></li></ul><br /><p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilahItLBNL8tGR0FF82eXl6ALuZ29awD16mGRU0BGn_3PPFgp0qg3NEBIIo06BLp_Q-_OCzE1oLH0v2Rs3GA2CkXES26bYEwcgolFxg3-Zcxuxc7KJ1HbIfGR1ESa5Iwr7U1F8fQ/s1600-h/image%5B31%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdyKI8601ct1GDHg8qzm4x6K784xraN7_7QQAbrVlmc2Q4gKy2zUmgQO2daYBt3G8TivJK9SBJkKE9Qe93VVRc2fTR_CGdqosJJaG4eUIJvdMO_CPDexJaU1ePZXPSwzlqvcSGzg/?imgmax=800" width="644" height="342"></a> </p><br /><p><em>I can still scroll the application if the virtualization was properly utilized, inspite of the grid binding to 1 million items<strong>.</strong></em></p><br /><h3>USING BeginInit() and EndInit() on the datagrid.</h3><br /><p>I changed the ViewModel’s LoadData() such that it calls BeginInit() as it starts loading the data and EndInit() when it done loading the data. This has helped quite a lot. Loading 1 Million items (without any sort applied on the grid) only took around 8 seconds (compared to the 18 seconds it took earlier). <strong>Unfortunately I did not spend enough time to use a profiler to show real numbers.</strong></p><br /><p>The changed code-behind for the Window is as shown.</p><pre class="csharp" name="code">public partial class MainWindow : Window<br /> {<br /> private MainWindowViewModel vm;<br /><br /> public MainWindow()<br /> {<br /> InitializeComponent();<br /> vm = new MainWindowViewModel();<br /> this.Loaded += (s, e) => DataContext = vm;<br /> vm.DataChangeStarted += () => dg.BeginInit();<br /> vm.DataChangeCompleted += () => dg.EndInit();<br /> }<br /> }<br /></pre><br /><p>I also had to include the DataChangeStarted and DataChangeCompleted actions to the Viewmodel class. The changed portion of the ViewModel class is shown below.</p><pre class="csharp" name="code"> public event Action DataChangeStarted ;<br /> public event Action DataChangeCompleted;<br /><br /> private void LoadData(int more)<br /> {<br /> Action act = () =><br /> {<br /> //Before the data starts change, call the method.<br /> if (DataChangeStarted != null) DataChangeStarted();<br /> var sw = Stopwatch.StartNew();<br /> EnableGrid = false;<br /> if (more > 0)<br /> {<br /> foreach (var item in DataGenerator.GetData(more))<br /> _data.Add(item);<br /> }<br /> else<br /> {<br /> int itemsToRemove = -1 * more;<br /> for (var i = 0; i < itemsToRemove; i++)<br /> _data.RemoveAt(_data.Count - i - 1);<br /> }<br /> EnableGrid = true;<br /> sw.Stop();<br /> Debug.WriteLine(sw.ElapsedMilliseconds);<br /> if (DataChangeCompleted != null) DataChangeCompleted();<br /> };<br /> //act.BeginInvoke(null, null);<br /> _current.BeginInvoke(act, DispatcherPriority.ApplicationIdle);<br /> }<br /></pre><br /><p>You can try this out and notice the performance difference yourself. </p><br /><p>If the sorting is applied on the datagrid, the performance still hurts in spite of using the above mentioned trick. The overhead of sorting out weighs the performance gain we get calling the BeginInit and EndInit. May be having 1 million records is not realistic enough. </p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-80121368525468156922010-07-15T19:30:00.000-07:002010-07-15T19:37:24.402-07:00Using LINQ Aggregate to solve the previous problemIn the <a href="http://krishnabhargav.blogspot.com/2010/07/algorithms-performance-and-getting.html">previous post</a> I talked about the problem which I simply re-iterate here. From the data which can look like <p/><br />Name, Value <p/><br />Sridhar, 1 <p/><br />Ashish,2<p/><br />PRasanth,3<p/><br />Ashish,5<p/><br />Sridhar,6<p/><br />Prasanth,34<p/><br />.....<br /><br />I want to aggregate the values for the names. Look at the previous post for some information on other approaches to solve this simple problem.<br /><br />The LINQ way to do this would be : <br /><pre name="code" class="csharp"><br />[Test]<br /> public void BTest()<br /> {<br /> var nvcs = tl.GroupBy(s => s.Name)<br /> .Select(s => new NameValueCollection<br /> {<br /> {"Name", s.Key},<br /> {"DrawerId", s.Aggregate(new StringBuilder(), <br /> (seed, g) => seed.AppendFormat("{0};",g.DrawerId)).ToString()}<br /> });<br /> //foreach (var nvc in nvcs)<br /> // Console.WriteLine(nvc["Name"] + " : " + nvc["DrawerId"]);<br /> Assert.AreEqual(4, nvcs.Count());<br /> }<br /></pre><br /><br />Note that I wanted am generating a list of NameValueCollection and this is not of significance here. If you compare it with the previous implementation that uses dictionary or lists to generate, this solution appears more concise and to those who already knows LINQ should find this really simple.<br /><br /><ul><br /><li> All I would like to take away from this post is that the IEnumerable.Aggregate() method is a great method that is not often mentioned around. We often accumulate some value over a collection of items and aggregate method lets you do just that without all the extra for and seeds that you should track.<br /></ul>Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-12285784865249638142010-07-15T19:05:00.000-07:002010-07-15T19:38:36.659-07:00Algorithms, performance and getting burntAfter a long time, I am writing something on my blog. So here it is ..<p>This post is about me starting to solve a small but interesting problem with different approaches and ended up breaking my head against why an algorithm with supposedly O(n) complexity is 4 times slower than O(n^2).</p><p>So here's the issue. I have the following data : </p><p>Name,Value <p/><br />Sridhar,1<p/><br />Ashish,2<p/><br />Prasanth,3<p/><br />Sridhar,4<p/><br />Ashish,5<p/><br />Sridhar,8</p><p>and so on .. I hope you get the idea.<br /><br />Now what I would like to do is to print the following output.<p/><br />Sridhar : 1;4;8;....<p/><br />Ashish : 2;4;.....<p/><br />Prasanth: 3;......<p/><br /><br />Note that here, it does not matter what the values are, I am giving this data just for the example. So shown below is my setup which would be used by my implementations. (I am demoing it as a test).<br /></p><br /><pre name="code" class="csharp"><br /> private Stopwatch sw;<br /> [SetUp]<br /> public void SetUp()<br /> {<br /> GC.GetTotalMemory(true); // I dont know why i did this!<br /> tl = new List<Ud>(10000);<br /> var names = new[] { "Krishna", "Ashish", "Sridhar", "Prasanth" };<br /> foreach (var name in names)<br /> for (var i = 0; i < 2500; i++)<br /> tl.Add(new Ud { Name = name, DrawerId = i.ToString() });<br /> tl.OrderBy(s => s.DrawerId);<br /> sw = Stopwatch.StartNew();<br /> }<br /><br /> [TearDown]<br /> public void TearDown()<br /> {<br /> sw.Stop();<br /> Console.WriteLine(sw.ElapsedMilliseconds);<br /> sw = null;<br /> }<br /><br /> public class Ud<br /> {<br /> public string Name { get; set; }<br /> public string DrawerId { get; set; }<br /> }<br /><br /> private List<Ud> tl;<br /></pre><br /><p>The above code is self explanatory. I basically create a lot of Ud objects which generate the data that I presented earlier. Shown below is the most straight forward way to do it. It has two for-loops which makes the complexity O(n^2).</p><br /><pre name="code" class="csharp"><br />[Test]<br /> public void BasicImplementation()<br /> {<br /> var nvcs = new List<NameValueCollection>();<br /> var list = new List<string>();<br /> foreach (var item in tl)<br /> {<br /> if (list.Contains(item.Name)) continue;<br /><br /> string val = string.Empty;<br /><br /> foreach (var item2 in tl)<br /> {<br /> if (item2.Name == item.Name)<br /> val += item2.DrawerId + ";";<br /> }<br /><br /> nvcs.Add(new NameValueCollection { { "Name", item.Name }, { "DrawerId", val } });<br /> list.Add(item.Name);<br /> }<br /> //foreach (var nvc in nvcs)<br /> // Console.WriteLine(nvc["Name"] + " : " + nvc["DrawerId"]);<br /> Assert.AreEqual(4, nvcs.Count);<br /> }<br /></pre><br /><p> Now I went ahead and added another potential implementation which gives the same result but instead makes use of dictionary to track the strings that we build for each name in the list of objects. So instinctively, it appears that the dictionary method would be way faster than the one mentioned above. Lets look at that code. </p><br /><pre name="code" class="csharp"><br /> [Test]<br /> public void ADictionary()<br /> {<br /> var vals = new Dictionary<string, string>();<br /> foreach (var item in tl)<br /> {<br /> if (!vals.ContainsKey(item.Name))<br /> vals[item.Name] = item.DrawerId;<br /> else<br /> vals[item.Name] = vals[item.Name] + item.DrawerId + ";";<br /> }<br /> Assert.AreEqual(4, vals.Values.Count);<br /> }<br /></pre><br /><p> When I ran these two tests, I did not notice any performance gain with the above O(n) implementation and in fact it was three times slower. So why was it slower? Look at the setup, it has GC.GetTotalMemory(true) which forced a full garbage collection and its time was accounted in the time consumed by this dictionary as well since for the second time (when test with dictionary was executing) it had a lot of strings to clean up. So why did I put it in the first place? The answer is "I was not thinking straight". Never ever use GC classes in your code. It is a bad-bad-bad practice.</p><br /><p>So I remove this GC call made and rerun the tests again. Yet I do not see any performance gain. WHY?? I took a lot of time trying to diagnose why this is happening and eventually gave up manual inspection. I downloaded the trail version of dotTrace (which is freaking awesome tool) Performance 4.0 and made it profile both the tests. The culprit was the strings. If you look at the code right, we are generate a lot of strings whose "Concat" operation was so time consuming that it dominated the gain that we obtained using O(n) algorithm.</p><br /><p>So the lesson here is "Be watchful of the strings that are generated when your code executes, otherwise you would be burned". It does not matter how small the string concatenation may seem but in cases like above it piles up a lot and screws up your clever algorithm. All I did was to change the tests to use stringbuilder instead of Strings.</p> <br /><ul><br /><li> Do not use GC calls in your code, especially those which force GC.<br /><li> Use a profiler to accurately capture performance information of specific methods or your program. Stopwatch, Timers, etc are not good enough and waste of time.<br /><li> Be aware of the impact of string operations. Use StringBuilder wherever possible. Use String.Format() in other simpler cases.<br /></ul><br />I will continue in the next post with some code that shows you how to approach the problem I initially started with using LINQ and how simple things would appear.Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-86427340444198840892010-07-11T21:00:00.000-07:002010-07-11T21:01:16.020-07:00Issues with SyntaxHighlighter on my blogI just messed up my blog template and could not get the syntaxhighlighter plugin to work properly. I will be fixing this shortly but in the meantime if the code seems really ugly to you all, I apologize for the inconvenience.Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-65323113968295237902010-05-05T21:24:00.001-07:002010-07-11T21:47:56.355-07:00WCF Security – the way I wanted to learn<p>For intranet applications where the users could be authenticated against Active Directory, using WindowsCredentials, setting up security for a WCF service might not be all that difficult. It might not be difficult even to set up a WCF Service hosted by IIS and make it use the ASP.NET Roles/Providers. But what I wanted was to come up with a series of steps that allows me to secure a WCF service for internet-like applications. While it appears that there would have been 1000s of implementations on the subject where the client application provides a UserName/Password login control and then on the authenticated users would be able to work with the service.</p> <p>To go back a little, this is what I want </p> <p>-  I have a client application which has a Login Control and the user enters Username and password. Without proper username.password combination, the service communications going forward should not be allowed. Remember Forms Authentication in ASP.NET ?? Something similar to that. </p> <p>- I do not want to tie my service strongly to the host – meaning I do not want to make use of the ASP.NET Membership Provider models, though it is relatively easy to do so. So I have a console host program that serves as the WCF Service.</p> <p>- For every call to an OperationContract, I do not want to read the Message Headers or add extra parameters to see the username and password. I don’t want specific logic within each operation that handles this check.</p> <p>- I want the operations to be limited to those users with some kind of “Roles”. Basically, i have a set of operations that only users of a Role “X” should be able to perform; whereas there are some other operations for users with other roles.</p> <p>- I don’t want my communication channel to be open and would want to prevent the users to sniff the traffic to see what is going on.</p> <p>To summarize these requirements, </p> <p>- I want a secure communication between client and server. <br />- I want to restrict access to the service unless the client sends in valid username/password. <br />- I want to restrict access to operations based on the roles of the calling user. <br />- I don’t want to deal with Windows Authentication at this moment, since I have plans to host my service on the internet in which case WindowsIdentity is not really preferred.</p> <p>In this post, I would like to show the way I achieved these goals. Note that I am not qualified enough to make strong statements or give strong explanation on how the security works. The intention of this post would be to help assist developers like me who has little knowledge of WCF Security but do understand how Security works in general. I recommend you read MSDN documentation for the classes and terms I throw here and there.</p> <p>While, the source code is available for download here : <a href="http://drop.io/yskic3h">http://drop.io/yskic3h</a> , here in this post I simply mention the steps that I used to achieve each of the goals mentioned above.</p> <h3>1. Secure Communication Channel</h3> <p>Used wsHttpBinding as the binding of my choice. The wsHttpBinding by default employs Windows security. We would have to change that to make use of Message security using “UserName” as clientCredentialType. All this is configured as a binding configuration.</p> <pre name="code" class="xml">
<br /><wsHttpBinding>
<br /> <binding name="secureBinding">
<br /> <!-- the security would be applied at Message level -->
<br /> <security mode="Message">
<br /> <message clientCredentialType="UserName"/>
<br /> </security>
<br /> </binding>
<br /></wsHttpBinding>
<br /></pre>
<br /><p>Now this bindingConfiguration has to be set on the endpoint as shown</p>
<br /><pre name="code" class="xml">
<br /><services>
<br /> <service name="WcfService.SecureService" behaviorConfiguration="secureBehavior">
<br /> <!--
<br /> notice the bindingConfiguration, we are applying secureBinding that
<br /> was defined in the bindings section.
<br /> -->
<br /> <endpoint address="secureService"
<br /> binding="wsHttpBinding"
<br /> bindingConfiguration="secureBinding"
<br /> contract="WcfService.ISecureService"/>
<br /> <host>
<br /> <baseAddresses>
<br /> <add baseAddress="http://truebuddi:8080/" />
<br /> </baseAddresses>
<br /> </host>
<br /> <endpoint address="mex" binding="mexHttpBinding" contract="WcfService.ISecureService"/>
<br /> </service>
<br /></services>
<br /></pre>
<br />
<br /><p>Now that we know the server is expecting username and password, we want a custom validator which checks this username and password combination against our custom repository of users. To do that, we would have to configure the service behavior this time. So binding ensures credentials are being passed and the service behavior validates them! ;)</p>
<br />
<br /><pre name="code" class="xml">
<br /> <serviceBehaviors>
<br /> <behavior name="secureBehavior">
<br /> <serviceMetadata httpGetEnabled="true" />
<br /> <serviceDebug includeExceptionDetailInFaults="true" />
<br />
<br /> <serviceCredentials>
<br />
<br /> <serviceCertificate
<br /> findValue="wcfSecureService"
<br /> storeLocation="LocalMachine"
<br /> storeName="My"
<br /> x509FindType="FindBySubjectName" />
<br />
<br /> <!--
<br /> Now in secureBinding (see in bindings section),
<br /> we set the Message security to use "UserName"
<br /> as ClientCredentialType. So we would like to
<br /> use a custom username password validator.
<br /> Here we specify that our custom validator should be used.
<br /> -->
<br /> <userNameAuthentication
<br /> userNamePasswordValidationMode="Custom"
<br /> customUserNamePasswordValidatorType="WcfService.CustomUserNamePasswordValidator, WcfService" />
<br /> </serviceCredentials>
<br />
<br />
<br /> <!--
<br /> The Custom Authorization policy is what used to verify the roles.
<br /> For a Role specified in the PrincipalPermission attribute,
<br /> IsInRole() method in the Principal that was set from the
<br /> CustomAuthorizationPolicy.Evaluate would be invoked.
<br /> -->
<br /> <serviceAuthorization principalPermissionMode='Custom'>
<br /> <authorizationPolicies>
<br /> <add policyType='WcfService.CustomAuthorizationPolicy, WcfService' />
<br /> </authorizationPolicies>
<br /> </serviceAuthorization>
<br />
<br /> </behavior>
<br /></serviceBehaviors>
<br /></pre>
<br />
<br /><p>
<br />In the above configuration, using the serviceCredentials\userNameAuthentication element we specify that the userName/password are to be validated using custom validator type.
<br />This is all configured there. While the username and password authenticates the client on the service, I think it does not really do anything to the communication channel.
<br />To make the channel secure, we make use of certificates. In order to do this, the following steps are required to be done on the development machine so that the sample gets working :
<br /><ol>
<br />
<br /><li>Using the makecert application (can run from Visual Studio Command), create and register the command for exchanging. Note that if you follow the MSDN article on creating certificates using makecert, it does not tell you about enabling the certificate such that it is suitable for key exchanging. So the command that worked for me is </br/>
<br /><p> makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN="wcfSecureService" -sky exchange -pe -r wcfSecureService.cer </p> </li>
<br />
<br /><li>
<br />We specify the same certificate to be used in the service configuration file using the serviceCredentials\serviceCertificate element. See the configuration snippet shown previously. It basically says "find the certificate by subject name where subject name is 'wcfSecureService' in the certificate store on the local machine and the store would be Personal". For all this to work, note that HTTPS base address should be used.</li>
<br />
<br /><li> While the first two steps takes care of the certificate on the server, the client should have some knowledge (basically the client should know the public key with which the messages would be encrypted) of the existence of the certificate. we specify that in the endpoint\identity section of the client configuration [see below]. The encodedValue can be obtained by adding service reference from Visual Studio which generates shit load of configuration on the client, just save the encodedValue and revamp your configuration file. </li>
<br /></ol>
<br /></p>
<br />
<br /><pre name="code" class="xml">
<br /><client>
<br /> <endpoint address="http://truebuddi:8080/secureService" binding="wsHttpBinding"
<br /> bindingConfiguration="secureWsHttpBinding"
<br /> behaviorConfiguration="ignoreCert"
<br /> contract="SecurityDemo.ISecureService">
<br /> <identity>
<br /> <!-- Don't panic this key is wrong ;) for the sake of this post-->
<br /> <certificate encodedValue="AwAAAAEAAAAUAAAAee8O3PpkfSCfjaa3mDmkK+HLb4QgAAAAAQAAAAcCAAAwggIDMIIBcKADAgECAhDgA4A6S0Z/j0d3IFg04e9gMAkGBSsOAwIdBQAwGzEZMBcGA1UEAxMQd2NmU2VjdXJlU2VydmljZTAeFw0xMDA1MDUyMzQ4NTZaFw0zOTEyMzEyMzU5NTlaMBsxGTAXBgNVBAMTEHdjZlNlY3VyZVNlcnZpY2UwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALF7OJsZ6AV5yqSSQyne9j+xwdRLDRoVMleYg0vGvB7W7Bk5zBNbSDCbb+spJR3ykayDoZYpykyY8Q7qzvPuUPdHu7SkMVZ9Ng8B8yAq0zrD8sJwnaqTEY4a8mj8Dt86Yr0wK31aF4VSDRZaK+XDyFd5hWU8Eya+bohhixndMYwNAgMBAAGjUDBOMEwGA1UdAQRFMEOAEJRtYMFDVIgPHFrIf0LU5e+hHTAbMRkwFwYDVQQDExB3Y2ZTZWN1cmVTZXJ2aWNlghDgA4A6S0Z/j0d3IFg04e9gMAkGBSsOAwIdBQADgYEApQ+Hy6e4hV5rKRn93IMcEL3tW2tUYcj/oifGbEPRX329s3cc8QH6jYaNN8cgS5RN+6QffrkvupMSUauGsWia20WHTRI8lyb+1gvvX4NpTxZE6+sZkvIu6R/qIsC6V9pbRCHm3HRFnAoMNZmPTr5mJvzwAQZzOdXMFq0OwakJKEw=" />
<br /> </identity>
<br /> </endpoint>
<br /></client>
<br />
<br /></pre>
<br />
<br /><p>You can also look at this <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.publickey.encodedkeyvalue(VS.85).aspx">link</a> to get the public key in any case.</p>
<br />
<br /><p>For testing purposes, you should also add a behaviorConfiguration on the client's endpoint such that certificates are not validated, once you deploy, you can remove this behavior.</p>
<br />
<br /><pre name="code" class="xml">
<br /><behaviors>
<br /> <endpointBehaviors>
<br /> <!-- ignore cerificates validation for testing purposes. -->
<br /> <behavior name="ignoreCert">
<br /> <clientCredentials>
<br /> <serviceCertificate>
<br /> <authentication certificateValidationMode="None" />
<br /> </serviceCertificate>
<br /> </clientCredentials>
<br /> </behavior>
<br /> </endpointBehaviors>
<br /></behaviors>
<br /></pre>
<br />
<br /><p> On the client config, you should also give the same wsHttpBinding with similar behavior but with few other options added. See snippet and compare it with the binding snippet that was shown earlier for the server </p>
<br />
<br /><pre name="code" class="xml">
<br /><bindings>
<br /> <wsHttpBinding>
<br /> <binding name="secureWsHttpBinding">
<br /> <security mode="Message">
<br /> <message clientCredentialType="UserName"
<br /> negotiateServiceCredential="true"
<br /> establishSecurityContext="true"/>
<br /> </security>
<br /> </binding>
<br /> </wsHttpBinding>
<br /></bindings>
<br /></pre>
<br />
<br /><p>With this the communication channel is secure. You might have some issues with Certificates but you should be able to use the exception messages to bing for answers online in the forums. Only other part that is left on the client end is to make sure that the client proxy used are set with Username and password. Code for the full client is shown below. </p>
<br />
<br /><pre name="code" class="csharp">
<br /> SecureServiceClient client = new SecureServiceClient();
<br /> client.ClientCredentials.UserName.UserName = "Krishna";
<br /> client.ClientCredentials.UserName.Password = "test";
<br /> User test = client.Login();
<br /> client.SafeOperationByAdmin();
<br /></pre>
<br />
<br /><h3>2. UserName and Password Custom Validation</h3>
<br /><p>Implement a type that derives from UserNamePasswordValidator class. You would have to reference the System.IdentityModel.dll and if you remember, we set the custom validator in the service behavior on the service configuration file. While the code shown below does not talk to DB, it should still serve as a good example on custom username and password validation. Note that this Validate() method gets called for evevery
<br /></p>
<br /><pre name="code" class="csharp">
<br /> public class CustomUserNamePasswordValidator : UserNamePasswordValidator
<br /> {
<br /> public override void Validate(string userName, string password)
<br /> {
<br /> Console.WriteLine("Username validation started");
<br /> if (userName == "Krishna" && password == "test")
<br /> return;
<br /> throw new InvalidCredentialException("Invalid credentials passed to the service");
<br /> }
<br /> }
<br /></pre>
<br />
<br /><h3>3. Restriction of Operations using Roles </h3>
<br /><p>The operations can be restricted to users of certain roles by applying a PrincipalPermission attribute on the OperationContract [see below]. The current principal would be checked to see if it is in the role specified, otherwise the operation would not allowed to be executed. Now how do we set this Principal to something? To do this, we need a CustomPrincipal which you should derive from IPrincipal. This Principal implementation has IIdentity which can be WindowsIndetity for Windows Authentication and GenericIdentity for other scenarios. Now this CustomPrincipal should be created and applied somewhere right? This is where the IAuthorizationPolicy comes into play, We should have a custom authorization policy whose Evaluate method should take care of fetching the identity and passing it to a newly created custom principal. This custom principal has to be set as the current principal. All the three code snippets : PrincipalPermission attribute on operations, CustomPrincipal and CustomAuthorizationPolicy is shown below.</p>
<br />
<br /><pre name="code" class="csharp">
<br />///
<br />///This authorization policy is set on the service behavior using service authorization element.
<br />public class CustomAuthorizationPolicy : IAuthorizationPolicy
<br />{
<br /> public bool Evaluate(EvaluationContext evaluationContext, ref object state)
<br /> {
<br /> IIdentity client = (IIdentity)(evaluationContext.Properties["Identities"] as IList)[0];
<br /> // set the custom principal
<br /> evaluationContext.Properties["Principal"] = new CustomPrincipal(client);
<br /> return true;
<br /> }
<br />
<br /> private IIdentity GetClientIdentity(EvaluationContext evaluationContext)
<br /> {
<br /> return null;
<br /> }
<br />
<br /> public System.IdentityModel.Claims.ClaimSet Issuer
<br /> {
<br /> get { throw new NotImplementedException(); }
<br /> }
<br />
<br /> public string Id
<br /> {
<br /> get { throw new NotImplementedException(); }
<br /> }
<br /> }
<br />
<br />public class CustomPrincipal : IPrincipal
<br /> {
<br /> private IIdentity identity;
<br />
<br /> public CustomPrincipal(IIdentity identity)
<br /> {
<br /> this.identity = identity;
<br /> }
<br />
<br /> public IIdentity Identity
<br /> {
<br /> get
<br /> {
<br /> return identity;
<br /> }
<br /> }
<br />
<br /> public bool IsInRole(string role)
<br /> {
<br /> return true;
<br /> }
<br /> }
<br />
<br />///in the WCF Service implementation
<br />[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
<br />public void SafeOperationByAdmin()
<br />{
<br />///more code
<br />}
<br /></pre>
<br />
<br /><p>The newly created Authorization Policy should be configured inside the service configuration file in the serviceBehavior\serviceAuthorization as shown below.</p>
<br />
<br /><pre name="code" class="xml">
<br />
<br /><!--
<br />The Custom Authorization policy is what used to verify the roles.
<br />For a Role specified in the PrincipalPermission attribute,
<br />IsInRole() method in the Principal that was set from the
<br />CustomAuthorizationPolicy.Evaluate would be invoked.
<br />-->
<br /><serviceAuthorization principalPermissionMode='Custom'>
<br /> <authorizationPolicies>
<br /> <add policyType='WcfService.CustomAuthorizationPolicy, WcfService' />
<br /> </authorizationPolicies>
<br /></serviceAuthorization>
<br />
<br /></pre>
<br />
<br /><p>
<br />To summarize, the following are the steps that should be performed to get the security working in WCF. (Message level security).
<br /><ol>
<br /><li> Create and register a certificate. Configure the service configuration specifying the certificate to use. This is done in the bindingConfiguration and the binding configuration is then applied on the endpoint.
<br /><li> Configure the service to make use of Message level security. Again done on the server configuration.
<br /><li> Configure the client to use encodedValue for its communication which is the public key. This is done in the bindingConfiguration on the client's endpoint. For testing purposes you can make the client not validate the certificates. This is done on the end point behaviors.
<br /><li> Configure the client's binding to make use of Message level security with UserName.
<br /><li> The client's code should specify the username and password. To validate this information, register a custom UserNamePasswordValidator in the serviceBehavior on the server configuration.
<br /><li> For roles, create a custom prinicipal and set it using a Custom Authorization policy. This authorization policy should be registered in the serviceAuthorization of the serviceBehavior in the server configuration file.
<br /><ol>
<br /></p>
<br />
<br />Again the code is available for download at : http://drop.io/yskic3h . Sometime, in the future, I would try to upload the code to codeplex or put it in the Windows Live Skydrive. Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-4488648451582357952010-04-15T06:07:00.001-07:002010-04-15T06:07:16.928-07:00Running Moles using NUnit Console from Visual Studio<p>Create an external tool in Visual Studio as shown below</p> <p><a href="http://lh3.ggpht.com/_Oy9HuksgrdU/S8cPghQVkZI/AAAAAAAABLE/GRguELA8sS0/s1600-h/image%5B3%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_Oy9HuksgrdU/S8cPhJO31tI/AAAAAAAABLI/_40bTSW4fIE/image_thumb%5B1%5D.png?imgmax=800" width="475" height="462" /></a> </p> <p>Command : C:\Program Files (x86)\Microsoft Moles\bin\moles.runner.exe </p> <p>Arguments: $(BinDir)/$(TargetName).dll /runner:"c:\Development Tools\NUnit 2.5.2\bin\net-2.0\nunit-console.exe" /x86</p> <p>Initialize Directory : $(ProjectDir)</p> <p>Now you can use Moles from within Visual Studio. :)</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-62606144430560461862010-03-18T19:27:00.001-07:002010-03-18T19:27:11.870-07:00Introducing Comfy.Couch, a CouchDB API for .NET<p>WARNING : The work is still in progress.</p> <p>From the past few days, I have been working on coming up with a nice little .NET Library that one could use with CouchDB. My goal was to stick with the CouchDB API documentation as much as I can so that it would be easier to work with the driver. The library has dependencies on Log4Net and Json.NET libraries.</p> <p>I tried to be over-smart when picking that name – Comfy.Couch (I meant a comfortable couch to use :)). At the moment, the database API is functionally complete, but you cannot really use it unless you have the Document API which would be coming soon. The reference is at <a href="http://wiki.apache.org/couchdb/HTTP_database_API">http://wiki.apache.org/couchdb/HTTP_database_API</a></p> <p>I made sure I wrote unit tests to cover most part of my code, but I did miss some pieces here and there.</p> <p><a href="http://lh5.ggpht.com/_Oy9HuksgrdU/S6Lg_K66wJI/AAAAAAAABK8/Six65bmoCK4/s1600-h/image%5B3%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjgLEE4JF9FFKVRcHqbAMP0XpQXYdao0JqExL6x9IMXH-cDFrBzv__t6ZwMKfpD_2h5gu_nvyfgVD3Bm3_5DK0R4fnH0gBaQMFw9Zd4NY9mQt0wiFO-i85nI3C6wN-3Le7GeRMNQ/?imgmax=800" width="644" height="369" /></a> </p> <p>So I ended up having 94% of code coverage so far and I will be adding tests soon.</p> <p>My idea is to first get the complete API in place and then worry about where it has to be tuned. So far, I have not focused on any kind of tuning, which I think could be done. I have thrown in a few asynchronous requests here and there but they are yet to be tested.</p> <p>1. Creating a database.</p> <pre name="code" class="c#"><br />CouchDatabaseOperations.Create("databasename"); //validation on database names is pending. <br /></pre><br /><br /><p>2. Getting database information from the server</p><br /><pre name="code" class="c#"><br />ICouchDatabase db = CouchDatabaseOperations.Get("databasename");<br />CouchDatabaseInfo info = db.Metadata; <br /></pre><br /><br /><p>3. Deleting the database</p><br /><pre name="code" class="c#"><br />//Delete operation uses MaxRetries as 2 since most of the times the first delete request fails on Windows.<br />CouchDatabaseOperations.Delete("databasename"); <br /></pre><br /><br /><p>While I have successfully completed the rest of the API that is described in the Database API reference at CouchDB website, I just showed how the library could be used in here. The unit tests might be more helpful for you to get started, if you are interested.</p><br /><br /><p>Next steps: </p><br /><ul><br /><li> Upload the sourcecode to comfy.codeplex.com<br /><li> Improvize logging information<br /><li> Work on the Document API<br /><li> Work on the View API<br /><li> Work on the Bulk Document API<br /><li> Run some performance tests and identify the bottlenecks in the driver.<br /><li> Sample Scrum Management tool in Silverlight 4 that uses CouchDB for data backend.<br /></ul> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com1tag:blogger.com,1999:blog-19967453.post-58235656878324668032010-02-10T19:24:00.001-08:002010-02-10T19:24:28.482-08:00Using .NET 3.5 (CLR 2.0) DLL inside Visual Studio 2010 for a .NET 4.0 Project/Application<p>When you first create a .NET 4.0 project inside VS 2010 and add reference to a .NET 3.5 DLL (say log4net or Rhino.Mocks), the project tends to not build. You can get errors as shown below.</p> <p><a href="http://lh4.ggpht.com/_Oy9HuksgrdU/S3N4aGcY8iI/AAAAAAAABKo/13NGfYQ2S3c/s1600-h/image%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiCDBnsf1hJT-zdijGeri-9rTQSY_Y1DmRm3V-nvz-ndUVyPeMc0PKh7ZhFMlETiS8VTMHjGbHPidc0g4Vb5z0bAewEaNjqexigvlnb42rEd80gqB5QbMwmq3iJvOwqCxInakx8Q/?imgmax=800" width="644" height="89" /></a> </p> <p>It might appear to be a CLR version issue – unable to run 2.0 DLL inside a 4.0 App Domain. And i thought that is what it was until now. </p> <p>You can get over this :) </p> <p>By default, VS 2010 creates the projects with Target Framework on the properties set to “.NET Framework 4 Client Profile”. You would have to change that to “.NET Framework 4” by going to Project Properties –> Application –>  Target Framework. And every thing begins to compile.</p> <p>So I guess, one has to be aware of this when migrating old solutions from Visual Studio 2008 to Visual Studio 2010.</p> <p>Proof that it works :) Notice the .NET Framework 4.0 features as well Log4Net and Rhino.Mocks used in all the same example. (It is a stupid example, but the intention was to show it works).</p> <p><a href="http://lh6.ggpht.com/_Oy9HuksgrdU/S3N4ak4QpfI/AAAAAAAABKw/BnKnGThCkVw/s1600-h/image%5B9%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Oy9HuksgrdU/S3N4a-SvWvI/AAAAAAAABK0/EpHt1R8INq4/image_thumb%5B5%5D.png?imgmax=800" width="819" height="592" /></a> </p> <p></p> <p>If for some reason, it does not work for you, try to add </p> <p><?xml version ="1.0"?> <br /><configuration> <br />  <startup useLegacyV2RuntimeActivationPolicy="true"> <br />     <supportedRuntime version="v2.0.5727" />  <br />    <supportedRuntime version="v4.0.21006"/> <br />  </startup> <br /></configuration></p> <p>to your csc.exe.config/msbuild.exe.config/VCSExpress.exe.config/devenv.exe.config …</p> <p>I initially thought it has something to do with not enabling Side-By-Side Execution of the  compiler and stuff but it turns out that it is not the case. For your information,<strong>  i have added supported runtime as .NET 2.0 <u>but then commented it to be sure that its ONLY the Target Framework that has to be changed.</u></strong></p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com2tag:blogger.com,1999:blog-19967453.post-73677135929918370942010-02-04T16:53:00.001-08:002010-02-04T16:53:08.243-08:00Breaking my head with message passing and Scala Actors<p>I recently started working on a personal project on which a friend of mine is helping. After some discussion, we thought Scala might be a good bet to use as the development platform of our choice (and we integrate Spring into Scala). Anyway for that, I got into Scala actors. Actors appear easy code – all you have to do is create an actor (if you use Actor.actor construct, it starts automatically, otherwise you have to invoke start) and from somewhere keep sending messages to the Actor.</p> <p>So let us first define what message that I want to send using Case Classes. Refer the documentation on case classes.</p> <pre name="code" class="scala"><br />case class Message(someData : String)<br /></pre><br /><br /><p>Now let us create our component which is an Actor and for each string passed in the constructor, we append the message (Some stupid behavior, but serves the example here)</p><br /><br /><pre name="code" class="scala"><br />class MyActor(toInform: Array[String]) extends scala.actors.Actor{<br /> private val noticeTo = toInform<br /> def act(){<br /> loop{<br /> react{<br /> case Message(r) =>{<br /> for(item<-noticeTo)<br /> println(item+"_"+r)<br /> }<br /> }<br /> }<br /> }<br />}<br /></pre><br /><br /><p>Now with the following code to test the above actor</p><br /><br /><pre name="code" class="scala"><br />val actor = new MyActor(Array("Krishna"))<br />actor ! "Welcome"<br /></pre><br /><br /><p>With all the excitement in the world, you run the test and it just hangs in there, nothing happens :). So you suddenly realize "YOU FORGOT TO START THE ACTOR". Damn! it was the actor.start that is causing the issue. So you cleverly add the actor.start statement. ("you" meaning "me")</p><br /><br /><pre name="code" class="scala"><br />val actor = new MyActor(Array("Krishna"))<br />actor.start //do not forget this :)<br />actor ! "Welcome"<br /></pre><br /><br /><p>You run the test again, to have no success. Then you realize that your actor expects Message(r) where as you are sending a String. So you change that</p><br /><br /><pre name="code" class="scala"><br />val actor = new MyActor(Array("Krishna"))<br />actor.start //do not forget this :)<br />actor ! Message("Welcome")<br />//actor.exit<br /></pre><br /><br /><p>Also notice the commented out explicit kill of the exit call outside the actor. It is not advised to be doing that. Remeber that "!" is a send-and-continue kinda call (asynchronous call). So before the actor is scheduled to work on the message, it might be killed. Instead it is advised that you specify an "Exit" like message within the case on the Actor. Now you run the test and it works :) Hurray!!!!</p><br /><br /><p> Later, smart-ass like you (this time, it is you :)), decide to write a very bad code (ok, you is not really you, for now lets assume the actor code i wrote is simply perfect) like shown below.</p><br /><br /><pre name="code" class="scala"><br />object Launcher extends Application{<br /> val actor = new MyActor(null)<br /> actor start() //do not forget this<br /> actor ! Message("Welcome")<br /> actor ! "E" //this is the exit message.<br />}<br /></pre><br /><br /><p>You repeat the test again :) and this time it does nothing, it appears to be blocked :). Again....damn... so how would we know what the issue is? You spend a your day-off trying to figure out what is wrong with this simple code...then after spending 10 hours trying all the magic tricks (aparently, i know too many magic tricks that never work, hence the time), you realize the actor must be dead (netbeans threads view would show what threads are running and you never see any FJ Threads). So the actor died! So what can kill an actor? - call to exit(), or an exception!!!! There it is .. so let us change the actor code so that it can catch the exception.</p><br /><br /><pre name="code" class="scala"><br />class MyActor(toInform: Array[String]) extends scala.actors.Actor{<br /> private val noticeTo = toInform<br /> def act(){<br /> loop{<br /> react{<br /> case Message(r) =>{<br /> try{<br /> for(item<-noticeTo)<br /> println(item+"_"+r)<br /> }catch{<br /> case e => e.printStackTrace<br /> }<br /> }<br /> case "E" => exit<br /> }<br /> }<br /> }<br />}<br /></pre><br /><br /><p>Run the test again and notice the stacktrace! </p><br /><br /><pre name="code" class="scala"><br />java.lang.NullPointerException<br /> at scala.collection.mutable.ArrayOps$ofRef.length(ArrayOps.scala:68)<br /> at scala.collection.IndexedSeqLike$class.foreach(IndexedSeqLike.scala:86)<br /> at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:20)<br /> at tryscala.MyActor$$anonfun$act$1$$anonfun$apply$1.apply(Launcher.scala:13)<br /> at tryscala.MyActor$$anonfun$act$1$$anonfun$apply$1.apply(Launcher.scala:10)<br /> at scala.actors.Reaction$$anonfun$$init$$1.apply(Reaction.scala:33)<br /> at scala.actors.Reaction$$anonfun$$init$$1.apply(Reaction.scala:29)<br /> at scala.actors.ReactorTask.run(ReactorTask.scala:33)<br /> at scala.actors.scheduler.ForkJoinScheduler$$anon$1.compute(ForkJoinScheduler.scala:111)<br /> at scala.concurrent.forkjoin.RecursiveAction.exec(RecursiveAction.java:147)<br /> at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)<br /> at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.java:340)<br /> at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:325)<br />BUILD SUCCESSFUL (total time: 7 seconds)<br /></pre><br /><br /><p>So the fix is to :) pass in an empty array at least, instead of sending in a null ( use Array.empty).</p><br /><br /><p>Clearly, this is not the exact sample that I was running, it was a little bit complicated and myself being totally new to Scala had a hard time trying to figure out that an exception can kill scala Actors!!! I know I am dumb, but with this post, I want to save you from considering yourself dumb after trying very hard for a few hours.</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com1tag:blogger.com,1999:blog-19967453.post-71616516671389773122010-01-26T15:56:00.001-08:002010-01-26T15:56:30.513-08:00Dependency Injection in Scala and some other stuff<p>My new  found love is the Java Spring framework. I initially looked at the SpringFramework for .NET and thought it was really good and well designed. But then for a pet project of mine, my friend promised to help me given the project would be on the JVM. So we decided that we shall consider Scala as the language of choice on the JVM. Now, I know how I can do DI using Spring Container in Java. So I was wondering if I could do the same in Scala. Turns out, it is not so difficult after all (so far).</p> <p>For my scala development environment, I played with Eclipse, IntelliJ Community Edition and then Netbeans. So far, Netbeans has the best support for Scala in the form of IDE integration. There are some issues with it but then it is not all too bad. One initial issue was that even though the Spring JAR files were in my class path, the netbeans editor complained that it could locate the Spring Jars. The error message was something like “the value springframework is not a member of the package org”. Turns out, if you clean and build the project, it would build successfully but the editor keeps complaining. The Netbeans wiki talked about having some “Reset Scala Parser” item on the context menu but I am so dumb that I still could not find it. So if there is no reset trigger then how about we close and start the IDE. And it works! For now, since I am using nightly builds (follow the wiki link here <a href="http://wiki.netbeans.org/Scala68v1">http://wiki.netbeans.org/Scala68v1</a>) I would not mind these minor issues. But let me tell you, I spent a whole evening trying to figure out which is the best IDE for latest Scala and the winner is Netbeans, without any question. </p> <p>Now at the time I created my sample project, I made sure that I check the settings in Netbeans project creation wizard which allows me to copy all my libraries into a common folder such that it would be easier when working as a team. Then I added the following files from the Spring Distribution.</p> <p><a href="http://lh6.ggpht.com/_Oy9HuksgrdU/S1-BLLaqxvI/AAAAAAAABKg/dH3SOpNgizA/s1600-h/image%5B3%5D.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_Oy9HuksgrdU/S1-BLSpfZaI/AAAAAAAABKk/Y2njS3PDl0U/image_thumb%5B1%5D.png?imgmax=800" width="644" height="251" /></a> </p> <p>Apart from these, I added commons-logging jar too.</p> <p>Following code shows how you can use Spring within scala. So lets say I have an interface Sample. (which are almost traits in Scala). And I have a concrete implementation ScalaImpl. Follow the code carefully and with some fundamental understanding of Spring  framework, you should be all set.</p> <pre name="code" class="scala"><br />/** Main.scala **/<br />package tryscala<br />import org.springframework.context.support.FileSystemXmlApplicationContext<br /><br />object Main {<br /><br /> /**<br /> * @param args the command line arguments<br /> */<br /> def main(args: Array[String]): Unit = {<br /> val fs = new FileSystemXmlApplicationContext("Configs\\Spring.xml")<br /> val sample = fs.getBean(classOf[Sample]).asInstanceOf[Sample]<br /> println(sample sayHello) <br /> }<br />}<br /><br />/** Sample.scala **/<br />package tryscala<br /><br />trait Sample {<br /> def sayHello(): String<br />}<br /><br />/** SampleImpl.scala"<br />package tryscala<br /><br />class SampleImpl(var firstName:String) extends Sample {<br /> def sayHello() = "Welcome to Scala : " + firstName<br />}<br /></pre><br /><br /><br/><br /><br /><p> The spring configuration file (Configs\\Spring.xml) is shown below. </p><br /><br /><pre name="code" class="xml"><br /><?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><br /> <bean id="sample" class="tryscala.SampleImpl"><br /> <constructor-arg name="firstName" value="Krishna Vangapandu"/><br /> </bean><br /></beans><br /></pre><br /><br /><p> I hope to be able to share more about what we are doing and how Scala effects us, as we move on. </p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com0tag:blogger.com,1999:blog-19967453.post-88277923355421758732010-01-14T18:53:00.001-08:002010-07-11T21:51:19.480-07:00Rhino Mocks : How to mock read-only properties.<p>As part of my never-ending quest to do something big, I started working on an application for which I am learning to use Rhino Mocks and I thought it would make a small and easy example to understand mocks. I have not particularly followed TDD approach so far, but anyway here is my attempt to show how you would use Rhino Mocks to stub/mock (am yet to understand when to use what…I mean i know the difference … just did not develop enough maturity in that aspect).</p> <p>Let us say I have some interface which is as simple as shown below.</p> <br /><pre name="code" class="csharp"><br />public interface IColumn {<br /> public string Name { get; }<br /> public string DataType { get; }<br />}<br /></pre><br /><br /><p> Now if I were to mock/stub this interface, I had some problems with Rhino Mocks throwing exceptions on how the property should have a getter/setter when I was trying to configure the mock such that whenever Name property is accessed you should return "Krishna Bhargava" and for DataType you should return "System.String". I tried different mechanims like using Expect.On(col).Call(it.Name).Return("Krishna Bhargava") or tried to define property behavior (PropertyBehavior()). Finally after struggling for an hour, looking at various examples online from good people like you and me, I was able to come up with proper code that can generate a stub for the column defined in this interface. The code snippet shows how you can use Rhino Mocks (which i think the most convenient framework - i tried Moq (somehow it does not click with me), NUnit.Mocks( too much ground work)) so that u can generate stubs for read-only properties! Unfortunately at the moment, I cannot comment much on the Rhino Mocks classes .. I hope the code snippet is easy to understand.</p><br /><br /><pre name="code" class="csharp"><br />private IColumn MockColumn(string name, string type)<br />{<br /> IColumn col = MockRepository.GenerateStub<IColumn>();<br /> col.Stub(it => it.DataType).Return(Type.GetType(type));<br /> col.Stub(it => it.Name).Return(name); <br /> return col;<br />}<br /></pre><br /><br /><p> you can later use this method as shown below </p><br /><br /><pre name="code" class="csharp"><br />[Test]<br />public void ColumnStubNameCanBeSet()<br />{<br /> IColumn col = MockColumn("name", "String.String");<br /> Assert.AreEqual("name",col.Name);<br /> Assert.AreEqual(typeof(string), col.DataType);<br />}<br /><br />/*a little bit more real world usage of a column stub is shown below. */<br />[Test]<br />public void ValuesCanBeSetOnARecord()<br />{<br /> IRecord record = new Record(); //NOT DESCRIBED in this blog, but this is my personal class....<br /> record.SetValue(MockColumn("name", "System.String"), "Krishna Vangapandu");<br /> record.SetValue(MockColumn("age", "System.Int32"), 25);<br /> Assert.AreEqual("Krishna Vangapandu",record.Value<string>("name"));<br /> Assert.AreEqual(25, record.Value<int>("age"));<br />}<br /></pre><br /><br /><p><br />And by the way, Castle Windsor has the best and easiest configuration schema in all the IoC containers I played with. I personally like Unity Container but its configuration is a mess and over-engineered. I will try to post an implementation of Observer-Pattern where observers and observables are linked with pure configuration - the best use I made of IoC container so far!<br /></p>Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com1tag:blogger.com,1999:blog-19967453.post-44816002580639390752009-11-21T11:06:00.001-08:002009-11-21T11:07:44.145-08:00Programming in Scala – Part 2/?<p>In my <a href="http://krishnabhargav.blogspot.com/2009/11/scala-for-dummies-like-me.html" target="_blank">previous post</a>, we got started with simple Scala HelloWorld and moved on to write a bubble sort in Scala. This time let us look at some differences between a “var” and a “val” in scala. I hope you all know what “immutable” means – simply put Strings in Java/.NET are immutable. Anytime you modify a string, a new object of string is created – they cannot be changed in place. Well, in scala when you declare a variable with a “val” it would be immutable. Look at the following scala code.</p> <pre class="scala" name="code">object ValVar<br />{<br /> def main(args: Array[String])<br /> {<br /> val immutableValue = 200<br /> //immutableValue = 20 -> gives compiler error<br /> var mutableValue = 200<br /> mutableValue = 20<br /> println("Immutable : "+immutableValue+"\n Mutable : "+mutableValue)<br /> }<br />}</pre><br /><br /><p>The decompiled program would look like shown in Java</p><br /><br /><pre class="java" name="code">import java.rmi.RemoteException;<br />import scala.Predef.;<br />import scala.ScalaObject;<br />import scala.ScalaObject.class;<br />import scala.StringBuilder;<br />import scala.runtime.BoxesRunTime;<br /><br />public final class ValVar$<br /> implements ScalaObject<br />{<br /> public static final MODULE$;<br /><br /> static<br /> {<br /> new ();<br /> }<br /><br /> public ValVar$()<br /> {<br /> MODULE$ = this;<br /> }<br /><br /> public void main(String[] args) {<br /> int immutableValue = 200;<br /><br /> int mutableValue = 200;<br /> mutableValue = 20;<br /> Predef..MODULE$.println(new StringBuilder().append("Immutable : ").<br /> append(BoxesRunTime.boxToInteger(immutableValue)).<br /> append("\n Mutable : ").<br /> append(BoxesRunTime.boxToInteger(mutableValue)).toString());<br /> }<br /><br /> public int $tag()<br /> throws RemoteException<br /> {<br /> return ScalaObject.class.$tag(this);<br /> }<br />}</pre><br /><br /><p>Clearly, it does not look like "immutable" variables are declared final in the Java code. So it appears to me that Scala compiler does the job of making sure that the val'bles are immutable. For those curios to see what happens if you attempt to change a val'ble, see the screenshot below. Also it would be interesting to note that the scala compiler takes care of optimizing our string concatenation to make use of String builder, just like the javac!</p><br /><br /><p><a href="http://lh6.ggpht.com/_Oy9HuksgrdU/Swg6JOQF4QI/AAAAAAAABHw/c8uTmVZJDZE/s1600-h/image%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_Oy9HuksgrdU/Swg6JYM8W6I/AAAAAAAABH0/s4SRPw6DWRE/image_thumb%5B1%5D.png?imgmax=800" width="436" height="75" /></a></p><br /><br /><p><b>So what did we learn?</b>: If you wish to keep changing the values for a variable, then use "var" and if you want immutable variables, use "var".</p><br /><br /><p>Now that we know how to create both variables and val’bles, let us look at some fancy stuff that we could do with lists.</p><br /><br /><p><strong>Whats your range?</strong></p><br /><br /><p>Let us say, we need all odd numbers between 20 and 2000 which are divisible by both 5 and 7. If you were like me, we write the program to look like shown below.</p><br /><br /><pre class="scala" name="code">object RangeAction1<br />{<br /> def main(args: Array[String])<br /> {<br /> for(i <- 20 to 2000)<br /> {<br /> if( i % 5 == 0 && i % 7 == 0)<br /> println(i)<br /> }<br /> }<br />}</pre><br /><br /><p>Can we do any better? This looks too long now that I have been imagining things, increasing expectations about scala being so nice.</p><br /><br /><pre class="scala" name="code">object RangeAction1<br />{<br /> def main(args: Array[String])<br /> {<br /> (20 to 2000).filter(i=>i%5==0&&i%7==0).foreach(i=>println(i))<br /> }<br />}</pre><br /><br /><p>When we say “20 to 2000”, it returns a <a href="http://www.scala-lang.org/docu/files/api/scala/Range.html" target="_blank">Range</a> object. Look in the documentation to see what all magic could we do with range. Similarly if we were to work with lists, we could do something similar. Now to add 1 cent to the 3 cents we covered so far, what if i want the range to start with 20 and end with 2000 but increment by 10 and exclusive of 2000.</p><br /><br /><pre name="code" class="scala"><br />(20 until 2000 by 10).filter{i=> i % 5 == 0 & i % 7 == 0}.foreach{i=> println(i)}<br /></pre><br /><br /><p>Also, I wanted to be more like a regular programmer and put my closure inside {} instead of (). More fun later!</p> Krishna Bhargava Vangapanduhttp://www.blogger.com/profile/16813193547125152985noreply@blogger.com1