Monthly Archive for July, 2008

MySQL Socket Error Fix - A Note of Help

Sometime or other you’re going to come across this error when trying to start MySQL from the Mac Terminal:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

This is very annoying and can keep you from using mysql for days and days if you don’t stumble across the right forum.

If you’ve installed mysql according to the recommended Hivelogic tutorials (either manually or through the mysql mac package) and you don’t want to read the manual to see how everything works, just remember that starting and using mysql on the mac are two different things…

When you type mysql into the command line, you’ll likely get that error:

$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This happens for many reasons… Maybe you forgot to set up the Library Daemon for autostartup of mysql when you turn on your computer, so when mysql is somehow shut off, it doesn’t start up again the same way. Or maybe mysql was started twice somehow. If the latter was the case, you can try these few commands:

$ ps -aux | grep mysql

This will show you all processes (ps) that have the string “mysql” (grep) in there. If there are multiple processes, get the process ID and kill it by using the following:

$ sudo kill -9 process_id_number

If there aren’t multiple mysql’s running, then it might be because mysql hasn’t actually been started up. The way to actually startup your mysql from the command line is to use the mysqld_safe command. You’ll have to use sudo to make this work. Once you type this command, you’ll get something like this:

$ sudo mysqld_safe
Starting mysqld daemon with databases from /usr/local/mysql/data

If you want it to run in the background so you can keep using the same terminal for typing commands, add an ampersand to the end of the command:

$ sudo mysqld_safe &
Starting mysqld daemon with databases from /usr/local/mysql/data

Once you type this, you can now type mysql into the command line and you will be welcomed into the mysql command-line environment! Now back to mysql.

Note: If you haven’t set your unix PATH variable so that you can type the mysqld_safe or mysql commands anywhere, you can set that path permanently without damaging your computer in any way by editing the file called profile in the /etc directory. Add the path to where the mysql commands are located on your system (if you used installed it using the Hivelogic tutorial above or the mac package, it should be in the “/usr/local/mysql/bin” directory):

$ sudo vi /etc/profile

This ancient vi text editor is kind of strange, but this is what the original computer people used. When you type that command above, you will see the file, but you cannot edit it, or “insert” text. First, use the arrow keys to guide you over to the right spot in the text file. Then, to insert text, type the letter i. This lets you insert text. Here’s the file:

# System-wide .profile for ...

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi

Where it says “PATH=”, add the following to the end of that line (before the quotes):

:/usr/local/mysql/bin

Your profile file should now look like this:

# System-wide .profile for ...

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/mysql/bin"
export PATH

if [ "${BASH-no}" != "no" ]; then
        [ -r /etc/bashrc ] && . /etc/bashrc
fi

Exit vi by first pressing the “esc” key to get out of “insert” mode and then type the colon key followed by a lowercase x. Press enter to exit and you will have saved the file.

To learn more about vi, click on the vi link.

Now anytime you type mysql into the command line (assuming the mysql server has been started), you will be welcomed into the mysql environment. Peace.

Share This Post

If you enjoyed this post, make sure you subscribe to my RSS feed!

Framework for Custom UIComponent in Flex - Part 2

Table of contents for Flex Framework

  1. Framework for Custom UIComponent in Flex - Part 1
  2. Framework for Custom UIComponent in Flex - Part 2

Understanding the Core Classes

The foundational package for building Flex UIComponents is Adobe’s widespread enterprise level framework, Cairngorm. Universal Mind has added several extensions to Cairngorm, including advanced FrontController and Command classes as well as enhanced Events and View Notification patterns. Inspired by Steve Reiner from IntegratedSemantics.org and Paul Williams from Adobe, I will also integrate into this Flex UIComponent Framework their work on the Passive View Presentation Pattern, originally conceived by Martin Fowler. Together, these Design Patterns come together into what is called the Model-View-Presenter pattern (MVP) for the Presentation and the Model-View-Controller pattern for the entire component.

In this post I will go over how the classes in these design patterns communicate with each other and how to think about it when building a Flex UIComponent. Check out my last post to get a general overview of how this pattern works.

The Model:

The biggest problem you run into when trying to build a Flex UIComponent is in determining the distinction was between the Model and View. Some places said the Model was a collection of properties while others said it contained business logic. The CairngormStore example (source) demonstrates this too: the shopping cart model has some logic on how to add items to the cart, but it leaves out other things. Compare this Model to the CairngormLogin example whose model contains just a list of properties and a property holding the state; it doesn’t have business logic like the Shopping Cart does in the CairngormStore. So how do you separate out the Model from the View? And how do you determine what the Model should have?

It turns out that you don’t always need a Model. Well that makes life easy! If only they said that from the start… You only need the model if you have global properties or states you are trying to keep track of. In addition, Views often have references to many Models from different components, not just their own! This makes things complicated for the beginner, so keep those two points in mind.

When we build our Flex UIComponents, the Model should consist only of the following:

  1. Properties: Booleans (such as State), Strings, Numbers, Value Objects
  2. Functions that result in Properites: XML Parser (parse XML into properties), adding elements to an Array property, getters and setters, etc.

This makes our life much easier. Let’s use our future DiagramUI component as an example. To keep it simple, say we have an “Edit” button that allows a panel of buttons to become modifiable. What is the Model for this “Edit” button? The only thing that will change globally when the Edit button is pressed in the DiagramUI is the state of the DiagramPanel: it can be either EDITABLE or FIXED. This is a boolean value and thus qualifies as a Model property. Thus, the DiagramPanel Model can be composed of this single boolean state property, though it isn’t limited to this.

Another example… If a DiagramNode on the DiagramPanel were to load something, say a text file, then the DiagramNode Model could start by having a “loadedFile” boolean value. When an Event occurred that asked the DiagramNode to load a file, the DiagramNode Model would change it’s state from “loadedFile = false” to “loadedFile = true”, and the DiagramNode component (View and Presenter) would no longer be able to load a file. One last example… If the DiagramPanel were to load its Diagram Nodes based on an XML file that specified Node titles, locations, connections, etc., the DiagramPanel Model would have a function that resulted in those Node properties, an “initProperties()” function that parsed the XML data into Node properties. These are the types of things that are kept in the Model. Everything else is probably in the ViewBase or the ViewPresenter.

The View:

The View (or Presentation) is a little bit more tricky. It is almost a mini MVC pattern in itself, presumably the reason why it’s called the Model-View-Presenter (MVP). Take a look at Paul Williams analysis of 6 different Presentation patterns to see how tricky this is. Cairngorm has been criticized for having an “Autonomous View”, meaning that the View keeps track of not only its state but also how to change its state, listen for Events, and perform all kinds of functions. This makes the View just one big mess of functions, event listeners, and properties.

The two Presentation patterns I find interesting are the Supervising Presenter and the Passive View. Download the two and compare the differences between their MVP implementations for reference. Both keep the state in the View and the logic in the Presenter, but in the Supervising Presenter the View.mxml file uses data binding to be updated, and so some of the state-changing logic is held in the View class. Williams says this makes Unit Testing more difficult as it more tightly couples the View to its Presentation logic. The Passive View, on the other hand, only allows the View to specify static properties, and so the state-changing logic is held in the Presenter class instead of the View.

For our purposes we will call the traditional View an MVP and focus on the Passive View implementation of the MVP. There is also a Presentation Model pattern (compare this to the Supervising Presenter and Passive View patterns), but you have to include some Actionscript code in the MXML file and that just makes things ugly and complicated. Nevertheless, the Passive View MVP is divided into three parts :

  1. View
  2. ViewBase
  3. Presenter

The View is an MXML file that has two main functions: add a visual object, the ViewBase, to the computer screen, and let the Presenter update the ViewBase any time there is some Event. That means it can have one or two lines of MXML code, or, if you want to specify some static properties (size, id, title, etc.) you can add those too. The View has the UIComponent’s state but not its logic.

The ViewBase is very simple too. I include the ViewBase because Steve Reiner’s FlexSpaces architecture is excellent (Paul Williams hasn’t included it his Album examples). Check out how the View, ViewBase, and Presenter work in his FlexSpaces project and compare it to Paul Higgins work to see how all this works. The ViewBase is an Actionscript file that either simply extends another Flex UIComponent or adds to the UIComponent by creating public variables that reference other components needed to build your ViewBase. A FolderViewBase that lists folders will simply extend a UIComponent (Canvas, Panel, etc.) and have public variables for a VBox, Grid, FolderDisplayBase, whatever. Essentially it is just a list of variables. The ViewBase is added to the display in the View.mxml class and modified in the Presenter.

The Presenter is the most complex of the MVP trio.  The Presenter is an Actionscript class that takes into its constructor the ViewBase class and performs functions on it according to user input and other events.  In the case of our DiagramUI Flex UIComponent, the Presenter will listen for events (such as a ChangeToEditable Event) and will 1) change the Model of the DiagramPanel by changing it's boolean state property and 2) animate or modify the View's state accordingly (such as making certain buttons functional and other not, or changing the color of the back panel).  The Presenter, thus, knows about the Model and the View (really the ViewBase since the View just displays the ViewBase), has all state-changing logic, and the View doesn't have to worry about a thing; the View doesn't even know the Presenter exists.

The Control:

The Control package consists of five main parts:

  1. Events
  2. Controllers
  3. Commands
  4. Delegates
  5. Services

Cairngorm does this differently by dividing its packages into Business (Delegates and Services), Commands, Control, and Events, but Steve Reiner’s style as he did it in FlexSpaces is more intuitive. After some UserInputEvent on the View, an Event is dispatched from the Presenter class. This Event is handled by a Controller, elsewhere called the Front Controller, Boundary Controller, or App Controller. When the Controller receives the Event, it instantiates the appropriate Command and calls “execute()”.

Event

public class CustomEvent extends CairngormEvent
{
	//type:String
	public static const EVENT_TYPE:String = "event_type";
	public static const ANOTHER_TYPE:String = "another_event_type";

	//If you want to attach properties or information to an event,
	//you can do that easily!
	public var customValueObject:ValueObject;

	public function CustomEvent(type:String, vo:ValueObject)
	{
		super(type);
		this.customValueObject = vo;
	}

	override public function clone():Event
	{
		return new CustomEvent(type, customValueObject);
	}
}

Controller

public class MySpecificController extends FrontController
{
	public function MySpecificController()
	{
		//The addCommand() function is handled in the FrontController super class.
		addCommand(CustomEvent.EVENT_TYPE, CustomEventCommand);
		addCommand(CustomEvent.ANOTHER_EVENT, CustomEventCommand);
		addCommand(AnotherEvent.EVENT_X, AnotherCommand);
	}
}

Command

public class CustomCommand extends Command
{
	public function CustomCommand()
	{
		super();
	}

	override public function execute(event:CairngormEvent):void
	{
		//this is where you change the model...
		model.loadedFile = false
		//or model.oldState = newState

		//If you were wondering, if you had a delegate, this is the
		//general form using Universal Mind's Cairngorm Extension
		//var handlers:Callbacks = new Callbacks(onSuccess, onFault);
		//var delegate:CustomDelegate = new CustomDelegate(handlers);
		//delegate.doSomeAction(event.valueObject);
	}
}

The Command can do either of two things from here, depending on your requirements. If you do not need to access remote services (such as a web sites RSS feed through HTTP requests, or a Java class through an open API like Alfresco’s Java API), then your Command can simply change the Model. In the case of our DiagramUI, a ChangeToEditable Event would invoke the ChangeToEditable Command that would then change the DiagramPanel “edit” state from true to false. The Presenter, which is observing the Model, will immediately be notified of this change and will then update the View (through the ViewBase) accordingly. If, on the other hand, your Command needs to access a remote service, the process is a bit more complex. This requires a Delegate.

The Delegate:

Universal Mind’s Cairngorm extension handles remote calls much more powerfully than Cairngorm’s delegate itself, but we won’t get into this now because we won’t be accessing remote services or doing any HTTP method calls. We will come back to this when we check out FlexSpaces for Alfresco. Delegates deserve their own post.

Summary:

What I have just outlined is a reusable framework for building Flex UIComponents that consists of the following:

  1. Cairngorm’s MVC Framework
  2. Passive View MVP Pattern
  3. Universal Mind Cairngorm Extensions

With this you can start thinking about how you would model anything you want, from a picture gallery built in Papervision 3D that gets images from Flickr to a Google Search Flex Display that accesses Google’s RESTful through HTTP requests and displays them in a cool mind web thingy. ANYTHING, you can model it according to this framework!

Coming up Next…

In the next post we will begin modeling our Diagram User Interface that will allow you to build a hierarchical diagram with crazy functionality. We will work through how to model it using this Flex UIComponent framework and will see just how amazing all this stuff really is. Until then…

Note: The CairngormStore and CairngormLogin examples don’t implement either the Universal Mind Cairngorm Extension or the Passive View MVP pattern. They are there to show how the Control sequence works.

Share This Post

If you enjoyed this post, make sure you subscribe to my RSS feed!

Framework for Custom UIComponent in Flex - Part 1

Table of contents for Flex Framework

  1. Framework for Custom UIComponent in Flex - Part 1
  2. Framework for Custom UIComponent in Flex - Part 2

Consistent Framework for Building Custom User Interface Components in Flex

It trips me out that there are not really any standards for creating completely customized UIComponents in Flex yet. There are some great patterns emerging out of the traditional Model-View-Controller (MVC) pattern as shown by Paul Williams on his site, but it’s still up in the air. I’ve spent the last month piecing together info from different blogs and such to create custom UIComponents, but there is no “one solution”. In these next few posts I plan on outlining a reusable and scalable framework that can be used to build Flex or AIR components that emphasize not just data access but also its Presentation. We want hardcore animations in 3D, not just lists of text in a table. This is a work in progress, as design patterns are never “complete”. Use this as a catalyst to build completely customizable user interface components in Flex or AIR for your software and web projects and we’ll help usher in the immersive and interactive age full force.

The Framework:

This framework should ideally be:

  1. Scalable - You can easily build hundreds of unique and modular components to use in any software or web application using the same methodology.
  2. Extendable - You can easily integrate animations from adobe after effects, papervision, maya, or flash, add custom state machine animators, create highly complex AI signaling networks.
  3. Reusable - Once you construct the user interface component, you can simply grab the main directory of the component and transfer it to a new application, getting rid of the need to modify the component to fit individual software needs.
  4. Fast - Right when you come up with a new component idea it will be very easy to model out the classes and behaviors required to build it. Every component will be easily mapped onto this framework.

The Current State:

Currently, when you want to build an amazing user interface in Flex with beautiful graphics and cutting edge interactivity, you must fuss around with archaic Documentation, scour tons and tons of blogs, forums, and wikis, not to mention model everything out to see how you want things to function. After the days and weeks of figuring out how to build the interface, you try to piece together your new knowledge into your dream product. What ends up happening is that, if you even manage to make a final product, it is very poorly designed and thus not scalable, reusable, or extendable. It somehow just works and that’s all we can hope for. The internet is flooded with these products!

This is a serious problem. First, it prevents standardization. Without standardization it is extremely difficult or impossible to integrate software components that were made by different people. Wordpress, for example, is poorly designed from this perspective because you can’t make a completely custom user interface (UI) using anything but their own Theme system. They could fix this by adding some sort of Service API, but they’ve become so popular that people adapted to their poorly designed solution. Though there is a huge community of people making Wordpress Themes with cool layouts, you are bound to use their system if you want to build a custom UI and thus have to learn their technology. This is a waste of time, money, and energy, and it prevents standardization.

Second, because their is no “one way” to design a user interface component, you often have to learn from scratch how to build what you have in mind every time you create something new; it seems like a whole new project. This is bewildering, especially if you are busy with school, work, or your family, not to mention if you are working on an important software project. It should be almost plug and chug.

The Desired Future State:

By the end of this short series of blog posts, you can begin very quickly building hardcore user interface components that are scalable, reusable, and extendable (in Flex or AIR of course, because I love Actionscript and Adobe, but the pattern is universal). You can even incorporate After Effects, 3DS max, or Maya animations right into the components design! Anyone building Flex UIComponents with this pattern can swap their components with other people and, in no time at all, we can begin building huge and amazing Enterprise Applications across networks!!! That would be pretty f**k’n cool.

The Pattern for Building Custom UIComponents in Flex:

This pattern for building custom UIComponents in Flex is based on Cairngorm’s MVC framework which itself is based on J2EE Design Principles. It separates the UIComponent into a Model, a View, and a Controller, with Events and Commands (operations) that can be performed on the UIComponent. This is extended by using the emerging Presenter Patterns

Elements of the UIComponent Design:

  1. View.mxml
  2. ViewBase.as
  3. Presenter.as
  4. ViewEvent.as
  5. FrontController.as
  6. Command.as
  7. Model.as
  8. Style.css
  9. Delegate.as
  10. Service.as

View: Simply instantiates the component onto the screen and listens for changes from the Presenter.
ViewBase: The Base properties of the component such as its size, shape, and what other component it extends.
Presenter: Contains all of the View’s functionality, such as event handlers and dispatchers, animations, and states. Dispatches User Input events to the Controller, initializes styles, listens for changes in the Model, and updates the View.
ViewEvent: Custom Event class (or prebuilt event classes such as MouseEvent, etc.) that can pass (send) any variables to the FrontController.
FrontController: Listens for events dispatched from the view, instantiates the proper command to handle the event.
Command: Performs actions on the model according to the event and event.variables it is passed. The model may be a Button and the command may be a SetStyleCommand to change the skin of the button when a user performs some action.
Model: Data properties of a UIComponent or Object. So, for example, a PanelModel could have a state property that switches between EDITABLE and FIXED, and that’s it. The Command would change this, allowing the Presenter to change the functionality of the UIComponent accordingly.
Style: Defines the initial settings of a UIComponent. This is changed dynamically with the SetStyleCommand. We’ll go over this in a later post.
Delegate: Used only if model accesses outside data, such as a Java Service or a Database or something. Often uses Flex’s RemoteObject or HTTPService classes.
Service: We don’t need to know about this now… Basically, if you are using the RemoteObject class, you can access Java or other classes and return the results to the Delegate.

How the Elements Fit Together…

Take a look at
Warning: gzuncompress() [function.gzuncompress]: data error in /home/.zwingli/lancorn411/systemsofseven.com/blog/wp-content/plugins/easyswf.php on line 255
to see how everything flows together. (We will be adding to their design later). It might be helpful to read this article that explains in detail how to implement an advanced Login Panel using Cairngorm in Flex.

First, we have the View–the UIComponent. This can be instantiated in an MXML file as the View. The View handles user input through its Presenter. When the user changes the value of the View (by moving a slider for example, or a mouse-click), the Presenter will dispatch an Event (such as MouseEvent.CLICK or CustomEvent.LOGIN) to the FrontController. The FrontController then instantiates the appropriate Command and calls its “execute()” function. The Command holds all the logic for manipulating the Model to which the View’s Presenter has a reference. The Command can also ask Delegate objects to get and use remote services, but we’ll talk about this later. The Presenter, finally updates the View and the View displays the changes for you to see!

The Idea…

It would be amazing if, when we are formulating new ideas, it only took a few seconds to build those idea into visually pleasing interactive diagrams. Granted, you could mash together a quick interactive diagram using Flex or Powerpoint and make it work. But you would have to do this every time you created a new diagram and would be causing yourself tons of extra work each time. In order to streamline the process, we are going to use Design Patterns. If you aren’t familiar with design patterns, read “Head First Design Patterns” and you’ll get real excited about them.

What we are building…

First, we will build from scratch a very powerful and reusable system for creating any new User Interface (UI) component in Flex. This requires a general understanding of Design Patterns, Flex’s MVC framework Cairngorm, Martin Fowler’s Presentation Patterns, and how to create Custom Events.

We will also need to understand Skinning in Flex, in specific, Stateful Skinning. I am not a fan of CSS (Cascading Style Sheets) because they are just blobs of memorized data to give set properties on UI components and require too much manipulation to make them dynamic (aka, interactive). Thus, we will be figuring out how to minimize our use of CSS all-the-while creating fully dynamic and interactive styles for our Flex UI Components.

In the end, we will have built an Interactive Diagram of what we have built! :D

Share This Post