Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

112 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 16:04

112 Chapter 3 Application Design: A Real-Life Example registers itself registers itself Main system Plug-in A receives data from receives data from Plug-in B Figure 3.9 Chat system with plug-ins. Design your own plug-in-system, at least theoretically. Create a minimal application that s able to register plug-ins with itself and execute them. When starting up, phpChat includes an include file, which in turn includes all wanted plug-ins. Listing 3.3 shows how this include file works: Listing 3.3 The plug-in includer. ////////////////////////////////////////////////////////////////////////////// // // Plug-in Integrator // ////////////////////////////////////////////////////////////////////////////// include( chat_plugin_out_htmlspecialchars.php3 ); include( chat_plugin_out_link_transform.php3 ); include( chat_plugin_out_colorcodes.php3 ); include( chat_plugin_clock.php3 ); include( chat_plugin_cmd_basic.php3 ); include( chat_plugin_out_basic.php3 ); ////////////////////////////////////////////////////////////////////////////// Each of the plug-ins is built up in the same way, consisting of a main part and an event part.The main part calls two functions in phpChat,with the following names: chat_register_plugin_init() and chat_register_plugin_deinit(). Each function takes as a parameter the name of another function, which should be called for plug-in initialization and plug-in deinitialization, respectively. phpChat adds these function names to an internal table. Upon initialization of the chat, as soon as phpChat is fully set up, it makes a run through the initialization table and calls the initialization function of every plug-in that registered itself. Similarly, upon shutdown,it runs through the deinitialization table.This method allows signaling the plug-ins to activate and deactivate themselves.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 109 This is exactly what

Filed under: Web Applications Development With PHP4.0 — webmaster @ 05:42

Interfacing the Network 111 Interface to HTML Developers In terms of the HTML interface, abstraction of code and layout is done using templates.This is the easiest possibility for tweaking an application to your needs,yet it s also the most powerful.Within seconds,you can change the look and feel without having to modify a single line of code. Everyone with basic HTML knowledge could completely restructure the way the application would show itself to a user.As this method is discussed elsewhere in this book, we won t go deeper into it here.To find more details about using templates,please read Chapter 5, Basic Web Application Strategies. Interface to Code Developers Providing an interface to other developers is usually associated with the term API (Application Programming Interface).APIs are normally provided by libraries (such as phpIRC), but not by complete applications. But applications that have the capacity to be extended by a programmer are much more successful than applications that must be used as is. Of course, in terms of PHP applications, anyone can modify the source code, but many people refrain from analyzing a complex system and applying modifications to it.Thus,the application itself needs to expose certain ways of being extended. Note: We re differentiating here between applications and libraries. Libraries are meant to be used by applications, cannot be run stand-alone, and are generally much easier to extend than applications.Applications consist of a full, closed system. Try to find out how common applications can be extended. For example, for your favorite text-processing tool, see whether the developers provided the capacity to extend the tool s functionality. Two primary possibilities of extending applications have evolved: Either the application provides scripting capabilities (similar to macros), or the application is able to use plug-ins.As for PHP,implementing a script language in a time-critical part of a system we don t need to think any further. On top of that, the complexity of creating a full-fledged parser is way too much to ask. But plug-ins are much easier to implement and have many advantages.A plug-in is a little piece of code that can register itself with the application and catch certain events from it, get access to internal data,and so forth.While integrating seamlessly with the main system,plug-ins still remain isolated files that can be detached and spread separately.They can be attached to the system without having to modify a line of code, which allows a system administrator without any knowledge of PHP to extend the application by using foreign code. Concretely, this is realized as shown in Figure 3.9.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 109 This is exactly what

Filed under: Web Applications Development With PHP4.0 — webmaster @ 05:42

Interfacing the Network 109 This is exactly what we want: the ability to store a Boolean value in a place in memory where every process can look at it. Since shared memory works (as the name suggests) only in RAM,it s extremely fast and requires almost no overhead.With this option, every chat process looks for its own variable in shared memory and only issues a query to the database whenever it finds that variable set by the user-input field. Why is the data exchange still based on a database at the very end? Try to find some answers. The database is still being used for one main reason. Shared memory is not supported by default in PHP; you need to specifically compile support for it into PHP. However, many people with access to a PHP-enabled server don t have the option of recompiling PHP because they only rented space on the server, because they don t have sufficient rights, or maybe because others depend on a certain setup of PHP. Leaving the database in as the final data-exchange path makes use of shared memory as an optional optimization. People who can t use it can simply disable it and still have a fully working version of the chat server operating at suboptimal performance, but operating. When creating an application designed for widespread distribution, keep in mind that not everyone will have the same setup as you and probably not the possibility of re-creating your very special setup. Even though PHP is 99% system-independent, some things do depend on the system. Carefully calculate whether enforcing certain circumstances is worth a potentially huge loss of customers. Interface to the User Now that we moved all the tricky parts with the data exchange out of the way, the actual HTML interface to the user is trivial.We know how to accept input from the user and how to deal with network communication.The last problem is packaging the generated output for the user in a convenient way. HTML offers only one way to have different windows act independently in one browser view:framesets.The interface typically consists of the user-input field; the chat output field; a nickname list (or just nick list), which shows other participating clients in the same room; and an action panel to allow one-click control over the chat for actions such as nickname changes,joins,parts,quits,and so on.These activities can all be handled by single processes whose output will be integrated into a frameset.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

106 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 00:18

108 Chapter 3 Application Design: A Real-Life Example What are the reasons? Try to find and write them down.Try to find the critical points this is crucial when having to optimize later on. A chain is only as strong as its weakest link, and software is only as fast as its slowest inner loop.The process of finding these bottlenecks is called profiling and is extremely important. When using a database, the bottleneck is the database: the time required to invoke the database, let it execute the (relatively small) query, retrieve the result, and determine what to do next (called the overhead) is pretty long compared to the result we re getting. In other words, we re using a huge software system designed for complex data storage to exchange simple, Boolean values if there s something a database was not designed for, it s this. No wonder it didn t perform optimally; the bottleneck is the overhead, the time required for setup and deinitialization. The file system performed badly because it was not designed for this usage, and because of other limitations: PHP doesn t include optimal file-system access methods. Determining the existence of a file requires constant cache invalidations and recaching again, large overhead for a trivial task. So why not use something completely different? We re surely not the first people having to deal with interprocess communication; others must have come up with good solutions for this already.And so we reach the next possibility:semaphores. Semaphores do exactly what we want to do:They work as signals.Semaphores are counters stored in shared memory.You can acquire a semaphore and thus increase its counter,and you can release a semaphore,decreasing its counter.Additionally,there s the possibility of waiting for a semaphore to become free, meaning that its counter falls back to zero.This option has one drawback,however:Semaphores were meant to lock resources, to create some kind of scheduling mechanism allowing many processes to wait for available time on a device,or something similar.Whenever you re waiting for a semaphore to become free, the process that s waiting is put to sleep and cannot perform other tasks. If the main process was waiting for the user-input field to signal a new message, it would sleep and couldn t process the incoming network traffic. No reason to give up yet; people have come up with still other solutions. Setting Flags in Shared Memory Shared memory is similar to semaphores, but a bit more versatile; shared memory is memory that s available to every process in a system. Multitasking systems are usually designed in such a way that each process is running completely isolated from other processes for security reasons. Different processes can share data by setting up and connecting to special memory blocks, namely shared memory blocks.These blocks can then contain variables (or any other kind of data, but PHP only supports storage of concrete variables).

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

106 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 00:18

106 Chapter 3 Application Design: A Real-Life Example Main process Input field Data exchange container (”door”) Figure 3.7 Upstream communication. The situation can be compared to a car race.The driver racing on the track is the main client and the racing team in the pit is the user input field.The driver is bound to the race he s in; he can t just leave the track and stop to see what s going on. Whenever the racing team flags him in for a pit stop, they interface to him giving him a signal to make a break after the next lap. What s being done is (leaving radio communication aside) to signal every time the driver passes the finish line.This signal works as the interface to the driver.Basically this is what we need to do,too signal to our main process.Since the main process is event-based, we frequently get the chance to take control over the application and do what we want to do.This means that we can install a handler that looks frequently for a signal from the outside.The method to periodically stop and check for incoming data is called polling and will be the preferred method for phpChat. phpIRC features idle callbacks, which get invoked every time phpIRC has nothing to do and simply waits for something to happen on the network.Tagging a handler to this event enables us to watch out for a signal. Now, how are we going to signal something? This is actually pretty easy, using one of the following methods: n Set a flag in a database. n Create a lockfile in the file system. n Use semaphores. n Set a flag in shared memory. These are basically the methods that we have with PHP to leave a message. The following sections describe each method. Pipes can t be used for interprocess communication here, because a pipe requires two processes to be running at the same time. Our situation requires interfacing one constantly running process from other, short-term processes. Note: Of course, more exotic methods are available, such as sending emails between processes.We ve seen people doing this,but we won t go into that option here,as the disadvantages should be clear to the reader. Setting a Flag in a Database Setting a flag in a database is probably the de facto standard method for PHP users: Connect to a database, leave some data in it, let it be processed further by other processes.This method is extremely easy to implement and is available on all systems, but has a disadvantage. Can you tell what the disadvantage is?

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 103 After doing initialization and

Filed under: Web Applications Development With PHP4.0 — webmaster @ 19:27

104 Chapter 3 Application Design: A Real-Life Example columns of the table to be present completely to determine the final size of the table. As long as you restrict yourself to outputting text lines one after another, and only use tables when you can print them all at once, everything works fine. Quirks such as streaming HTML are common tricks that you should know. Always keep yourself informed about such things. Streaming HTML also has one implication that some see as drawback and some as advantage: Since the client connection stays open, there must always be one server process handling it.This means that every client requires at least one Web server process to be running only for that client.The advantage is that no overhead per hit occurs. Usually, when the client requests a document, a new process has to be spawned; the script generating that document has to be loaded, parsed, and executed; and finally, the data has to be sent. Since the server process now remains in memory, however, spawning, script loading, and interpretation only have to be done once per client. On sites that would otherwise have hundreds of hits per second, this might be a definite advantage. However, each process now stays resident in memory and demands RAM for itself on Intel x86 systems equipped with Linux,Apache,and PHP 4.0, such processes tend to be as big as 2MB each. Consequently, a small server with minimal RAM on board would soon start to run from swap and that means death. Note: Swap memory is virtual memory that s meant to extend the RAM the physical amount of memory on a computer. Swap memory is stored on a hard disk, which is extremely slow.When physical memory is all used up,modern operating systems start allocating new memory in the slow swap memory. If a chat server gets hit by a lot of clients at once, which eat up all physical memory and start running in swap memory, the operating system will constantly have to exchange parts of the RAM with parts of the swap memory (since programs can t be executed from swap memory),and this starts a cycle of death :The operating system notices that a process in swap needs to be run and loads it into RAM, but has to put another running process from RAM into swap. It executes the process in RAM but finds the old process (now residing in swap) has to be run, so it swaps it back into RAM, and so on and so on.You can quickly kill a server this way, forcing it to be reset or taken off the net. By the way, this is also a common denial of service attack, a bit similar to the ones that Yahoo! and others were exposed to earlier this year. Would you have thought about the implications of resident processes? If not, make sure you do next time! Keep evaluating every situation fully. Upstream Communication Upstream communication that is, accepting user input and sending it to the network is the next stage to consider.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 103 After doing initialization and

Filed under: Web Applications Development With PHP4.0 — webmaster @ 19:27

Interfacing the Network 103 After doing initialization and setup, the application has to surrender control to phpIRC. phpIRC then enters its main event loop and waits for something to happen. During setup, the application has to register callbacks for each event it wants to process (for example, incoming private messages, incoming server messages, and so on). These callbacks are the only possibility for the application to regain control. phpIRC then dispatches all events to all functions that have registered themselves with the library.These functions can in turn enter another idle loop in phpIRC to wait for another event to happen, or they can use phpIRC s API to perform certain actions on the network (send private messages, join/leave channels, and so on). This very basic layout already allows for downstream communication, which means that phpIRC is able to receive messages from other users. People could actually talk to your script. Note: Downstream means from the network to the user. Upstream is the opposite, from the user to the network. Exercise for the Reader Structure a downstream interface that makes use of phpIRC s features. Implement it on paper to become familiar with phpIRC s API.Then build a simple downstream interface that logs onto IRC and displays all messages from a specified channel. Downstream Communication Since chatting is a real-time task, meaning that it happens as you do it and causes instant replies, we don t want to introduce latency into the interface. Latency describes the reaction time of the interface; for example, the time from the point when the reader presses the Enter key to submit a message until it shows up in the chat window. Even though a latency of less than a second might objectively be a very short wait, it seems extremely long and annoying to the user. Ergo, incoming messages must be displayed at once (or at least as soon as possible). HTTP is a stateless protocol, however, and doesn t allow instant updates of pages without reloading a complete document. Of course, there are multipart documents and automatic refreshes, but these options introduce a very nasty flicker each time the page loads again, require database buffering for output, and introduce lag because of constant reconnects and data transfer from the Web server. One solution is streaming HTML, something that s not officially supported anywhere,but works nevertheless:The script that does the interface output simply idles in an endless loop and doesn t terminate the HTML page the browser is receiving.When something has to be sent to the user,it s printed and immediately flushed from the server s buffers.This way,the browser is constantly rendering and always displays the most up-to-date data. One problem persists in this approach, however; no complex HTML entities can be rendered on the fly. For example, you can t output the rows of a table one by one, because the browser requires all rows and

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

100 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 13:35

102 Chapter 3 Application Design: A Real-Life Example n From one central point of the program, all data can be dispatched to all recipients transparently.You don t have to worry about manually copying and transforming structures; each event handler takes care of receiving its data on its own. n New code can be plugged into the application extremely easily, just by creating a procedure that registers itself to the appropriate event. Thus, once the main event-dispatcher framework is created, the whole application can be created by writing handlers, handlers, and more handlers. Get familiar with the techniques used to implement finite-state machines.These are elemental in programming and information processing in general. Luckily, the event-dispatcher framework is already contained in phpIRC, so we won t need to do that programming for this project. Interface Structure phpIRC forms the IRC client part of the application and is responsible for all network communication.This means that it also needs to be in control all the time to be able to react to network messages in a timely manner. If phpIRC s message- processing functions were activated only occasionally, safe, secure, and speedy communication couldn t be guaranteed. For this reason, phpIRC forces a special program layout, as shown in Figure 3.6. Main application Program entry point Passing control to phpIRC phpIRC Initialization Setup Incoming network data Outgoing network dataCallback 1 Callback 2 Callback 3 Callback n . . . Figure 3.6 phpIRC s forced application layout.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Fitting the Application into the Network 97 Multiplexing

Filed under: Web Applications Development With PHP4.0 — webmaster @ 07:29

Interfacing the Network 99 Interfacing the Network As we mentioned earlier, IRC requires some processing overhead. Hacking a complete protocol handler for interfacing with IRC is a bit of a complex task, but we favored IRC instead of the database-backed solution because an API already exists that does this work for us. Know the market! It s essential for every programming project to know which parts have already been done by other people and which still need to be done. Never reinvent the wheel! Especially for commercial projects, it can pay off tremendously to buy foreign bulletproof solutions for specific tasks, rather than design and develop one yourself.The latter is sometimes more expensive and much more time-consuming. On top of that, external solutions are usually constantly being improved a process that s totally independent of the progress of your own project. By receiving an upgrade from an external company, you simply replace a part of your application with a newer version.This way,you can upgrade certain parts of your application without having to put your own work into the changes. Plus, when using existing libraries, you automatically agree to build your project on common, standardized APIs, which is always a great benefit. On the other hand, binding yourself to foreign products can prove to be a negative decision if the producer fails to improve the product or keep it up to date, as well as if bugs in it aren t corrected. In our experience, Open Source products have been the most successful external parts to be integrated. Open Source products are being improved and extended extremely rapidly and are usually oriented at common and open high-potential standards. Exercise for the Reader Search for applications/libraries written in PHP that make use of IRC and compare them in terms of design, flexibility, and ease of use. Of course, the implementation is also interesting (but shouldn t be your main focus). The design is always the most crucial part of development; after the design is finished, the actual implementation is usually straightforward and easy to do (even though a lot of programmers think differently).

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Comparing Technologies 91 When designing an application with

Filed under: Web Applications Development With PHP4.0 — webmaster @ 19:36

Comparing Technologies 93 Of course, this isn t as powerful as we d want it to be, but it s a start. Next, we could surf the Web to look for pages that have one of the (nowadays almost obligatory) chat links.Although they differ widely in look and feel/implementation,most of them can be boiled down to the following: n Java for fancy interfaces, although some use plain HTML. n A proprietary protocol with a single server (or simply database-backed). n Few predefined rooms. n Few predefined commands. Apart from these chat setups, there are chat applications and networks such as Mirabilis ICQ or the diverse Instant Messaging Systems systems that don t always provide real-time services and generally require additional proprietary client software to be installed on every participating system. However, one system stands out from the list. IRC (Internet Relay Chat) is a widely- known and long-used chat protocol used by many networks, some of which carry hundreds of thousands of users simultaneously.The IRC protocol is text-based a drawback when operating under high load (long string commands generate much more traffic than single binary characters), but this also makes it significantly easier to process. Most current IRC servers support compressed backbone links, which greatly reduce traffic. Although IRC requires special client software on every participating system, we can tweak this requirement to our advantage:Why not provide the client software ourselves server-side, and abstract it by using an HTML interface and allowing each user access to the network through an HTML client? This would give us control over what the user can do (each user is required to use our HTML client).Additionally,we have all the advantages of an existing network system: reliable client software, proven concept,hundreds of tools,etc.We could even allow users to use their own client software an option to be avoided in most cases, however, as we want to create a closed chat network. On a closed network, you know every way that each client can access your network. By limiting the access points to specific setups, you greatly reduce the risk of being attacked. This directly leads to the question, do we need a real protocol such as IRC? Or would it be sufficient to simply use a database-driven protocol, with a remote synchronization feature to provide the requested networking abilities? Questions such as this will arise every time you plan an application, and they ll arise often. Make sure that you ve got all of them covered, and make sure that no questions will arise at a later stage during development. This is the point where you can still address these questions; later on you might be unable to resolve them (and eventually get your project kicked into the trash).A good project is a project without doubts, without uncertainties, without inconsistencies, and without unforeseen eventualities. Make sure that after your planning phase you can assure a stable, fully evaluated situation!

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

« Previous PageNext Page »

Powered by Cheap Web Hosting