Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

114 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 20:32

116 Chapter 3 Application Design: A Real-Life Example Listing 3.4 Continued } ////////////////////////////////////////////////////////////////////////////// // // myplugin_deinit() - deinitializes this plug-in // ////////////////////////////////////////////////////////////////////////////// // // All deinitialization code should go here. This function is called before // the bot goes down; thus, all network connections are still active. // // Although the return value is currently not used, 0 should indicate // deinitialization failure and 1 deinitialization success. This might be // used later on to force delayed shutdowns. // ////////////////////////////////////////////////////////////////////////////// function myplugin_deinit() { // remove callbacks here chat_remove_callback(CHATCB_IDLE, myplugin_idle_callback ); return(1); } ////////////////////////////////////////////////////////////////////////////// // // NOTE: DO NOT CHANGE ANYTHING BELOW THIS POINT! // ////////////////////////////////////////////////////////////////////////////// // installer code starts here // register initialization function chat_register_plugin_init($plugin_init_function); // register deinitialization function chat_register_plugin_deinit($plugin_deinit_function); // installer code done ////////////////////////////////////////////////////////////////////////////// ?>

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

PHP, MySQL, Java, Unix Cheap Web Hosting

114 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 20:32

114 Chapter 3 Application Design: A Real-Life Example As you can see, plug-ins offer an extremely powerful way of extending a complex system. Consequently, phpChat has abstracted most of its own internals into plug-ins as well.The complete command interpreter has been moved into a plug-in,as well as the complete set of text formatting/printing procedures.This means that there is only a solid kernel that doesn t have to be changed because there s simply nothing in there that would require changing the rest can be freely modified, extended, even removed, without any impact on system performance or operability. Have you ever seen an application that doesn t complain about someone deleting its files? Using this technique, an application won t complain and will even dynamically adapt to it. Plug-ins can be used in many ways, not just for chat programs. For example, you could also build a portal site consisting of the traditional news page, an email interface, etc. Using plug-ins, you can design a site kernel that handles all basic issues such as providing page layout, database back end, sessioning, and so on. Based on the site kernel, you can then create plug-ins for displaying news, sending and receiving email, even for providing different methods of logging in. Even if it s quite an effort, we encourage you to create a plug-inbased application as an exercise. It will be worth the work. Listing 3.4 shows a plug-in template implementing a dummy plug-in as code base for new plug-ins. Listing 3.4 A plug-in template.

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

PHP, MySQL, Java, Unix Cheap Web Hosting

114 Chapter 3 Application Design: A Real-Life Example

Filed under: PHP Web Hosting — webmaster @ 20:32

Interfacing the Network 115 // This is an example for a callback function. See below on how to register // and remove it from the call chain. // // $code specifies the reason for invocation, $parameter contains all callback // information. // // The return value should always consist of a modified or unmodified version // of the input parameter $parameter. The return value is used as input // parameter for the next callback. This allows for multi-stage message // processing and such. // ////////////////////////////////////////////////////////////////////////////// function myplugin_idle_callback($code, $parameter) { return($parameter); } ////////////////////////////////////////////////////////////////////////////// // // myplugin_init() - initializes this plug-in // ////////////////////////////////////////////////////////////////////////////// // // Put all your initialization code in here. This code will be called as soon // as the main bot is all set up with connecting and callback installation; // thus, you can rely on a safe environment. // // Although the return value is currently not used, 0 should indicate // initialization failure and 1 initialization success. This might be used // later on to enable plug-ins to stop the current chat session right after // login. // ////////////////////////////////////////////////////////////////////////////// // // Return value: // 0 - error // 1 - success // ////////////////////////////////////////////////////////////////////////////// function myplugin_init() { // register callbacks here chat_register_callback(CHATCB_IDLE, myplugin_idle_callback ); return(1); continues

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

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 111 Interface to HTML Developers

Filed under: Web Applications Development With PHP4.0 — webmaster @ 14:01

Interfacing the Network 113 To be useful in the application, phpChat offers a set of events to which each plug- in can attach itself. During plug-in initialization, each plug-in tells phpChat to send a set of desired events. Events might include the chat being idle, the user submitting a new message, the user clicking on a nickname in the nick list, an incoming message from the network, and so on. At runtime,the plug-ins can intercept these events and perform certain tasks.The clock plug-in, for example, registers itself to the idle event and checks the current system time frequently.After a predefined number of minutes,it announces the time to the user. For most events, phpChat also sends parameters (such as the message texts for incoming messages), which the plug-ins are allowed to change. For example, the list of plug-ins in Listing 3.3 includes plug-ins named htmlspecialchars and link_transform.These plug-ins change the output of messages; htmlspecialchars applies a call to htmlspecialchars() to all printed text (for security reasons, so that no one can insert malicious HTML code into the chat), and the link transformer detects all URLs and email addresses and prefixes them with or mailto:, respectively, so that users can click links right in the chat window (see Figure 3.10). Figure 3.10 The plug-ins at work.

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 111 Interface to HTML Developers

Filed under: PHP Web Hosting — webmaster @ 14:01

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 MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 111 Interface to HTML Developers

Filed under: PHP Web Hosting — webmaster @ 14:01

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 MySQL Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

108 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 06:09

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

108 Chapter 3 Application Design: A Real-Life Example

Filed under: PHP Web Hosting — webmaster @ 06:09

110 Chapter 3 Application Design: A Real-Life Example The main process, also responsible for the chat-output streaming, will keep state information updated in a database that all other interface components can access, retrieve, and display in a suitable fashion (see Figure 3.8). Outgoing network data via shared memory Incoming network data Main IRC process Interfacecomponents(nick list, etc.) User input via database via database via frameset HTML output to user Figure 3.8 The final application layout. Interface to the Developer An interface for developers? What does this have to do with chat? And how is it supposed to work? Typically, most applications suffer from the disability of being solid, meaning being either completely unmodifiable or difficult to modify by foreign developers. In terms of end-user-oriented software (for example, desktop environments such as Windows, KDE, MacOS, etc.), hardly anyone will ever find the ideal solution.Similar to a chat system,most people who download it say, Hey,great, but it lacks this and that, or Cool, but I don t like the way it does xyz. Without an easy, clearly exposed path for modification by anyone using it, most applications end up in the trash. Most people won t even try to work on a program they didn t develop themselves if the ease of doing so doesn t hit them right in the face. This means that for the chat application to consistently enforce independence of code and interface layout (allowing an interface to HTML developers) and to consistently enforce independence of data-processing steps, we need to create a solid application core (the part of the application that nobody should ever need to change) which interfaces to a distributed set of plug-ins (the part of the application that most people will want to change somehow). Think again about the importance of these enforcements.Would you like an application to be designed like this? Would you even need it? Think about how this could be realized.

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

108 Chapter 3 Application Design: A Real-Life Example

Filed under: Web Applications Development With PHP4.0 — webmaster @ 06:09

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 Web Hosting services

PHP, MySQL, Java, Unix Cheap Web Hosting

Interfacing the Network 105 Here s the hard part:We

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

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 Web Hosting services

Next Page »

Powered by Cheap Web Hosting