dcollections how to LinkList // port c# code

BLS windevguy at hotmail.de
Tue Jun 29 14:33:13 PDT 2010


On 29/06/2010 22:12, Steven Schveighoffer wrote:
> For now, can you do something like this?
>
> sl = new ServerList;
> sl.add([
>     new Server("ServerI", "120.14.220.18"),
>     new Server(...)
>     ...
> ]);

Hi Steve, I think this should work, however I got very strange err. msg. 
in file ArrayList.d  Have to stop now..need some sleep.
BTW the new constructor stuff would be nice to have.

//current code.

import std.stdio;
import std.random;

import dcollections.ArrayList;
import dcollections.LinkList;

void main() {

	auto b1 = LoadBalancer();
	auto b2 = LoadBalancer();
	auto b3 = LoadBalancer();

	// Confirm these are the same instance
     if (b1 == b2 && b2 == b3 )  {
     	writeln("Same instance\n");
    	}

	// Next, load 15 requests for a server
   	for (int i = 0; i < 15; i++) {
		string serverName = b1.nextServer.servername;
		writeln("Dispatch request to: " ~ serverName);
       }
}

// D2 singleton
final class LoadBalancer {
	private static LoadBalancer lb;
	alias ArrayList!Server ServerList;
	private ServerList sl;	
	
	static this() {
		synchronized lb = new LoadBalancer;
	}

	static LoadBalancer opCall() {
		return lb;
	}
	
	private this() {
		sl = new ServerList;
		sl.add([
	 	  	new Server("ServerI", "120.14.220.18"),
   	 		new Server("ServerII", "121.14.220.18")
		]); 	
	}

	@property
	{	
		Server nextServer() { return sl[uniform(0, sl.length)]; }
	}
		
	private class Server {
		private string _name, _id;

		this(string name, string id) {
			this._name = _name;
			this._id  = id;	
		}
		
		string servername() {
			return _name;
		}
		
		/* OLD PROPERTY STUFF
		@property
		{
			string servername(string sn) { return _name = sn; }
			string servername() { return _name;	}

			string id(string id) { return _id = id; }
			string id() { return _id; }
		}
		*/
		
	}
}

cheers,bjoern


More information about the Digitalmars-d-learn mailing list