Saturday, January 26, 2013

Debian 6.0.6 with Hyper-V networking support



What I did:
Install clean debian (with a legacy network adapter attached, using a fixed mac address)

Prepare build environment
apt-get update
apt-get install build-essential libncurses5-dev bzip2 linux-source-2.6.32
Unpack linux sources
cd /usr/src
tar -jxvf linux-source-2.6.32.tar.bz2
ln -s linux-source-2.6.32 linux
cd linux
Cleanup (not really needed because we just unpacked it all)
make clean
make mrproper
Configure kernel
cp /boot/config-2.6.32-5-amd64 ./.config
make menuconfig

 - Load an Alternative Configuration File
  - .config [OK]
 - General setup --->
  - Local version - append to kernel release
   - -hyperv (mind the hyphen in front of it)
 - Device Drivers --->
  - Staging drivers --->
   < >  VIA Technologies VT6656 support   (BUGFIX http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=568454) 
     Microsoft Hyper-V client drivers
 - Kernel hacking --->
  [ ] Compile the kernel with debug info
 Exit and save!
Build kernel
make
make modules_install
make install
depmod 2.6.32-hyperv
Create initramfs
mkinitramfs -o /boot/initrd.img-2.6.32-hyperv 2.6.32-hyperv
Update grub
nano /etc/default/grub
 Default=2 (To make the 3rd menu option selected)
update-grub
Add Hyper-V modules
nano /etc/initramfs-tools/modules
  hv_vmbus
  hv_storvsc
  #hv_blkvsc (THIS IS STILL BUGGY, SO WE DISABLE THIS FOR NOW)
  hv_netvsc
Some bugfixing...
nano /etc/modprobe.d/blacklist.conf
 #fix: Driver 'pcspkr' is already registered, aborting...
 blacklist snd-pcsp
 #fix: SMBus base address uninitialized - upgrade bios or use force_addr=0xaddr
 blacklist i2c_piix4
update-initramfs -u -k 2.6.32-hyperv
Fix networking
nano /etc/network/interfaces
  edit eth0 to seth0
poweroff
In Hyper-V settings remove legacy network adapter and add normal one
Start up, you will get an message saying: Fixing recursive fault but reboot is needed!
Just poweroff and power on again
You're done!

Known issues:
 - Mouse still isn't working
 - hv_blkvsc isn't working
 - When using more then 4 GB of memory you get SRAT: Hotplug area too small

Sunday, January 20, 2013

Reverse proxy on openSUSE

My previous article was about creating a reverse proxy on Debian.
Due to incompatibilities between Debian and Hyper-V I had to recreate the setup in an openSUSE environment.

How I did it:
1. Install openSUSE 12.2 as clean as possible.
2. Fix a error that causes yast to f#ckup the lines in the menus (disable all graphicall boot stuff in grub)
3. Fix a error "piix4_smbus", edit /etc/modprobe.d/blacklist.conf -> blacklist i2c_piix4
4. start yast2
    - Add "apache" + "yast2-httpserver"
5. Configure http-server from yast
    - Enable http
    - Open firewall port
    - Start apache on boot
    - Add modules (proxy, proxy-http, headers, rewrite)
    - Add vhost
6. Edit /etc/apache/vhost.d/IIS01.wouterspaans.nl.config
7. Reboot

Thursday, January 17, 2013

Reverse proxy on Debian

Solution: Reverse proxy.

What I did:

1. Download Debian 6.0.6 (64bit)
2. Install smallest possible version
3. apt-get install apache2
4. a2enmod proxy_http
5. a2enmod headers
6. a2enmod rewrite
7. nano /etc/apache2/sites-available/default

 ServerName server1.wouterspaans.nl
 ProxyPass / http://IIS01.wouterspaans.local/
 ProxyPassReverse / http://IIS01.wouterspaans.local/



 ServerName server2.wouterspaans.nl
 ProxyPass / http://IIS02.wouterspaans.local/
 ProxyPassReverse / http://IIS02.wouterspaans.local/

8. /etc/init.d/apache restart

Done!

Debugging when something goes wroong can be done looking at the logs...
tail -f /var/log/apache2/error.log

Friday, December 7, 2012

Get path for app.config

When building an application and debugging it we often use app.config. But which app.config is really looked at? We can find it's path using: string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; Example using an app.config or web.config could be: string connStr = ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString

Wednesday, November 21, 2012

RGB Led Color Picker using C# and an Arduino

Used:
 1x Windows Computer with Visual Studio
 1x Arduino UNO
 1x RGB Led
 3x 330 ohm resistors (I needed to use 2 more to calibrate the RGB led, Green and Blue where to bright compared to Red)
 1x Breadboard
 Some jumpwires

Result:

How did i do it?

C# Code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace RGBLedColorPicker
{
    public partial class Form1 : Form
    {
        // Initialize serial port
        SerialPort port = new SerialPort("COM3", 9600);

        // Initialize the default color to black
        private Color defaultColor = Color.FromArgb(0, 0, 0);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Open connection to Arduino
            port.Open();

            // Set default color
            this.SetColor(defaultColor);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Set default color
            this.SetColor(defaultColor);
            
            // Close connection to Arduino
            port.Close();
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                SetColor(colorDialog1.Color);
            }
        }

        private void SetColor(Color color)
        {
            // Update color in the panel
            panel1.BackColor = color;

            // Write color to Arduino
            port.Write(new[] { color.R, color.G, color.B }, 0, 3);
        }
    }
}

Arduino Code:
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;

void setup() { 
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() == 3)
  {
    analogWrite(RED_LED_PIN, Serial.read());
    analogWrite(GREEN_LED_PIN, Serial.read());
    analogWrite(BLUE_LED_PIN, Serial.read());
  }
}

Thursday, November 15, 2012

WCF Service in a console app

Create a new console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Add Reference.... Select System.ServiceModel from the .NET tab and click OK.
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service1));
            host.Open();
            Console.Write("Service is up and running");
            Console.ReadKey();
            host.Close();
        }
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GreetingMessage(string Name);
    }

    public class Service1 : IService1
    {
        public string GreetingMessage(string name)
        {
            return "Welcome to WCF " + name;
        }
    }
}

Add App.config:

  
    
      
        
        
          
          
            
          
        
        
        
          
            
          
        
      
    
    
      
        
          
          
          
          
        
      
    
  

soapUI Compatible App.config:
Remember to activate WS-A options in your request:
Add default wsa:Action
Add default wsa:To


  
    
    
    
      
        
          
            
          
        
      
    
    

    
      
        
        
          
          
            
          
        
        
        
          
            
          
        
      
    
    
      
        
          
          
          
          
        
      
    
  
  
  

Wednesday, February 29, 2012

Generate type safe classes for sharepoint - vol2

I created a T4 template file that will:
1 - Search for SharePoint content types.
2 - Generate type safe Names and ID's for them.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<# 
// GET fileNamespace -> http://lennybacon.com/CommentView,guid,6ba5f768-6325-4f09-8341-201122804f52.aspx
var hostServiceProvider = (IServiceProvider)Host;
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
var activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
var dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
var defaultNamespace = dteProject.Properties.Item("DefaultNamespace").Value;
var templateDir = Path.GetDirectoryName(Host.TemplateFile);
var fullPath = dteProject.Properties.Item("FullPath").Value.ToString();
fullPath = fullPath.EndsWith("\\") ? fullPath.Substring(0, fullPath.Length-1) : fullPath;
var subNamespace = templateDir.Replace(fullPath, string.Empty).Replace("\\", ".");
var fileNamespace = string.Concat(defaultNamespace, subNamespace);

// GET All XML files -> http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx
var searchPath = new DirectoryInfo(fullPath).Parent.FullName;
var folderList = new Stack<string>();
var allXmlFiles = new List<string>();
string[] currentFolders = null;
string[] currentFiles = null;
string thisFolder = null;
folderList.Push(searchPath);
while(folderList.Count > 0)
{
    thisFolder = folderList.Pop();
    currentFiles = Directory.GetFiles(thisFolder, "*.xml");
    foreach(string file in currentFiles) if (!file.Contains("\\Debug\\")) allXmlFiles.Add(file);      
    currentFolders = Directory.GetDirectories(thisFolder);
    if(currentFolders != null && currentFolders.Length > 0) foreach(string folder in currentFolders) folderList.Push(folder);    
}

// GET All Fields from XML files
var fields = new List<KeyValuePair<string, KeyValuePair<string, KeyValuePair<string, string>>>>();
foreach (string xmlFile in allXmlFiles)
{
 var doc = new XmlDocument();
    doc.Load(xmlFile);
    XmlElement root = doc.DocumentElement;
    foreach (XmlNode node in root.ChildNodes)
        if (node.Name == "ContentType" && node.Attributes != null)
  {
            var attributeName = node.Attributes["Name"];
            var attributeID = node.Attributes["ID"];
            var attributeDisplayName = node.Attributes["Description"];
   fields.Add(new KeyValuePair<string, KeyValuePair<string, KeyValuePair<string, string>>>((attributeName != null) ? attributeName.InnerText.Replace(" ", string.Empty).Replace(
                                            "-", string.Empty) : "",new KeyValuePair<string, KeyValuePair<string, string>>(xmlFile.Replace(searchPath + "\\",""), new KeyValuePair<string, string>((attributeID != null) ? attributeID.InnerText : "",(attributeDisplayName != null) ? attributeDisplayName.InnerText : ""))));
  }
}
#>// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContentTypes.cs" company="Imtech ICT Integrated Solutions">
//   Copyright 2012 by Imtech ICT Integrated Solutions. All rights reserved. This material may not be duplicated for any profit-driven enterprise.
// </copyright>
// <summary>
//   Static ContentType Names And Ids
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace <#= fileNamespace #>
{
    using Microsoft.SharePoint;

    /// <summary>
    /// Static ContentTypeNames
    /// </summary>
    public static partial class ContentTypeNames
    {
<#
string previousFile = "";
bool firstTimeRun = true;
foreach (var field in fields.OrderBy(t => t.Value.Key).ThenBy(t => t.Key))
{
 if (previousFile != field.Value.Key)
 { if (!firstTimeRun)
  { #>

<# }
  if (firstTimeRun){firstTimeRun = false;} #>
        // <#= field.Value.Key #>
<#   previousFile = field.Value.Key;
 } #>
        public static readonly string <#= field.Key #> = "<#= field.Key #>";
<#}#>
    }

    /// <summary>
    /// Static ContentTypeIds
    /// </summary>
    public static partial class ContentTypeIds
    {
<#
previousFile = "";
firstTimeRun = true;
foreach (var field in fields.OrderBy(t => t.Value.Key).ThenBy(t => t.Key))
{
 if (previousFile != field.Value.Key)
 { if (!firstTimeRun)
  { #>

<# }
  if (firstTimeRun){firstTimeRun = false;} #>
        // <#= field.Value.Key #>
<#   previousFile = field.Value.Key;
 } #>
        public static readonly SPContentTypeId <#= field.Key #> = new SPContentTypeId("<#= field.Value.Value.Key #>");
<#  } #>
    }
}