December 2006 - Posts

reposted from here

for quite a few years now, i've had this idea of a virtual cpu. it all started back in... oh i guess 98 or 99 and my fascination with the beowolf architecture. i convinced a friend of mine to help me persuade the company we worked for to give us like 20 of the old computers. the intention was to setup 2 beowolf clusters and then to... i dunno make them do something. this is really before i was into development, but i loved scripting and batch files and using linux to do something like this seemed pretty cool.

fast forward to now, my idea is this. you setup a cluster. any cluster size, shape etc. it doesn't matter. preferably for a machine to be 'in the cluster' you would just install an app or something like that. it would have to connect to a traffic controller or something. then, you would install a driver on your machine that utilized the cpu cloud for processing. your machine would see it as another cpu. this may not sound as cool, but take a scenario like this. say you are a company who has 100 machines. you need to migrate to citrix for some reason. now your machines processing is lying dormant.  you could install a service, install a simple controller box (or maybe more with a single point of entry), or maybe just install a different service. or maybe there is a listener on all of the machines and they dynamically decide who is in charge. kind of like master browser stuff.

i digress. anyway, say you are this company. you want to give a boost to your sql server, and maybe your iis server. neither is used heavily, but sometimes they have spikes. so you install your virtual cpu and bammo. you can use all that extra horse power.

my original idea also involved distributed memory usage and disk space too. just spread it all around the cloud in some sort of ultra parity and retrieve any of it from anywhere. now, while the memory may not be better or even close to memory locally, imagine if you could setup a ram drive using the ram spread across 100 machines. imagine, not only the speed ramifications of utilizing all these machines, but the ability to continue using your old machines. places like google, can always try and reduce power consumption etc. i know that's the big new thing, processes per power unit or whatever. but if you aren't a huge company and you already have those assetts depreciating on the books, it may be worth it. heck, maybe there's a creative way to write it off. /shrug
Very simple text rotator. No blending, just rotation. the quotes array could be dynamically grabbed from somewhere, or you could simply have the rotate function grab it randomly from somewhere instead of from the array. since these are just testimonials that i'm rotating over, i don't think anyone will have the page up for like 100 hours, and if they do, they can just see the same ones cached in js memory.

you could also simply build the array included here dynamically when you are writing out your page. this is really just here for the logical example.

of course, modify the html to fit your needs.

var quotes = new Array();
quotes[0] = "<blockquote><q>quote 0</q> - <cite>person 0</cite></blockquote>";
quotes[1] = "<blockquote><q>quote 1</q> - <cite>person 1</cite></blockquote>";
quotes[2] = "<blockquote><q>quote 2</q> - <cite>person 2</cite></blockquote>";
quotes[3] = "<blockquote><q>quote 3</q> - <cite>person 3</cite></blockquote>";

function rotate(id){
    var e = document.getElementById(id);
    e.innerHTML = quotes[Math.floor(Math.random()*quotes.length)];
}
window.setInterval("rotate('testimonial');",300000);
<%@ Page Language="C#" %>
<%@ Import namespace="System.Data.SqlClient"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Page.IsPostBack)
		{
			try
			{
				dg.DataSource = svht();
				dg.DataBind();
				using (SqlConnection cn = new SqlConnection(t.Text))
				{
						cn.Open();
						l.Text = String.Format("Connection.State = {0}", cn.State);
				}
			}
			catch (Exception ex)
			{
				l.Text = String.Format("Error: {0}", ex.Message);
			}
		}
	}
	protected Hashtable svht()
	{
		Hashtable ht = new Hashtable();
		foreach (string s in Request.ServerVariables.AllKeys)
		{
			ht.Add(s, Request.ServerVariablesSleep);
		}
		return ht;
	}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
	<title>Connection Test Page</title>
</head>
<body>
	<form id="f" runat="server">
	Enter a connection string and click submit:<br />
	<asp:TextBox ID="t" runat="server" Width="500px"></asp:TextBox><br />
	<asp:Button ID="b" runat="server" Text="Submit"/><br /><br />
	Result will show here:<br />
	<asp:Label ID="l" runat="server" ForeColor="red"></asp:Label><br /><br />
	Server variables will show here:
	<asp:DataGrid ID="dg" runat="server" AutoGenerateColumns="false">
		<Columns>
		<asp:BoundColumn DataField="Key" HeaderText="Key"></asp:BoundColumn>
		<asp:BoundColumn DataField="Value" HeaderText="Value"></asp:BoundColumn>
		</Columns>
	</asp:DataGrid>
	</form>
</body>
</html>
Very simple app that just shows how to traverse files and folders. Note that you can also utilize the FileSystemInfo class, however if you do that you have to write your own file filter checking etc. Not that the default is super robust, but it does end up being less code. Just for a change, I short cut the class references with using statements up top =P

I didn't just do this randomly, I was actually looking at using the c++ glob app shown here in my c# project and maybe just cycling through a glob instead of traversing the tree, but after testing them on an svn repository with a few thousand files, the difference was so small that i couldn't justify adding all that extra code and including a c++ dll etc. Not to mention, i would have to *write* the dll as the only thing included was an exe. That wouldn't be too bad, but there is only so much time in the world and this was so much quicker. =P

it's still probably fastest to just do a shell out and dir /s /b *.whatever and parse the text, but whatever.

using c = System.Console;
using d = System.IO.Directory;
using di = System.IO.DirectoryInfo;
using fi = System.IO.FileInfo;
using s = System.String;

namespace Glob
{
    class Program
    {
        static void Main(string[] args)
        {
            e(args[0],new di(d.GetCurrentDirectory()));
        }
        static void e(s ff, di cd)
        {
            foreach (fi f in cd.GetFiles(ff))
                c.WriteLine(f.FullName);
            foreach (di sd in cd.GetDirectories())
                e(ff,sd);
        }
    }
}



Here's a sample of what you could do for the FileSystemInfo class. Note, when i wrote this i was short cutting this class with a usting statement tool. Also the FileAttributes class with the fa var.

foreach (fsi fsi in cd.GetFileSystemInfos())
if ((fsi.Attributes & fa.Directory)!=0)
e(ff, new di(fsi.FullName));
else
c.WriteLine(fsi.FullName)));
//you would need a regex for name here though

reposted from my other blog

inspired by this article, i decided to write a .net version. i thought this could be something useful for searching our code base at work. kind of the same as this guy has setup for his single machine, but i figured i could search a whole subversion tree and pipe it to our dev intranet site.

Since I was at it, I figured I would make it a little more flexible. You can set all the options through the querystring. if you don't like that, feel free to just set them manually. i didn't want to pass around the xml object, so i just made it static. looking at this, i feel like there is a better way to do this, but i can't put my finger on it at the moment and it works fine. i skipped doing full regex and only match lines that have the requested item. i also skipped returning all the matching lines in a single rss entry and i just return every line match in it's own item. if i didn't do that, i had to kind of pick what format to use internally for the matches and i decided i'd let someone do that with a style sheet.

enjoy!

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Xml;

public partial class _Default : Page
{
static XmlTextWriter xml;
static string title;
static string path;
static Regex re;
static string filefilter;

static void EnumFolders(DirectoryInfo d)
{
EnumFiles(d);
foreach(DirectoryInfo sd in d.GetDirectories())
EnumFolders(sd);
}

static void EnumFiles(DirectoryInfo theDir)
{
foreach (FileInfo f in theDir.GetFiles(filefilter))
Find(f.FullName);
}

static void Find(string fn)
{
using (StreamReader r = new StreamReader(fn))
{
Match m = re.Match(r.ReadToEnd());
while (m.Success)
{
xml.WriteStartElement("item");
xml.WriteElementString("title", fn);
xml.WriteElementString("description", m.Value.Trim());
xml.WriteEndElement();
m = m.NextMatch();
}
}
}
    private void Page_Load(object sender, EventArgs e)
    {
        title = Request.QueryString["t"]; // name your feed
        path = Request.QueryString["p"]; // c:\code\
        re = new Regex("^.*" + Request.QueryString["r"] + ".*$",RegexOptions.Multiline); //TODO
        filefilter = Request.QueryString["f"]; //*.cs
    
        Response.Clear();
        Response.ContentType = "application/xml+rss";
        xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        xml.WriteStartDocument();
        xml.WriteStartElement("rss");
        xml.WriteAttributeString("version", "2.0");
        xml.WriteStartElement("channel");
        xml.WriteElementString("title", title);
        xml.WriteElementString("link", Request.Url.PathAndQuery);
        xml.WriteElementString("description", "rss for (" + re + ") in " + path + filefilter + " and it's sub directories");
        xml.WriteElementString("copyright", "(c) " + DateTime.Now.Year + ", " + Request.Url + " All rights reserved.");
        xml.WriteElementString("ttl", "5");
        try{
            EnumFolders(new DirectoryInfo(path));
        }
        catch (Exception ex)
        {
            xml.WriteStartElement("item");
            xml.WriteElementString("title", "error");
            xml.WriteElementString("description", ex.Message);
            xml.WriteEndElement();
        }
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Flush();
        xml.Close();
        Response.End();
    }
}