12/2/15

ASP.NET 5 Beta 8

It's been a while since a wrote something on a blog.

Today I would like to share with you a good starting point for ASP.NET 5 and switching to beta 8.

Here is a nice article Upgrading from ASP.NET 5 Beta 7 to Beta 8

And nice tutorial made by the same author Building a Web App with ASP.NET 5, MVC 6, EF7 and AngularJS

3/6/14

TCExecutor on NuGet

TCExecutor is now awailable on NuGet.org

Project page: TCExecutor

From Visual Studo:

9/11/13

Thread safe rotate index.

Sometimes you may need a rotation thread safe index.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Init value = Int32.MaxValue - 10");
            RotatorIndex.Init = Int32.MaxValue - 10;
            Thread thread1 = new Thread(new ThreadStart(ThreadMethod));
            Thread thread2 = new Thread(new ThreadStart(ThreadMethod));
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            Console.WriteLine("Init value = - 10");

            RotatorIndex.Init = - 10;
            thread1 = new Thread(new ThreadStart(ThreadMethod));
            thread2 = new Thread(new ThreadStart(ThreadMethod));
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            Console.WriteLine("Init value = Int32.MinValue + 10");

            RotatorIndex.Init = Int32.MinValue + 10;
            thread1 = new Thread(new ThreadStart(ThreadMethod));
            thread2 = new Thread(new ThreadStart(ThreadMethod));
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();

            Console.ReadKey();
        }


        static void ThreadMethod()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(RotatorIndex.ID);
            }
        }

        /// 








        /// This is a thread safe rotator index
        /// 
        class RotatorIndex
        {
            private static int _iSeq = 0;
            private static int _digits = 3;

            public static int Seq
            {
                get
                {
                    return Interlocked.Increment(ref _iSeq) & 0x7FFF % (int)Math.Pow(10,_digits);
                }
            }

            public static string ID
            {
                get
                {
                    return Seq.ToString().PadLeft(_digits,'0');
                }
            }            

            /// 
            /// Use it just for test
            /// 
            public static int Init
            {
                set
                {
                    _iSeq = value;
                }
            }
        }
    }
}

6/21/12

Localized type conversion

If you need a DateTime or double convertion depending on system localization here are some examples:
Date to string
DateTime dt = new DateTime(2012, 06, 13, 17, 50, 00);

string date_US = dt.ToString(new CultureInfo("en-US").DateTimeFormat);

string date_GB = dt.ToString(new CultureInfo("en-GB").DateTimeFormat);

string date_PL = dt.ToString(new CultureInfo("pl-PL").DateTimeFormat);

string date_RU = dt.ToString(new CultureInfo("ru-RU").DateTimeFormat);

Console.WriteLine("date_US = {0}", date_US);
Console.WriteLine("date_GB = {0}", date_GB);
Console.WriteLine("date_PL = {0}", date_PL);
Console.WriteLine("date_RU = {0}", date_RU);

/*
date_US = 6/13/2012 5:50:00 PM
date_GB = 13/06/2012 17:50:00
date_PL = 2012-06-13 17:50:00
date_RU = 13.06.2012 17:50:00
*/
String to date
date_US = "6/13/2012 5:50:00 PM";
date_GB = "13/06/2012 17:50:00";
date_PL = "2012-06-13 17:50:00";
date_RU = "13.06.2012 17:50:00";

DateTime dt_US = Convert.ToDateTime(date_US, new CultureInfo("en-US").DateTimeFormat);

DateTime dt_GB = Convert.ToDateTime(date_GB, new CultureInfo("en-GB").DateTimeFormat);

DateTime dt_PL = Convert.ToDateTime(date_PL, new CultureInfo("pl-PL").DateTimeFormat);

DateTime dt_RU = Convert.ToDateTime(date_RU, new CultureInfo("ru-RU").DateTimeFormat);

Console.WriteLine("Dates Compre {0}", DateTime.Compare(dt_US, dt_GB));
Console.WriteLine("Dates Compre {0}", DateTime.Compare(dt_GB, dt_PL));
Console.WriteLine("Dates Compre {0}", DateTime.Compare(dt_PL, dt_RU));

/*
Dates Compre 0
Dates Compre 0
Dates Compre 0
*/
Double to string and string to double
double d = 1003.045;
string double_US = Convert.ToString(d, new CultureInfo("en-US").NumberFormat);
string double_PL = Convert.ToString(d, new CultureInfo("pl-PL").NumberFormat);

Console.WriteLine("double_US = {0}", double_US);
Console.WriteLine("double_PL = {0}", double_PL);
/*
double_US = 1003.045
double_PL = 1003,045
*/

double_US = "1003.045";
double_PL = "1003,045";


double d_US = Convert.ToDouble(double_US, new CultureInfo("en-US").NumberFormat);
double d_PL = Convert.ToDouble(double_PL, new CultureInfo("pl-PL").NumberFormat);

Console.WriteLine("Double compare {0}", d_US == d_PL);
/*
Double compare True
*/

5/2/12

SSH

Recently I was thinking of SSH support in my TCExecutor application.
I've found some 3rd party libraries which support SSH.... but... I think I'll try to implement it on my own...

Here are some specs:

  • The Secure Shell (SSH) Protocol Assigned Numbers - RFC 4250
  • The Secure Shell (SSH) Protocol Architecture - RFC 4251
  • The Secure Shell (SSH) Authentication Protocol - RFC 4252
  • The Secure Shell (SSH) Transport Layer Protocol - RFC 4253
  • The Secure Shell (SSH) Connection Protocol - RFC 4254

4/30/12

TCExecutor v0.0.1 released

TCExecutor - Telnet Command Executor C# open source project is released in version 0.0.1

Thanks to TCExecutor you can add communication/configuration layer to your application in just four steps:

  1. Create TCExecutor object
  2. Define commands
  3. Hook up your code to commands events (TCExecutor is multithread/multiconection application remeber that your code must be threadsafe)
  4. Start TCExecutor listener.
Commands style:
>command1 subcommand1 ... subcommandN param1=X1 ... paramN=XN ARGV1...ARGVN



Features:
  • Adds network communication layer to your application
  • Supports multiple connections. For each connection separate session is established.
  • Commands can be defined at your application startup.
  • Each command has separate event to which you can connect to.
  • Each command has a short and long help string which helps end user to navigate through available options.
Next releases features:
  • Enable SSH connections to the TCExecutor.
  • Add simple user autentication. Login password.
Project in action


Project can be found here: https://bitbucket.org/chesti/tcexecutor/
You can clone code from git: git clone https://bitbucket.org/chesti/tcexecutor.git
Code example:

using TelentCommandExecutor;

class ProgramObject
{
   private TCExecutor executorSRV;

   public ProgramObject()
   {
      //create an executor
      executorSRV = new TCExecutor();
   
      //hook up some commands
      CmdExecutor e;
      e = executorSRV.AddCommand("set name");
      e.ExecutorEvent += new CmdExecutor.ExecutorHandler(Execute_SetName);
      e.HelpString = "set name n=name";
      e.ShortHelpString = "Dispalys your name";
      
      //listen to the any end point on port 9999
      System.Net.IPEndPoint endPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 9999);
      //start executor
      executorSRV.StartServer(endPoint);

      Console.ReadKey();

      //stop executor and disconnect all users.
      executorSRV.StopServer();
   }
   
   //handle command event
   string Execute_SetName(Cmd command)
   {
      if (command.CmdParams.ContainsKey("n"))
      {
          return String.Format("Your name is: {0}", command.CmdParams["n"]);
      }
      else
      {
         return "Use n= parameter";
      }
   }
}


12/30/11

Nikon D40 Intervalometer

Is it possible to create time lapse pictures with Nikon D40... yes...


You can find results here:

result1
result2

You are a software engineer... you can do anything... it is just a matter of time ;)