|
Object pooling is the technique that when you reference an object in two places with the same ID, you only deal with one object in memory so you don't get out of sync objects. To implement object pooling, a factory method is often implemented to ensure the same object reference is given. Example (in PHP):
// Place 1 (foobar.class.php) $object = Foo::factory(1);
// Do stuff..
------
// Place 2 (min.class.php) $object = Foo::factory(1);
// Do stuff..
-----
class Foo {
static function factory($id) { // Return instance (and create it first if it wasn't requested before) }
}
This is especially handy when you have to save the object. You don't get corrupted database data because the object you're working with is always the most recent one, you'll save memory, etc.
But programmers do forget that there's a factory method and thus will break things.
There are two options:
1. Mandate the use of the factory() method by making the constructor protected.
2. Use another form of pooling by using a sort of bean.
I'd like to talk about option 2.
Say you have the following class:
class Foo {
protected $foo = 0; protected $id = 0; function __construct($id) { $this->id = $id; } function set($x) { $this->foo = $x; } function get() { return $this->foo; }
}
This object must have some sort of pooling. So I figured, parent classes are able to modify these protected properties. With a little bit of thinking I came up with the following code:
class Foo extends FooPool {
protected $foo = 0; protected $id = 0; function __construct($id) { self::load($this, $id); $this->id = $id; } function set($x) { $this->foo = $x; } function get() { return $this->foo; }
}
class FooPool {
private static $values = array(); // only set this when you want to, you don't need it private static $vars = array(); private static $set = false; static function load($object, $id) { // If the ID is 0, return (because there is nothing to cache) if ($id == 0) { return; } // If the object is not an instance of us, throw an exception if (!($object instanceof self)) { throw new Exception('object does not extend me :('); } // If we don't know the variables, get variables if (!self::$set) { self::$vars = get_object_vars($object); self::$set = true; } // If we don't have this id set, set it in the values array if (!isset(self::$values[$id])) { self::$values[$id] = self::$vars; } // now set a reference to very variable in the array foreach (self::$values[$id] as $name => $value) { $object->$name = &self::$values[$id][$name]; } }
}
This actually works pretty neat and is nicely transperant to others. Some example code that uses it:
$classone = new Foo(1); $classtwo = new Foo(1); $classthree = new Foo(2);
// Set classone:foo to 50 and echo classone, classtwo and classthree $classone->set(50);
echo $classone->get()."\n"; // Prints 50 echo $classtwo->get()."\n"; // Prints 50 echo $classthree->get()."\n"; // Prints 0
$classtwo->set(60);
echo $classone->get()."\n"; // Prints 60 echo $classtwo->get()."\n"; // Prints 60 echo $classthree->get()."\n"; // Prints 0
$classthree->set(80);
echo $classone->get()."\n"; // Prints 60 echo $classtwo->get()."\n"; // Prints 60 echo $classthree->get()."\n"; // Prints 80
Ofcourse you can make a standard pool by using an extra array:
// basic pool class class Pool {
// This array contains every value of every class. private static $values = array(); // This array contains the variable names of classes. private static $vars = array(); // This array determines what classes are already known. private static $set = array(); static function load($object, $id) { // If the ID is 0, return (because there is nothing to cache) if ($id == 0) { return; } // Get class name $classname = get_class($object); // If we don't know the class yet get variables and initialize values array if (!isset(self::$set[$classname])) { self::$vars[$classname] = get_object_vars($object); self::$set[$classname] = true; self::$values[$classname] = array(); } // If we don't have this id set, set it in the values array if (!isset(self::$values[$classname][$id])) { self::$values[$classname][$id] = self::$vars[$classname]; } // now set a reference to very variable in the array foreach (self::$values[$classname][$id] as $name => $value) { $object->$name = &self::$values[$classname][$id][$name]; } }
}
For making the Pool class to work with Foo, just let Foo extend from Pool instead of FooPool and everything should work.
In LUA , because it has no concept of classes but can be implemented using tables, it's way easier:
-- Lua implementation of pooling idea
Foo = {} Foo.values = {} function Foo:new(id)
if (Foo.values[id] == nil) then Foo.values[id] = setmetatable({}, {__index = Foo}); end return Foo.values[id]
end
classone = Foo:new(1) classtwo = Foo:new(1) classthree = Foo:new(2)
print(classone) print(classtwo) print(classthree)
(but I'll talk about Lua another time)
|
|
Jeej, judgement day vandaag.
Ik woon nu een jaar in Leiden en het heeft me tot nu toe altijd wel redelijk bevallen. Tuurlijk, ik mis nog steeds mijn ouders en mijn vrienden die allen gemiddeld zo'n 100 km van me vandaan wonen, maar ik heb gelukkig nog genoeg contact met ze.
Voor de mensen die me goed kennen weten dat mijn studie niet je-van-het gaat. Niet omdat het moeilijk is, het is best simpel, maar omdat mijn focus er totaal niet lag de afgelopen halfjaar. Ofwel ik ben eigenlijk niet het 2e semester naar de faculteit geweest. En dat ga je merken, je wordt dan nog gedemotiveerder en je wilt niks meer qua studie omdat je dan al zo druk bent. Dus dan neem je als beslissing, de avond voor je eerste herkansing, dat je maar tijdelijk stopt met je studie.
Dat's niet het enige. Mijn vakantie, wat eigenlijk over 2 dagen begint, had eigenlijk in Italie moeten beginnen. Maar helaas, dat gaat ook niet door door omstandigheden, dus ik moet maar 10 dagen iets anders doen. Gelukkig is er nog genoeg (leuks) te doen, dus ik zal me niet echt vervelen.
En dan nu de positieve dingen natuurlijk.
Allereerst ben ik van een shitload van mensen af. Dat is positief ja. Het zijn mensen die een nogal negatieve invloed op je humeur hebben. Mensen die:
- borderline neigingen vertonen - teveel aandacht willen (aka mensen die denken dat ze 't middenpunt zijn) - denken dat ze te belangrijk zijn voor mij - totaal niet weten wat ze willen, en ze dat laten weten op elke manier dan ook
De lijst kan langer, maar ik heb geen zin meer om nog meer aandacht aan ze te besteden. Niet alles is negatief over hen, met sommigen ben ik meerdere jaren om gegaan. Maar dan is dat keerpunt wat juist belangrijk is om nou exact te weten wat je aan ze hebt.
Als tweede heb ik toffe mensen leren kennen, via zowel mijn studie als werk.
De onafhankelijkheid die je hebt als je op je eigen gaat wonen zowel een zegen als een vloek. Je moet goed op jezelf letten dat je niet veel fastfood naar binnen werkt, en goed opruimen. Aan de andere kant kan je nu wel gerust rond 3/4 uur naar huis gaan zonder dat je er commentaar op krijgt en mag je je eigen boodschappen halen (ook wel vervelend trouwens, maar jij mag beslissen wat erin huis komt).
Als ik nu terug kijk op 't afgelopen jaar, ben ik best tevreden wat ik heb gedaan. Behalve mijn instelling tov studeren dan, maar het zat er wel aan te komen. Maar toch lekker eigenwijs door gaan, en dan toch niks doen.
En dan ga ik nu verder met niks doen.. |
|
The blog has now been officially moved to the new server, called Carmadain!
Installing the server went pretty well I think, although we did have a few minor issues with mounting the server.
However, moving my website to the server costed me more than a week! Why? Well, because some brainiac forgot to pay the bills and have been without electricity for a week, WTF. Now, all is well again and as you can see my blog is blazing fast on the new server, of which I'm quite proud of. |
|
In a few days (hopefully) this blog, and all my other projects are no longer hosted on my computer, but on a server. This is actually the first time since self-hosting I voluntarily put my stuff on a server..
But not to worry, this server is still managed by me and a good friend.
I'm actually quite proud of it, getting a new server and having it hooked up on a 100mbit connection, you know..
So why don't I use hosting providers? I mean, they are cheaper than colocated hosting and you don't have to worry about server stuff, right??
Since I started having my own website, I used free hosting providers like Freewebs, Coecu, and a friend's one. But, since I really got "pissed off" by the fact that I can't be in control of everything (and the "Woops, Windows Server crashed again" messages really got me pissed off lots of times), I started to (ab)use my router as a webserver. And I liked it ever since, since I can do what the hell I want to do it. I don't pay a cent extra if I want to have "blablabla.denniscgc.nl" or if I want to have 1000 databases (because I have 1000 projects), or whatever is considered luxury on hosting but are common when you have your own server.
Then I moved to Leiden, which made me to put my router offline. My site was then hosted on another friend's server. I really missed what I had before. And about 6 months later, I thought: "What the hell, let's install a webserver on my workstation and see what happens." Well, so I did and this page already runs for a few good months on this PC. However, this setup has its merits. A buggy PHP port, an unstable Livebox and using wireless are not contributing to a stable server setup.
A few months back, we decided to start a new project. And that made us think about colocated hosting with an own server. Although the project never got started, we never gave up on the idea of an own server.
And just the day before yesterday I was surfing the web for a new server, until I stumbled on a Dell PowerEdge R200 server. The server now only costs about 450 euros.
So we decided it was time to make our move, and we're going to order one.
I'll let you know when this blog has moved... |
|
It's time for a new update, so I figured. This will probably the last blog that I don't have to be really code specific, and that should be great for you. Because when I get into the code/design, you will either run away or say WTF (or might think: "Genius", but that's very unlikely).
This will be mostly about features I tend to implement, plus what I'll program as well extra... (and why).
The features.
To quote from my specification (but translated, since it's Dutch):
- Personal * IM, meaning you get to decide who you'll give permission to talk to you :p (contact list) * Display pictures are more important, in the sense that there will be support for larger DPs and integration with Gravatar. * PSM (not new)
- Group conversations * Should be possible on every combination: text, audio and video.
- Encryption support in the protocol * There's support for encryption. There's encryption in some MSN chat clients, but they are dependant on whatever client you're using...
- Better support for file sharing/streaming * MSN completely blew up file transfers. With IPv6 however, it should be fixed, but since I don't see IPv6 coming, I decided to use something called hole punching.
- Server/routing support * With limited bandwidth, you still want to have all the nice features like an audio group conversation. The chat client should be able to route either through other clients or the server.
- Resource friendly * After all, it's just a chat program, not a 3D rendering program or game...
Things missing.
Of course, there will be some things missing.
- No GIF/JPEG support yet, read on. - No animated emoticons, they suck, are annoying, etc.
The reason why I don't program support for GIF/JPEG is that I'll be writing, for example, the PNG implementation and I simply don't have enough time. Most will consider this a waste of time, I however think it's great. I learn and hopefully it's efficient...
Some technical stuff.
After all that basic feature set, let's get to some more technical stuff.
The first thing I'd like to think as a feature is the protocol that's being used. It's a simple binary protocol that's entirely extensible. It also supports pre- and post-padding so it can be better encrypted. The format is simple: first you have one byte that indicates the command group, which means that the commands are categorized! This first byte has a marked bit so the client can find the command stream.
The second byte indicates the command, followed by a few bytes indicating the length of the total packet.
Parameters are followed, but are optional. A parameter consists of a minimal of 3 bytes.
This binary protocol lends itself for very efficient handling in Assembly/C.
The second feature I think is great is the routing protocol that's embedded in the protocol, although it's considered the advanced part of the protocol and thus is not implemented in the first versions of Elfoyx.
Anyway, it lets the client decides how a stream should go. Consider the persons A, B and C. A has fast bandwidth, B and C both have a pretty slow internet connection but enough for a few streams. In this case it will be better if the streams will go like this:
B <-> A <-> C
This means that any stream sent to C will go via A, and vice versa. This will scale up nicely with more users. There will be an option of course to deactivate this in your client, but it's turned on by default. The server will probably be involved in this process as well, so if all have a slow internet connection the server might help. The only thing that prevents me doing this, are the costs ;-).
That's enough for today's post. Stay tuned! |
|
Okay, so I wrote a few posts ago about that I will post some information about the projects I'm involved in.
This post will be mostly about the why part.
The beginning of the idea.
It was about 2005 when the idea started. A few friends and I were tired of the fact that we couldn't speak with eachother in a group chat. Literally, speaking. Since I was tired of MSN anyway, and the limited ICQ as well, I figured I could create my own one. So I started analyzing audio. However, I was held back because of other (paid) projects so it didn't get my highest priority. Later on, I forgot the project because it wasn't neccesary anymore, eg. I lost contact with them.
In 2007, the idea was back..
Designing the protocol and chat, in my exam period..
Since I had the idea, I figured I could start writing a draft about the protocol, features, etc. This was a project done in my CSE period ("Centraal schriftelijk examen" aka National Exam )... Most people had lots of stress, I hadn't. However, I had to keep myself busy with some challenges (or thinking in general) I reasoned. Well, it worked. I didn't do that much for my exams but still passed with flying colours.
Anyway, I made the draft and showed it to others. Some were impressed, some questioned whether it's worth the effort.
Why a new chat?
* All official chat programs are bloated. Google Talk is slower than it has to be, WLM and ICQ are slow.
* The alternatives of WLM are no better. The MSN protocol is a WTF on itself. It embeds XML objects in a HTTP like protocol, as if they must have thought: "Well, we have some problems we didn't see before. Let's put XML into it.. and call it objects". It's really a mixture of HTTP and XML, why not stick with HTTP alone? How can you expect the alternative programs, like Pidgeon, to be new and refreshing?
* Jabber is entirely XML. Well, it at least has some consistency of being XML at the whole time. But seriously, using XML as a serious protocol? I guess it had to had support for XMLSocket() ? It's pretty good in theory, and nice for all the linguistics out there, but somehow bad in practice.
My conclusion is that no chat is good enough for what I wanted. It's a chat program, why should it be bloated? It's not a 3D rendering program or whatsoever?! It should be at all times responsive and when something goes wrong at the server side, it shouldn't put the whole chat down!
That's enough for one blog post, stay tuned for other parts :-). |
|
Elk jaar is het de gewoonte dat je 2 minuten stil bent om 8 uur 's avonds en dat je aan de mensen denkt die voor onze vrijheid hebben gestreden. Dit doe ik dus elke jaar.
Maar niet dit jaar... Nee, geen goede reden zit erachter of wat dan ook. Ik was het simpelweg vergeten. (Nadeel van alleen op jezelf wonen...)
Nadat ik erachter kwam, door een sudden realizatie van de datum, heb ik alsnog 2 minuten stilte gehouden.
Mijn geheugen is op dit moment echt een zeef en vergeet ik gewoon dit soort dingen... AAH
|
| |