C:\ant buildandtest
C:\me go and get coffee
C:\unit tests fail
// I know this first include isn't very embedded. I need it for Sleeping
#include "windows.h"
#include "stdio.h"
// Declare typedefs for state conditions and actions
typedef unsigned char (*ConditionFunction)(void);
typedef void (*ActionFunction)(void);
// The structure defines an action, a condition and a transition
// If the condition is met, the action is executed and the transition occurs
typedef struct _tagState {
ConditionFunction Condition;
ActionFunction Action;
struct _tagState *nextState;
} StateItem;
// Some sample conditions
unsigned char falseCondition(void)
{ return 0; }
unsigned char trueCondition(void)
{ return 1; }
// Some sample actions
void SomeAction(void)
{ printf ("SomeAction\n"); }
void BootAction (void)
{ printf ("BootAction\n"); }
void NoAction (void)
{ printf ("NoAction\n"); }
// The state machine engine.
// Simplicity is bliss
StateItem* ProcessState(StateItem *si) {
while(1)
{
if(si->Condition() == 0)
{
si++;
}
else
{
si->Action();
return si->nextState;
}
}
}
// Forward declare states so jumping is unhampered
const StateItem OtherState[];
const StateItem BootState[];
/*
* The tables represent states, each line is a state item
* Generally, the last state item performs a state action,
* and jumps back to itself. Preceeding items are generally
* transitions out to other states based on conditions
*/
// Except boot, end all the states with a trueCondition and a jump back to self
const StateItem OtherState[] = {
{&falseCondition, &SomeAction, OtherState},
{&trueCondition, &NoAction, OtherState}
};
const StateItem BootState[] = {
{&falseCondition, &SomeAction, BootState},
{&trueCondition, &BootAction, OtherState}
};
// Main just runs the state machine (no inputs or outputs are processed)
// The 'tick' time is 1 second which is slow for an embedded system
// but good for demonstrations
int main(void)
{
StateItem *StatePtr = BootState;
while (1) {
StatePtr = ProcessState(StatePtr);
Sleep(1000);
}
return 0;
}
| Use Project, not Normal |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace OcDac
{
public class OCDataAgent
{
private readonly SqlConnection myConnection;
private readonly SqlCommand selectPendingObjectCommand;
private readonly SqlCommand updatePendingObjectCommand;
public OCDataAgent()
{
myConnection = new SqlConnection();
// build the connection string
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = "tcp:10.0.0.114, 1370";
bu.InitialCatalog = "ocSample";
bu.IntegratedSecurity = false; // Sql Server Authentification
bu.UserID = "sa";
bu.Password = "youwish";
myConnection.ConnectionString = bu.ConnectionString;
// This stmt is used to get the ID and Version of one object that's pending
selectPendingObjectCommand = new SqlCommand { Connection = myConnection };
selectPendingObjectCommand.CommandText = "SELECT TOP 1 ID, VERSION FROM OnlyTable WHERE STATUS='PENDING'";
// Claim this object if it's row number hasn't been modified and has the correct ID
updatePendingObjectCommand = new SqlCommand { Connection = myConnection };
updatePendingObjectCommand.CommandText = "UPDATE OnlyTable SET STATUS='IN_PROCESS', VERSION=@new_version WHERE ID=@id AND VERSION=@version";
updatePendingObjectCommand.Parameters.Add("@id", System.Data.SqlDbType.BigInt);
updatePendingObjectCommand.Parameters.Add("@version", System.Data.SqlDbType.BigInt);
updatePendingObjectCommand.Parameters.Add("@new_version", System.Data.SqlDbType.BigInt);
}
public void LockPendingObject(ConcurrencyObject co)
{
if (myConnection.State == System.Data.ConnectionState.Closed)
OpenConnection();
updatePendingObjectCommand.Parameters[0].Value = co.Id;
updatePendingObjectCommand.Parameters[1].Value = co.Version;
updatePendingObjectCommand.Parameters[2].Value = co.Id + 1;
// If the claim succeeded, then we can update our co otherwise exception
var rowsAffected = updatePendingObjectCommand.ExecuteNonQuery();
if (rowsAffected != 0)
co.Id++;
else
throw new Exception("Could not lock object using OC");
}
public ConcurrencyObject GetPendingObject()
{
if (myConnection.State == System.Data.ConnectionState.Closed)
OpenConnection();
var co = new ConcurrencyObject();
var reader = selectPendingObjectCommand.ExecuteReader();
while (reader.Read())
{
co.Id = reader.GetInt64(0);
co.Version = reader.GetInt64(1);
}
reader.Close();
return co;
}
private void OpenConnection()
{
myConnection.Open();
}
}
public class ConcurrencyObject
{
internal Int64 Version { get; set; }
internal Int64 Id { get; set; }
}
}
![]() |
| VMWare Running Windows 2003 Server on my Windows 7 laptop |
| Uncertainty the only certainty in software development projects |