Thursday, December 26, 2013

Neo4J user service plugin for secure social (play framework/scala)

SecureSocial-Neo4JUserService

Neo4J user service plugin for secure social (play framework/scala)

Project on GitHub: SecureSocial-Neo4JUserService


Background

Building reactive web applications with play framework and scala often starts with a social login system. One great way to get started here is to use Secure Social. They provide a simple way to get up and running with a large number of providers. Once you are running you will need to store your account information and more the likely the relationships between your social users "friends". This is where Neo4J really shines.

Requirements

Setup

Once you have secure social up and running all you need to do is add this scala file to your play project. I created a app/servicies directory to place the file. Next you simply need to add a line to your play.plugins

play.plugins

9998:service.Neo4JUserService

Neo4J Structure

Users will now be added to your neo4J with the following structure

(u:User)-[:HAS_ACCOUNT]->(p:Provider)

You can also use the utility methods outlined below to make users friends

(u1:User)-[:FRIEND]->(u2:User)

Helper methods

Here are number of usefull methods for helping you to work with secure social and Neo4J

object  Neo4JUserService{
  def socialUserFromMap( umap: Map[String, String]): Identity = {
    ...
  }

  def makeUsersFriends( uuid1: String, uuid2: String ) = future{
    ...
  }

  def socialUserFromToken(provider: String, token: String) = future{
    ...
  }

  def uuidFromProviderInfo( provider: String, id: String, name: String = "" ) = future {
    ...
  }


  private def createUserAndProvider(name: String, id: String, fullname: String) = {
    ...
  }
}

Things to consider.

You will still need a way for users to "link" multiple providers on your back-end. Currently if a user signs in using another provider, they will get another user and provider record in Neo4J. You could try to combat this at the login level by looking for emails that are same as other providers (you would want to verify the email before linking for security reasons)

Another way would be a settings section on your site when a user is loged in, that would allow them to "link" their other accounts. In this manor you would want to create to continue to build a structure like the following

        /[:HAS_ACCOUNT]->(p0:Provider)
(u:User)-[:HAS_ACCOUNT]->(p1:Provider)
        \[:HAS_ACCOUNT]->(p2:Provider)

Tuesday, December 10, 2013

Base3d a simple c++ library

Base3d lib C++

GitHub https://github.com/coreyauger/cpp_base3d_lib

Overview

This is a small and simple base 3d library written in C++. You would be crazy to write your own 3d library in todays world… However there are still a number of applications for using a compact 3d library to learn from or implement application specific problems.

This code was written a while back but used in a number of 3d applications during my time in University. I have decided to share it in the hopes that someone could find it as useful as I once did.

Example projects

Collision Detection Project

This project I designed a large "ball" course that I let serve as a demonstration of the physics I wrote. This was a super fun project and ended up working very well.

demo 1

Water Simulation

This was a really fun project to work on. The main idea for the movement of the water came from one of my favorite sites on the net: The good-looking textured light-sourced bouncy fun smart and stretchy page

demo 1

Radiosity

Dividing each face of our scene into light-map texel regions, we are going to perform a gathering type of radiosity solution. First by using GL to render the scene from the perspective of a texel into a hemi-cubed texture map, we solve the visibility problem. More info here

demo 1

Monday, December 9, 2013

FLV Socket Writer Direct Show Filter

flv socket filter

Project on GitHub: https://github.com/coreyauger/flv_socket_filter

Windows Direct Show filter. Stream FLV and header information to any connecting application. Takes care of late joiners by resending h264/aac headers.

Overview

FLV files consist of a single header, and metadata, chunk followed by N interleaved audio and video chunks.
Most streaming filter implement RTMP and provide a single client with the ability to connect and start injesting a stream.
This filter provides a much more flexible and free form way of Interprocess Communication with you data.

  • Supports M number of simultaneous connection steams to FLV
  • Supports "late joiners" headers(Meta,Head) + AAC/H264 packets are retransmitted to new connections
  • Actionscript ByteStream clients will receive data on chunk boundaries for easy parsing.
  • Simple to right stream out to file on disk at the same time as other operations.

Requirements

The only requirement are the direct show base classes. Sockets are winsock implementation.

Usage

Currently the filter has the listening port hardcoded. You could add this as an option to the filter option page if you so desire. Once this filter is up and running you can connect to it via the port number and start to injesting your FLV.

Example

Here is an example client written in C# that will connect and write the FLV to disk.

This example uses the FlvStream2FileWriter that is also an open source project I have on GIT here: (https://github.com/coreyauger/flv-streamer-2-file)[https://github.com/coreyauger/flv-streamer-2-file]

This takes care of inseting the new timing information into the FLV so that your file on disk will allow seek operations.

internal void SaveStreamToFile(string path)
        {
            const int count = 4096;
            byte[] buffer = new byte[count];

            // Declare the callback.  Need to do that so that
            // the closure captures it.

            AsyncCallback callback = null;

            System.IO.File.Delete(path);
            FlvStream2FileWriter stream2File = new FlvStream2FileWriter(path);

            // Assign the callback.
            callback = ar =>
            {
                try
                {
                    // Call EndRead.
                    int bytesRead = _clientStream.GetStream().EndRead(ar);

                    // Process the bytes here.
                    if (bytesRead != buffer.Length)
                    {
                        stream2File.Write(buffer.Take(bytesRead).ToArray());
                    }
                    else
                    {
                        stream2File.Write(buffer);
                    }

                    // Determine if you want to read again.  If not, return.                
                    // Read again.  This callback will be called again.
                    if (!_clientStream.Connected)
                    {
                        throw new Exception("Just catch and exec finallize");
                    }
                    _clientStream.GetStream().BeginRead(buffer, 0, count, callback, null);
                }
                catch
                {   // most likly discontinuity ...just fall through                   
                    stream2File.FinallizeFile();
                    return;
                }               
            };
            _clientStream = new TcpClient("127.0.0.1", 22822);  // (CA) TODO: make port configurable on the filter and here
            // Trigger the initial read.
            _clientStream.GetStream().BeginRead(buffer, 0, count, callback, null);
        }

Wednesday, December 4, 2013

Blue Tooth Low Energy Scanner for Android written in scala

android-scala-ble

Blue Tooth Low Energy Scanner for Android written in scala

Overview

This project serves to wrap up some common functionality regarding scanning for a BLE sensor device.
Right now the code is simple and to the point. Install a filter (device name, mac address) and then start scanning for devices.

Features

I will try to keep adding to this list as I go. For now here is a short list of features:

  • Scan for a BLE sensor devices
    • Filter device list
  • Callback function to report device and signal strength

Example Usage

class MainActivity extends Activity with BleDeviceScanner{
  // Notice the "with BleDeviceScenner"
  // ...

  @Override
    protected override def onCreate(savedInstanceState: Bundle) = {
      // ... normal android init
      initBleScanner(this)

      val bscan = findViewById( R.id.bscan ).asInstanceOf[Button]
        bscan.setOnClickListener(new View.OnClickListener {
          override def onClick(v: View) = {         
             val filter = {
                d: BluetoothDevice =>
                    d.getName() != null     // You could filter by device name or address here..                                    
              }
              startScanWithFilter(filter){
                di: BleDeviceInfo =>  // This ia a callback with the located device 
                  Log.d(TAG,"Found device[%s] with signal stregth: %s".format(di.getBluetoothDevice.getAddress, di.getRssi) )
              }             
          }
        })

    }

}

Example Projects

Was used in a hackathon to try to do accurate indoor positioning. We ended up having to make a ton of modifications since the TI Sensor Tag can not be used to accuratly interpolate position.

Here is a screen shot TODO://

Monday, December 2, 2013

Create better native UI with Awesomium

awesomium.ui



Source on Github

Create amazing UI the way it should be done... with awesomium.ui.
This is a starter kit for the Awesomium browser based UI and WInform.

Dependencies

Awesomium http://awesomium.com.

Overview

Creating Web UI is easy and fun. With the enormous amount of power built into a modern HTML5 browser, creating fluid rich UI is a breeze.

This is not true when creating Native Application UI. The process is plagued with subtleties that slowly crush developers spirits and ultimately produce projects that take more time and do far less.

Fortunately for us there are ways to bridge the gap between web and native. One of those project is http://awesomium.com/. Awesomium gives you C# wrappers to an underlying Chromium based browser. What does these mean for developers….

All the great features like:

  • CSS3
  • Canvas
  • SVG
  • WebGL
  • WebRTC
  • WebSockets
  • And much more …

You can even run web plugins… think Flash or Unity. Side Note: Awesomium has a Unity3d plugin for Mac/Windows game builds.. also very cool.

Inspiration

Game companies have been doing this for years. Both “Stream” and “Origin” are native browser based UI. The goal with my project is to provide some simple extensions to the Awesomium based libraries that get you up and running fast. Also to provide a good looking UI example … Similar to “Steam” and “Origin”

UI 1

Usage

I have provided a demo project that you can refrence to get you up and running. The basic idea is this:

  • Create a Native Winform project for you solution.
  • Reference

    • Awesomium.Core.dll
    • Awesomium.Windows.Forms.dll
    • awesomium.ui
  • Create a Winform. In my example project I used the default “Forms1.cs”

  • Open the Form1.cs and replace the inherited base class from “Form” to my base class “awesomium.ui.FromBase”
  • Now create a directory in your project called “.appui”

  • Create an html page inside “.appui” directory that has the same name as your Winform. Inside my demo this file was “Form1.html” Make sure to change the “Build Action” for all your html/css/img content to “Content” and “Copy if newer”. This will move the html to your output folder when you build your project.

  • Compile and run the project and you will now have amazing looking UI that has limitless possibilities.

Communication

Communicating with your UI layer is a breeze. This is all done through javascript and json. There are 2 directions for your communication

HTML UI to Native C-Sharp

Inside you javascript create a call like so

ScriptInterface.call('JSMyMethod', 'arg1','arg2');

The first argument is the name of the method in your C# to call. All additional argument are arguments to that function.

On your C# side you would define a method like so:

public void JSMyMethod(string a, string b){
    // do stuff..
}.

C# to HTML javascript

simply use the interface

base.Webbrowser.ExecuteJavascript( "javascript to execute in here" );

Demo Project

UI 2

Sunday, December 1, 2013

DirectShow AudioLoopbackFilter

Source code on Github

AudioLoopbackFilter

Directshow Audio Loopback Capture Filter

Dependencies

Microsoft SDK (Direct Show base classes) http://www.microsoft.com/en-ca/download/details.aspx?id=8279

Overview

This project provides an efficient working audio capture source filter. A “what you hear is what you get”. The code is based on Microsofts IAudioCaptureClient running in “loopback” mode. A feature that has been added since Windows Vista.

Past Work

This code is based on the initial work done by “Maurits” at Microsoft. His work and all the comments containing some of the issues with the code are located here.

Additionally “Roger Pack” wrote the original directshow filter wrapper for Maurits code. Roger’s work can be found here: https://github.com/rdp/virtual-audio-capture-grabber-device. I found that in Windows 8 the filter no longer worked on this project. I also submit a patch that fixed the audio in windows 8.. but the filter graph would pause if there was silence (no audio).

The main issue with these works, is the inability to generate “silence”. This problem is magnified in directshow by not allowing your filter graph to process any video unless there is valid audio (ie silence will pause your graph).

My Solution

I based my filter off the Synth Audio Source, from the directshow example projects. I have removed most of the source that was not needed for my filter to make it easier to understand.

There are a few major differences in my filter from the one I was using before.

  • I generate silence values into the buffer when there is “No” data available from the AudioCaptureClient. This will allow the filter graph to continue to process video frames/mux even if there is no audio.

  • I sync the Reference clock to use the clock time specified by the AudioCaptureClient. This eliminates the usage of the extra threading and BAD sleep statements in the code. The result is a significantly lighter CPU footprint.

Finally

My solution has only undergone a very small amount of testing. Which did include both a Windows 7 and Windows 8 machine. However, if you have any trouble with the filter please contact me and I will be glad to help. Thanks.

Friday, November 29, 2013

Zero to Play with Linux Neo4j, Play2 and nginx

This is a guide for taking your (debian like) linux system from a fresh install to staging server.

sudo -s
apt-get update
apt-get dist-upgrade

# install Sun java 7
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
apt-get update
apt-get install oracle-java7-installer

# install scala
apt-get install scala

# build tools
apt-get install build-essential

# install recent nodejs
sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:richarvey/nodejs #or sudo apt-get-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs nodejs-dev npm

# install nginx
apt-get install nginx


# get play
wget http://downloads.typesafe.com/play/2.2.1/play-2.2.1.zip

# unzip this somewhere
vim .bashrc

# add play to path
export PATH=$PATH:/path/to/play

# install neo4j

# start root shell
sudo -s
# Import our signing key
wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add - 
# Create an Apt sources.list file
echo 'deb http://debian.neo4j.org/repo testing/' > /etc/apt/sources.list.d/neo4j.list
# Find out about the files in our repository
apt-get update
# Install Neo4j, community edition
apt-get install neo4j
# start neo4j server, available at http://localhost:7474 of the target machine
/var/lib/neo4j/bin/neo4j start


# install git
apt-get install git

# clone projects
...
...

# make sure things are good
cd projects/dir/
play compile

# test running the server
play run

# compile staging package
play clean compile stage


# setup your init scripts to keep it alive
vim /etc/init.d/play.myapp

###################################################
## Init Script
###################################################
#!/bin/bash

APPLICATION_PATH=/home/user/projects/myplayapp

start() {
    echo -n "Starting"
    sudo start-stop-daemon --start --background --pidfile ${APPLICATION_PATH}/RUNNING_PID -d ${APPLICATION_PATH} --exec target/start -- -Dhttp.port=9000
    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}
stop() {
    echo -n "Stopping"
    sudo start-stop-daemon --stop --pidfile ${APPLICATION_PATH}/RUNNING_PID

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}

  case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: play-server {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL



## END INIT



#make it executable 
chmod +x /etc/init.g/play.myapp

# test it
/etc/init.d/play.myapp start

# now make it start on boot
update-rc.d play.myapp defaults


# finally lets get some proxy action
# edit nginx.conf adding servers for each of your projects...
vim /etc/nginx/nginx.conf


######### SAMPLE SECTION TO ADD in http{ ...
# add 1 sever per domain. Also note that you can have wildcards *
 server {
        listen       80;
        server_name  *.mysite.com;
               
        location / {
           proxy_pass http://localhost:9000/;
           proxy_set_header Host $http_host;
        }
# 404 ect...
}

# now restart nginx
/etc/init.d/nginx restart


Lets assume you had a number of sites on one server. You could have a number of play applications running on various ports. Then in nginx.conf you would define a sever rule for each site. Also MAKE sure you pass through the Host header or you will run into issues. (ex: secure_socaiul sending redirect urls as "localhost:9000"

Wednesday, November 27, 2013

Nginx as a proxy passthrough for BOSH

Nginx as a proxy passthrough for BOSH

In a past post I talked about using nodejs and http-proxy as a way of routing your request to internal handlers. One thing that we want to do when working with XMPP from the web is to proxy request to a BOSH server running on another port. There are issues that arise here from Cross Origin Scripting or COORS. Basically if (stophe.js) does not make the BOSH request from the same domain and port, it will fail.

To get around this I added another rule into my nodejs routes. The rule looked something like the following.

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server
//

var options = {
  router: {
    'domain.com/http-bind/': 'localhost:7070/http-bind/',
    'domain.com' : 'localhost:9000',    
  }
};

httpProxy.createServer(options).listen(80);


This did not work. I figure it has something to do with not passing through the correct host header.. thus still running into a CORS issue.

Solution Nginx

Download and install nginx What was required was to setup nginx to allow proxy-passthrough to BOSH. You need to edit your nginx.conf file and include something similar to the following.

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
  location /http-bind/ {
   proxy_pass http://localhost:7070/http-bind/;
  }

        location / {
    proxy_pass http://localhost:9000/;
    proxy_set_header Host $http_host;
  }
}


In this case we are taking traffic from port 80 and either mapping it to our Play Server (port 9000) or to the openfire BOSH connection (http://localhost:7070/http-bind/). This worked :)

Friday, November 22, 2013

Neo4J Membership Provider



Neo4JMembershipProvider

asp.net Neo4J Membership Provider

Dependencies

Neo4J Graph Database >= v 2.0.0 (Make sure it has label support)
Neo4JClient https://www.nuget.org/packages/Neo4jClient. NOTE: this package is auto installed as a dependency when you use NuGet

Install

NuGet package https://www.nuget.org/packages/Neo4JMembershipProvider/
This is a step by step install for a new MVC application.
First thing to do is to make sure that you have the latest NuGet installed in your Visual Studio. Again, make sure it is the latest version.
ScreenShot
At this point make sure that you have Neo4J up and running and that you can connect to it. Assuming you have Neo4J installed on your localhost you should be able to connect via your web browser with the address.
http://localhost:7474
If you have everything up and running you should see a screen similar to this one. ScreenShot
Create a new MVC 3/4 web application. Once you are looking at the project files, right click on your refrences and choose "Manage NuGet Packages.."
This will open the NuGet Modal. Select "Online" from your list of sources on the left hand side. Next you can search for "Neo4JMembershipProvider" in the top right search menu.
Once you see the package in the main list. Click the install button.
Verify that the package installed by looking for a green checkmark next to the package. See image as refrence ScreenShot
Now we have to make a few changes to the configuration of our application.
First off we will need to modify our web.config file to include the following lines
<configuration>

  ...

 <connectionStrings>
    <add name="DefaultConnection" connectionString="http://localhost:7474/db/data" providerName="Nextwave.Neo4J.Connector.Neo4JClient" />
  </connectionStrings>
  <appSettings>

    ...

    <add key="enableSimpleMembership" value="false" />
    <add key="autoFormsAuthentication" value="false" />
  </appSettings>
  <system.web>

    ...

    <roleManager enabled="true" />
    <machineKey validationKey="C50B3C89CB21F4F1422FE158A5B42D0E8DB8CB6CDA1742572A48722401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD22AF6979E7D20198CFEA50DD3D3799C77AF2B722" validation="SHA1" />
    <membership defaultProvider="Neo4JMembershipProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear />
        <add name="Neo4JMembershipProvider" type="Nextwave.Neo4J.Membership.Neo4JMembershipProvider" connectionStringName="DefaultConnection" applicationName="Nextwave" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" />
      </providers>
    </membership>

    ...

</configuration>
I have also included an image with the highlighted changes.
NOTE: that your connection string may need to change to point to your db location ScreenShot
The last thing you need to do is to modify your "InitializeSimpleMembershipAttribute.cs" This is located in your /filters/InitializeSimpleMembershipAttribute.cs
Make the following code changes
  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
  {
      private static SimpleMembershipInitializer _initializer;
      private static object _initializerLock = new object();
      private static bool _isInitialized;

      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          // Ensure ASP.NET Simple Membership is initialized only once per app start
          LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
      }

      private class SimpleMembershipInitializer
      {
          public SimpleMembershipInitializer()
          {
              try
              {
                  WebSecurity.InitializeDatabaseConnection("DefaultConnection", "User", "Id", "UserName", autoCreateTables: false);
              }
              catch (Exception ex)
              {
                  throw new InvalidOperationException("Something is wrong", ex);
              }
          }
      }
  }
That should be all you need.
You will now be able to start your application and regester / login
Your users will be nodes in neo4J with the "User" label.
So for example you could list all your users with the following Cypher
MATCH u:User RETURN u;

Finally

My solution has only undergone a very small amount of testing.
However, if you have any trouble please contact me and I will be glad to help.
Thanks... and Enjoy the power of your new graph db :)

Thursday, November 21, 2013

AngelHack Vancouver and Bluetooth Low Energy



AngelHack Vancouver and Bluetooth Low Energy

Event: details

This last weekend I participated at the AngelHack hackathon. What is this?  Teams got to pitch their idea and collect members that were interested in the same thing. You then had 24 hours to complete/hack your project and present it to the group.


The idea I wanted to work on included the use of a TI Sensor tag. This is similar to apples iBeacon technology and uses BlueTooth 4.0 Low Energy. The low energy means that the devices work for low range, but also that some will operate for over 2 years on a coin battery. We intended to use a number of TI Sensor tags and an android device to get accurate indoor positional data. Once we had an accurate position and an orientation for the device we planned on allowing you to interrogate your environment.  This environment interrogation would take form as a kind of Augmented Reality looking through the devices camera.  I had imagined the terminators red view of the world with constant printouts of scanned objects ;)

The plan for getting the BLE data was to fix the sensor tags to known coordinates (Lat, Lng) and then measure the field strength of the tag to the device.  Using a number of field strength values we could then interpolate to get an accurate position for the device.  THIS DID NOT WORK :(

As it turns out the TI Sensor tag is incapable of providing data that can be used in this manor.  About the only predictable use of the field strength was to determine if you were really closet to the device (like 10cm close).  This was a major letdown and it took us well into the hack to realize that we were going to be unable to use the devices in this manor.





TI Sensor Tag

I have now ordered the "estimote" beacons, with the hopes that you will work with the above idea. I have already had a few reports that they are higher powered and should not suffer from the same shortcomings. Here is a link to the product http://estimote.com

But like any good hacker you adapt and move on.  We changed the idea from trying to position a moving target to more of a check-in system.  By that I mean you would bring your device right up to the tag.. once we knew you were really close to the device we assigned you the same position as the fixed tag.  From here we displayed your location and the locations of "points of interest".


Once you know there are points of interest you can switch into the AR mode that we have and view information about the objects right on top of the camera.  You could imagine that you have entire product schema that you could drill down into to learn more, or even overlay video or virtual message boards that people could write on ect.

In the end the project lacked the right visuals to really impress the crowd.  However the knowledge gained was really valuable and I will be moving forward with this in the future.

Links to some source code on github

android-scala-ble - this is the scanning code required for android to locate devices and their signal strength.

android-scala-gl - OpenGL ES 2 library to help drawing basic shapes and sprites for the Augmented Reality portion of the project.

Wednesday, November 20, 2013

Introduction to Currying in Scala

Scala and functional programming

I have recently moved to programming in Scala. I started with 1 book and have now completed Functional Programming Principles in Scala , a free course on coursera. I am also part way into the next course on Principles of Reactive Programming.So far I have been having a ton of fun, and been blown away at the power of functional programming. I wanted to share some notes that helped me to understand some of the topics that I have studied.

Introduction to Currying in Scala


What is Currying
Currying is named after "Haskell Brooks Curry" a mathematician and logician.
Looking at A Tour of Scala: Currying: "Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments."
Currying is a way of applying partial functions in an effort to make your code operate or appear more like built in language constructs thus making it more readable.

Here is some text from the book "Programming in Scala: A Comprehensive Step-by-Step Guide"
Currying allows you to make new control abstractions that feel like native language.... and, A curried function is applied to multiple arguments lists instead of just one.

Partial Functions
To better understand Currying we must take a look at partial functions. Functions in Scala and other functional languages are first class objects. These objects are generated for you by the compiler and contain a method apply. When you call a function

def f = {}
f()        // you are actually calling f.apply()

One of the things we can do with a function that contains a number of arguments is to supply "some" of the arguments. The compiler will again generate a function for you but this time it will also generate a wrapper with the partially applied values. Lets take a look at this in a Scala worksheet.

def mul3( a: Int, b: Int, c:Int ): Int = {
 a * b * c
}                                         // mul3: (a: Int, b: Int, c: Int)Int
mul3( 2, 2, 2)                            // res0: Int = 8

val partial = mul3( 5, _: Int , 5 )       // partial  : Int => Int = >function1<

Note that the compiler gave us a >function1<. This is a function that takes a single parameter, in our case the middle parameter to the original function that we left as a place holder. What was actually generated would be something similar to the following.

def f1( b: Int ): Int = {
   mul3(5, b, 5)
}

Currying
So now that we have some background lets take a look at a curried function. We will write a function prod and then show the equivalent curried function curriedProd

def prod( a: Int, b: Int) = { a * b }     //> prod: (a: Int, b: Int)Int

def curriedProd(a: Int)( b: Int) = { a * b }
                                                  //> curriedProd: (a: Int)(b: Int)Int

When you envoke the curried function you actually get another function with one of the arguments applied to it. This can be seen by supplying a place holder to the curried function as follows

val timesThree = curriedProd(3)_          //> timesThree  : Int => Int = 
 
val result = timesThree(3)                //> result  : Int = 9                                                  //> curriedProd: (a: Int)(b: Int)Int

Now that we have seen how currying works. We can now look at a number of uses.

Function Calling Syntax
Lets again define a simple function. This function takes a single parameter and adds five to its value. There are actually 2 different function calling syntax (there are actually more then 2). One that uses the normal parenthesis for arguments () and another method that uses curly braces or block syntax {}. Lets take a look at an example

def plusFive( x: Int ) = {
  x + 5
 }                                         //> plusFive: (x: Int)Int
 
val b = plusFive(10)                      //> b  : Int = 15
val c = plusFive{ 10 }                    //> c  : Int = 15

The purpose of substituting curly braces for parenthesis is to enable client programmers to write function literals between curly braces. One thing to note however is that we can only supply this kind of syntax if the function has a single argument. But wait !! Using currying it is always possible to break arguments out into their own argument list, thus yielding a function with a single parameter. Here is a small example.

def applyOpt( a: Int, b: Int, op: (Int, Int) => Int) = {
  op( a, b )
 }                                         //> applyOpt: (a: Int, b: Int, op: (Int, Int) => Int)Int
 
 applyOpt( 3, 3, (a: Int, b: Int) => { a * b } )
                                                  //> res2: Int = 9
 
 
 
 def applyOpt2( a: Int, b: Int)(op: (Int, Int) => Int) = {
  op( a, b )
 }                                         //> applyOpt2: (a: Int, b: Int)(op: (Int, Int) => Int)Int
 
 
 applyOpt2( 4, 4 ){ (a,b) => a*b }         //> res3: Int = 16
// or even..
applyOpt2( 4, 4 ){ _ * _ }                //> res3: Int = 16

Now that we have seen how this works it is time to put it all together in a real world example.


The Loan Pattern

The loan pattern is a way of lending resources to the caller while managing the lifecycle of those resources when they are no longer used. Coming from a C# background this is exactly the purpose of the "using" key word in C# .net

Lets finish off with a concrete example of this pattern in action.


def withOutput(f: OutputStream => Any) {
  val out: OutputStream = getOutputStream()
 
  try {
    f(out)
    out.flush()
  }
  finally {
    out.close() 
  }
}

// Now we can write...
withOutput { out => out.write(response.getBytes()) }


Tuesday, November 12, 2013

Android SDK Setup for Linux Mint

Android SDK Setup for Linux Mint

Quick Setup reference:
You will need to get the correct sun-java package. There are a few ways to do this. Perhaps the easiest is to update your apt repo with one of the links listed in this article here. If you choose to install the package via the oracle website (which is what I did) you will Require the 32 bit version for android. There is an overview of how to install this on linuxmint.com.

Next make sure that you download the correct SDK bundle from the android website. Download and uncompress to the folder of your choosing.

Configuring your device will require that you know your vendor id. Make sure your device is attached via USB and use the command lsusb. This will give you the vendor id. Once you have down this you should be able to add it to your /etc/udev/rules.d/51-android.rules. You will need to edit the file as root.

Here is an example configuration.
# adb protocol on passion (Nexus One) SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0600" 

Once you have this line added you will need to restart you ADB server
/sdk/platform-tools/adb kill-server
/sdk/platform-tools/adb start-server 

It might server to add a symlink to adb so you can execute it anywhere in your environment.

Lastly if you are still having trouble with the device not showing up reference this post.
Your device still can't display? Make sure your "Project Build Target" Android version is supported in your Device.
  • Check your device's Android version. In your device, select Settings->About Device.
  • Check Android version of your project. Right click your project->Properties->Android->ProjectBuildTarget.
  • Make sure that it's not newer than your device's version.


Android SCALA

For creating android application with Scala (Which is more fun) download the scala IDE.

Follow all the documentation and you should be good to go.

Tuesday, October 29, 2013

Windows DLL Injection used for good not evil

Dll Injection


Dll Injection is a procedure for "hooking" into an already running peice of codes memory space.  So as to spy on or replace functionality.  This post serves to outline my open source project that demonstrates dll patching.  You can find the source code for this post on GitHub


slimhook

Demonstration of dll injection. As well loading .net runtime and calling .net code. Example hijacking d3d9 dll and altering rendering of games.
This project has 2 goals. The first is to have a simple clean interface for performing DLL injection on a windows shared library. The second is to load and execute your custom code in the .net runtime.
Last this project puts these 2 concepts together an performs a DLL injection on a direct3d video game. It then deals with the d3d driver using the SlimDX framework in .net. In this simple example we turn your triple A PC game graphics into “toon” style shading. Which is to say, we reduce the color space significantly using a technique described here: http://en.wikipedia.org/wiki/Posterization

Requirements:

  • N-CodeHook: http://newgre.net/ncodehook This is included in this project since there is a free license to do what you want with it. N-CodeHook is based on Microsoft detours(http://research.microsoft.com/en-us/projects/detours/).. but is completely free. The main advantage to N-CodeHook is the inline patching takes care of your “trampoline”. More info on this is availabe here: http://newgre.net/node/5
  • SlimDX SlimDX is a free open source framework that enables developers to easily build DirectX applications using .NET technologies such as C#, VB.NET, and IronPython. http://slimdx.org/ You need to goto the site and download the latest 4.0 runtime version of the library in order to run the demo code.

There are 3 projects in the solution.

  • inject.prog (Native C++ dll) This is the native C++ dll that will perform the dll injection on the target process. This is also responsible for loading the .net runtime and registering the function callbacks
  • SlimHook3D (C# .net) This is the code we want to execute in the targeted applications memory space. In the case of this demo we require SlimDX and perform graphics operations on the frame buffer of a hooked video game.
  • WinHookApp (C# ,net) Simple WinForm application that allow you to put in the process ID of the target process. This will then start the dll injection process.

How it works

The WinHookApp includes “System.Runtime.InteropServices” to load the symbols from our inject.dll. This can be seen with the following code
[DllImport("inject.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
internal static extern int Inject(int pid, StringBuilder amsBase);
Here we are loading the function that takes to parameters. The PID of the process we want to inject and the assembly base for the .net assembly that we want to load.
Once the inject function is called __declspec(dllexport) int WINAPI Inject(int pid, wchar_t* asemblyBase)
We start the process of DLL injection. We intend to inject the same dll we are executing from, namely inject.dll.
We start by getting a handle to the kernel32 lib
HMODULE hKernel32 = ::GetModuleHandle(L"Kernel32");
Next we call OpenProcess on the process we are trying to inject
HANDLE hProcess = ::OpenProcess( PROCESS_ALL_ACCESS,FALSE, pid );
Next we allocate memory inside that process with enouch room to hold the path to this library
pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath), MEM_COMMIT, PAGE_READWRITE );
Now we write the path to this lib in the memory we just allocated.
::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath, sizeof(szLibPath), NULL );
From here we create a remote thread that will call "LoadLibraryA" passing it the location of the path we just wrote
hThread = ::CreateRemoteThread( hProcess, NULL, 0,(LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,"LoadLibraryA" ),pLibRemote, 0, NULL );
We now should have this dll loaded into the remote process. The next thing we need to do is call a method in our dll. In this case we want to call "HookD3d9". We first get the address of the function.
FARPROC hookProc = ::GetProcAddress( hLoaded,"HookD3d9" );
Now we load the parameters to the function in the same manore that we did before. This parameter will be the base path for the .net dll asembly.
::WriteProcessMemory( hProcess, pLibRemote, (void*)asemblyBase, sizeof(szLibPath), NULL );
We now calculate the offset to our function "HookD3d9" and call the function with passing in the parameter.
DWORD offset = (char*)hookProc - (char*)hLoaded;
myfile<< std::hex << "Offset: " << offset <<std::endl;

  // Call the real action now that we are loaded.  We can not do anything interesting from the dllMain so we need another exprot to call
  DWORD entry = (DWORD)hLibModule+offset;
  myfile<<"Create Remote Thread 2 at entry: "<< std::hex << entry  <<std::endl;
  HANDLE hThread2 = ::CreateRemoteThread( hProcess, NULL, 0,(LPTHREAD_START_ROUTINE)entry, pLibRemote, 0, NULL );
We now will be executing "HookD3d9" inside of the other process.

Loading and running the .NET runtime

I will save this for a future post ....

Friday, October 25, 2013

android scala gl



android scala gl

Android OpenGL ES 2 / draw utility library written in scala
Link to source code on GitHub

Overview

This project serves to wrap up some common functionality regarding drawing to the screen on the Android platform. I am also fairly new to the scala language and have been very impressed with what I can do with it so far. I hope to continue to add more usefull utilities as I encounter a need for them.

Features

I will try to keep adding to this list as I go. For now here is a short list of features:
  • Simple OpenGL drawable class hierarchy
    • Includes Triangle, Square, Line, Sprite class
  • Simple way to extend functionality via shaders
  • DRY (Do not repeat yourself)

Example Usage

val texInfo = Drawable.loadGLTexture(imgPath)

val w = 2
val h = 2

val verts = Array(-0.5f*w,  0.5f*h, 0.0f,   // top left
                -0.5f*w, -0.5f*h, 0.0f,   // bottom left
                 0.5f*w,  0.5f*h, 0.0f,   // top right
                 -0.5f*w, -0.5f*h, 0.0f,  // bottom left    
                 0.5f*w, -0.5f*h, 0.0f,   // bottom right
                 0.5f*w,  0.5f*h, 0.0f )  // top right
val color =  Array(1.0f, 1.0f, 1.0f, 1.0f)      
val tex = Array(     0.0f, 0.0f,  // top left
                              1.0f, 0.0f,  // bottom left
                              0.0f, 1.0f,  // top right
                              1.0f, 0.0f,  // bottom left
            1.0f, 1.0f,  // bottom right
            0.0f, 1.0f) // top right

val sprite = new Sprite( verts, color, tex, texInfo.textureId)

Example Projects

I currently use this iib in an Android app "Perspective Correct" that should be out in the google play store soon ~(10/20/2013)
Here is a screen shot of the current app Perspective Correct

Thursday, October 24, 2013

Openfire Plugin Dev / Customization

Openfire Plugin Dev / Customization

Now that I have openfire up and running... the next thing I want to do is to customize it to my needs. The normal way to extend openfire is by way of plugin. This allows you to enjoy future upgrades of openfire while not blowing away your source code additions. Admittedly it took a little more digging that I would have liked to find the right getting started guide.

Im my case I decided to go with using eclipse (Scala IDE). Originally I had hopes of writing my plugins in scala.. but ran into a number of problems there, so in the interest of time decided to continue forward with eclipse java. The first guide you should look at to get setup in eclipse is the following doc. Once you have that setup you may want to look at this one as well regarding building all the plugins.

One of the other things that you will notice is the ability to customize the data storage. The architecture here was really focused on JDBC and a SQL back-end... In my case I am using a graph database store so none of this helps me. Looking a little further into it and seems that replacing the JDBC provider with a custom implementation would not take to long ref here. However in the interest of time I have put it on the list of TODO: and forged ahead. There is however a method that will get us partly there. I will outline the goals and methods for achieving them in the following paragraphs

Goals

The idea here is that I would like to replace the "user" and "roster" aka "buddy" list of openfire with my own graphdb full of FOAF like relations. The next thing is I don't want to be slowed down at this stage in dev... so it is ok to leave some "hacks" in the interest of time and then circle back.

Method

In browsing the Plugins I noticed the "userservice". Here is a bit of text right out of the README

The User Service Plugin provides the ability to add,edit,delete users and manage their rosters by sending an http request to the server. It is intended to be used by applications automating the user administration process. This plugin's functionality is useful for applications that need to administer users outside of the Openfire admin console. An example of such an application might be a live sports reporting application that uses XMPP as its transport, and creates/deletes users according to the receipt, or non receipt, of a subscription fee. 


Perfect... this solves our quick and dirty way of getting our users into the system. Next we just need to deal with the roster... so that we get the proper "presence" requests for users coming on and offline.

Again a bit of digging lead me to the creation of a Custom Roster Item Provider. Here is the documentation that describes how to implement.

With not a ton of code, I was able to write a simple Roster Provider that calls my Scala Play webservice. The webservice of course is a REST Json service wrapper for all the good things stored in the graphdb :)

Few last points

In order to get things to work correctly I had to return roster items using the constructor that takes a domain (for example: jid@mydomain.com). But when creating that user inside open fire we only want to pass in the "jid" portion for the user name. Failing this the two ends will not be linked how you would expect them to be.

Tuesday, October 22, 2013

C# FLV byte stream metadata injector

C# FLV byte stream metadata injector

This post relates to my open source project on github. For source code visit me on git flv-streamer-2-file

flv-streamer-2-file

Project takes and FLV stream coming in from a "raw" source, and could be at any point in the "live" stream.
It then saves the FLV to disk and corrects Metadata to allow for seek operations on stream close.
Is this an FLV Metadata injector? Yes. But this one has been written to address some specific needs. Also I think it could be more useful than a lot of the other ones that are out there. Here are some of the major differences:
  • Written entirely in C# (.net)
  • Avoids usage of “unsafe” code.
  • Handles arbitrary file sizes and streams.
  • Memory efficient.
  • Small code base
  • And of course is open source

Overview

This code was developed to accomplish a specific task, but can be adapted to do a number of things to an FLV file.
My goal was to take an FLV stream … and save that stream to disk while still being able to seek inside the file. This means that you can drop out of the stream at any time and the file will contain the portion of content up to that point. The file on disk will contain the total duration and thus be seekable (tested with VLC).
Here is the general idea. I store the FLV header and meta data. I make sure that there are “placeholder” values for the meta data I want. I then write this out to the head of the file we are saving. I then stream the tag data to disk. I also keep a count of audio and video packets… you could also choose to alter timestamps ect…
When the stream errors our or has reached the end. I close the file and then go back in to the head to alter placeholder values. In my case the “duration” or total time of the file in seconds will allow players to now seek in the file.

Usage

The solution consists of 2 projects:
  • FlvStream2File (class library)
  • ConsoleTest (example app)
Compile the FlvStream2File. This is the only project required to use in your application. To use the test app.. simply compile and in a console window you can run:
>ConsoleTest.exe “path to infile.flv” “path to outfile.flv”
This will read in the source FLV and produce an output FLV with the correct duration in the metadata (thus it will allow seek operations)
If you run in the debugger you will also get a lot of useful output via the “Debug.Write” (System.Diagnostics)
Here is an example of how to use the lib.
using(FlvStream2FileWriter stream2File = new FlvStream2FileWriter("out.flv"));
{
  // keep adding bytes to the file (choose a block size)
  while(bytedata){
    stream2File.Write(buffer);
  }

  // finish write and fix flv header.
  stream2File.FinallizeFile();
}

Refrences

Here are some of the features of the more full featured and closed source version of FLV Metadata injector http://www.buraks.com/flvmdi/
Here is some good information around FLV file format
Also you will need to know a little bit about “Action Message Format” (AMF).

Expanding on this

My needs were specific, but the code can be adapted very easily to handle any of the other functions that you see in closed source projects such as FLVMDI

Ideas, Questions, Comments?

Just message me and I will try to help.

Monday, October 21, 2013

CandyJS and OpenFire configuration hell

CandyJS and OpenFire configuration hell

In my last post I got a web based client up and running with facebook chat. This used a BOSH server called punjab. This service acts as a proxy to the actual XMPP server for a web based client. This is required since we lack implementation details for the protocol at the browser level.

When riding on top of facebook chat you quickly realize that there are a number of limitations to 3rd party client. The largest one in my opinion been the lack of group chat support. If you to have been thwarted by this lack of support you can help by directing your request for group support in the API to the Facebook XMPP Developer Relations.

To build in our own support for this we will now need to host our own XMPP server. This is what the following article deals with... all the problems I had one a windows server.

First off ... given the choice I would implement the following on a linux based platform.. but this document may prove useful to someone in either case.

Choose your XMPP server I went and downloaded openfire, and open source apache licensed server. On windows this was an exe installer and a real breeze to install. Once installed you can login via a browser http://localhost:9000 and view / admin the settings.

The first thing that threw me a bit was not knowing that BOSH is integrated right into the openfire server. I originally though that I might have to use my punjab service to form a connection to openfire as well. This is wrong... your BOSH openfire service should be enabled and running on port 7070.

Now that we have our XMPP server up and running we will need to create a test client to connect to it. For me I choose to go with candyjs.

Configure Candy - So download and extract the candy files and example... looking at the documentation I did a configuration like the following.


Candy.init("http://localhost:7070/http-bind", {
  core: {
       debug: true,
       autojoin: ['name@conference.yourdomain.com']
   },
   view: { resources: '/assets/res/candy/' }
});

Candy.Core.connect('user@yourdomain.com','password');


I am developing my web framework in Scala using Play Framework... which like any development server runs on some higher port number (ex: 9000).
This gave me CORS errors saying that my server running on port 9000 is unable to communicate with the openfire server. No problem I rember seeing server settings in openfire to configure CORS. As it turns out I already had the domain set to properly. I tried a number of things to get this working but it always seemed to fail. No problem candy had provided documentation on HTTP Procy Configuration.

Proxy Setup - First thing I tried to do was to configure IIS using the URL rewrite module. This got me to a place of frustration as the "test" methods they provide claimed that it was working while I was unable to get the URL rewrite to perform in my browser.

Rather the fight with M$ I decided to just run the simple node.js proxy. But first off needed to shut down IIS. Going into the services tab a stoping the "World Wide Web Publishing" service should have been all that I need to do... but I failed to free up port 80. What made it worse was that `netstat -a -b -n` refused to tell me the process id that was bound to the port (as if this somehow provides a layer of protection?). With a bit more digging I wound that I also needed to shut down a service called "Web Deployment Agent". This did the job and freed up port 80.

Node.js config - now that I could bind node to port 80 I should have been off to the races, and indeed it looked that way for a while. I created the service as instructed on the candy HTTP Proxy Configuration

var http = require('http');
 
http.createServer(function(request, response) { 

 if(request.url === '/http-bind/') {
  var proxy_request = http.request({
   host: 'yourhost.com',
   port: 5280,
   path: '/http-bind/',
   method: request.method
  });
  
  proxy_request.on('response', function(proxy_response) {
   proxy_response.on('data', function(chunk) {
    response.write(chunk, 'binary');
   });   
   proxy_request.on('end', function() {
    response.end();
   });
   response.writeHead(proxy_response.statusCode, proxy_response.headers);
  });
  
  request.on('data', function(chunk) {
   proxy_request.write(chunk, 'binary');
  });
  request.on('end', function() {
   proxy_request.end();
  });
 }
 
}).listen(80);

So now my candy js configuration had the simple connection like this

Candy.init("http://localhost:7070/http-bind/", ...


Things seemed to be good on first request to the service and then hang on any further requests... grrrr!
Again after digging around I found a better way to implement the node.js proxy service using nodejitsu node-http-proxy


npm install http=proxy


Since I did not care about routing my request... I just wanted to test if this would work.. I wrote the simplest proxy I could think of.

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server
//
httpProxy.createServer(7070, 'localhost').listen(80);


This now finally worked and candy connected properly to openfire.
This is a quick and dirty approach to getting things up and running... but I hope this helps someone else navigate around some of the bs involved and will allow you to get right to the fun stuff... coding.

Thursday, October 17, 2013

Facebook Chat via Punjab Bosh and XMPP

Facebook Chat via Punjab Bosh and XMPP

I recently setup a web based facebook chat and found a number of problems with the information provided on the web on how to do this. This post serves as a reminder to myself and hopes to help others in my situation

Here is a quick diagram on the technology stack

Steps to making a web based client talk through facebook chat

First thing to understand is that you will not be able to talk to facebooks (somewhat XMPP compliant) server directly through your web browser. You are going to have to go through and intermediary service known as a BOSH server. Some more information on BOSH here.

Step 1: Setup and configure your BOSH service. In doing a bit of research I found that most people are using punjab. Punjab is written in python and runs on top of Twisted, here are the steps to getting your environmant setup.

On the server you want to run the BOSH service:
  • Download Python 2.7 here. (I have heard of problems with the above stack in 3.3)
  • Next (and yes this is required) download and install pyOpenSSL for python 2.7. Download link here.
  • Now you need to download and setup Twisted.
This is your environment to allow you to run punjab (the BOSH service) correctly.

Step 2:We need to now get the latest version of punjab from github. Clone or uncompress the file achieve and open a terminal window window into the directory.

Now we need to setup the service in a way were we will be able to talk to facebook. Note: you will require SSL certs. There are a number of examples of people using MD5 hash to connect to facebook and none of these worked for me. I am not 100% sure but I think you may no longer be able to connect in this manor... you therefore require SSL keys.

Create and edit a file in the punjab folder. I called mine "punjab.tac". Below is the contents of my file

# punjab tac file from twisted.web import server, resource, static
from twisted.application import service, internet
from twisted.internet import reactor, ssl

from punjab.httpb import Httpb, HttpbService

root = static.File("./html")

bosh = HttpbService(1)

root.putChild('http-bind', resource.IResource(bosh))

site = server.Site(root)

application = service.Application("punjab")
internet.TCPServer(5280, site).setServiceParent(application)

#
# RMW adding TLS support for Facebook chat/xmpp support
#
sslContext = ssl.DefaultOpenSSLContextFactory(
'PATH_TO_KEY_FILE.key',
'PATH_TO_CERT_FILE.crt',
)
reactor.listenSSL(
5281,
site,
contextFactory=sslContext,
)


# To run this simply to twistd -y punjab.tac
# To run in debug mode twistd -n -l - -y punjab.tac


You should now be able to start the service with the command `twistd -y punjab.tac`
When the service starts for me I am asked for the passphrase for me private key... once I enter this I see the following output
2013-10-16 10:14:32-0700 [-] Log opened.
2013-10-16 10:14:32-0700 [-] twistd 13.1.0 (C:\Python27\python.exe 2.7.3) starting up.
2013-10-16 10:14:32-0700 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2013-10-16 10:14:32-0700 [-] Site starting on 5280


Step 3: Connecting to facebook with our minimal web client application.
Next you are going to need to setup your facebook application. Follow this guide if you do not already have your facebook app setup.
Take note of both your AppId and your AppSecret. You will require both of these in the html client.
Now you can download my sample HTML application from github.
You will need to edit the APP_ID and APP_SECRET in the html file. Also you may need to change the location and/or port for your BOSH server. Right now it is setup for http://localhost:5280/http-bind

Note: You would not want to share your APP_SECRET in production release

Here is a final screen shot of the simple app.