Sunday, June 28, 2009

Objective C - Quick Notes

So, made it through the first 2 chapters on Objective C last night. Here is what I remember. Many of these notes are in relation to java.

General Information



  • Objective C is an extension of C

    • Procedural roots: You don't have to be OO and some libraries like Open GL will be strictly procedural.

    • Pointers: Sort of like references in java. Yeah I know, gross oversimplification. Will elaborate later.

    • Memory management/GC: You have none in iphone apps

    • typedefs: Used to define aliases for types, typically enums

    • You may use C constructs like structs, functions and variables.



  • Objective C is a fully object oriented language

    • encapsulation

    • inheritance

    • polymorphism



  • NSObject is the base object that all Objective-C objects should extend from

  • Classes are objects in and of themselves and operate as factories: You message methods on the class to instantiate objects.
  • There is no concept of a garbage collector, at least on the iphone. You have to handle all memory management.



Data Types

































type modifiers examples string formatting
int
long

long long

short

unsigned

signed


10

010

0xF

15L


%i, %li, %hi

%o, %#o, %lo, %#lo, %ho

%x, %#x, %lx, %#lx, %hx

float

5.5F

1.7e4F


%f, %e, %g
double

5.5

1.7e4

%f, %e, %g
char
'a'
'\n'
%c


C language elements



  • Operators

    • arithmetic, assignment, bitwise and logical operators work like java



  • Flow Control

    • for, while and do while loops work like java

    • continue and break work like java, but don't accept labels

    • goto statement can be used to hit labeled spots.



  • Conditional Statements

    • if/else if/else has same logic & syntax as java.

    • switch/case/default has same logic & syntax as java.

    • ternary operator (condition ? e1 : e2) has same logic & syntax as java

    • Integer expressions are used for boolean values. 0 = false, anything else is true.





Defining Classes - Interface



@interface ClassName : Superclass
memberVariables

methodSignatures
@end


  • This is the interface of the class

  • Typically contained in a .h file

  • Contains

    • member variables

    • method signatures

    • C variables/constants

    • C functions






Defining Classes - Implementation



@implementation [ClassName]
[methodImplementations]
@end


  • This is the implementation of the class

  • Typically contained in a .m file

  • Contains method implementations



Defining Member Variables



Type varname; // variable stored by value
ObjectType *varname; // Pointer to object


Defining Methods



[+|-] (returnType) methodName: (argType)argName {
methodBody
}



  • Method Type

    • + is a class method

    • - is an instance method



  • Return type is in parenthesis and can be a primitive, typedef or class

  • method name uses same conventions as java

  • parameters are specified after a colon. Param type is in parenthesis followed by the name.

  • getter methods follow the convention variableName while setters follow the convention setVariableName



Object Construction



ClassType varName = [[ClassType alloc] init];


  • alloc
    message instantiates the object. Sent to class.
  • init
    message initializes state of object. Sent to object.



Invoking Methods



[receiver message]
[anObject aMethod]
[anObject aMethod:param1:param2]


  • Specify receiver followed by message within brackets

  • Parameters specified after colons



That is what I know for now. I will expand on this later.

iPhone plunge

So, I've been convinced that I need to start developing iphone apps. I mean, iFart sold $70,000+ worth of copies in its first 2 weeks. I'm sure I can come up with some sort of iBodilyFunction app that could bring in some sort of side income, right?

There is a lot of overhead to this. I had to buy a mac and learn new tools in xCode. I have to learn a new language in objective C that is very different from my comfy world of Java. I have to learn a large new API in cocoa. A friend at work who has been doing this for a year or so said plan to make a solid 6 month investment in learning before being able to kick things out.

But, the wild-west entrepreneurial frontier of the app store is hugely appealing. Where else can a guy make hundreds of thousands of dollars peddling their gaseous emulsions? If this were a web app, it would not have made a dime without a marketing campaign that was much more costly and time consuming than the actual app.

So I'm diving in head long, and hope that I don't drown. My first goal is to learn enough objective C to be productive, so I am working through "Programming in Objective-C" by Stephen G. Kochan. So far, it seems like a good book. It would be perfect for someone with very little programming knowledge at all. That is the assumption it makes. As someone who already knows OOP principals thoroughly from java and its C origins, I find myself having to skip a lot of sections that are of little benefit to me. But, I would rather have to do that then have a book that assumes more C knowledge than I have and be left wondering WTF is this code doing?

Thursday, June 11, 2009

PostgreSQL - Allowing remote connections on Ubuntu 8

The following steps were used to allow remote connections to postgreSQL:


  • Declare IPs allowed to connect

    • vi /etc/postgresql/8.3/main/pg_hba.conf

    • add line: host all all 71.199.0.0/16 password

    • This enables any IPs 71.199.x.x to connect remotely and authenticate via password.

    • Refer to pg_hba.conf file reference: http://developer.postgresql.org/pgdocs/postgres/auth-pg-hba-conf.html



  • Specify listen addresses for postgres

    • vi /etc/postgresql/8.3/main/postgresql.conf

    • modify listen_addresses='localhost' line to be listen_addresses='*'



  • Restart PostgreSQL

    • /etc/init.d/postgresql-8.3 restart





At this point I was able to be able to connect via dbVisualizer. Note, this does not enable SSL or any sort of secure authentication. That will be the subject of another post.

PostgreSQL Installation in Ubuntu 8

I've gotten postgreSQL installed on my VPS. I decided on it over mysql primarily because some of the tools (namely trac) work better with it. Here is the process I followed:


  • Install Postgre

    • sudo apt-get install postgresql

    • I chose not to install pgadmin3 as this is running on a remote VPS; I will have no ability to use the graphic command line client. Also, its 68.5 meg size was a little hefty. I will do my administration via command line and dbvisualizer.



  • Set postgres linux user password

    • sudo passwd postgres



  • Set postgres user password

    • sudo -u postgres psql postgres

    • \password postgres



  • Create a database

    • CREATE DATABASE [name];

    • list databases: \l
    • connect to database: \c [name]



  • Create a table

    • CREATE TABLE [table_name] (id SERIAL);

    • describe table: \d [table_name]





This couldn't be easier to get up and running. The server is not yet accepting remote connections. I will be working on that next and posting instructions.

Project Infrastructure

So, I am embarking on a new learning experience. I am going to set up a project infrastructure on a virtual private server. This will consist of:

- MySQL database
- Subversion
- Trac
- Artifactory
- Hudson

I plan on keeping this updated more regularly as I work with these technologies.

Monday, November 17, 2008

Terracotta

So I'll start my first meaty post with some thoughts about Terracotta. Note I am no expert, and haven't used this in a real project -- just some proof-of-concept type work for my side project. Take what I have to say with a grain of salt. That said, I think I can safely say the following:


  • Terracotta is pretty slick.

  • It is easy to learn/work with.

  • It could be leveraged to provide clustering fairly easily.

  • It has great tools & integrations

  • You still have to think to write effective distributed code.

  • There are some gaps you have to fill.



Terracotta is a relatively easy way to cluster java applications. It uses a server (or server array) to manage client JVM instances as a single, clustered java virtual machine. The implications of that are fairly large. It means that memory is shared across the cluster, including the objects in that memory, and that failure of one VM doesn't shut down the application. A list of features is


  • Clustered VM

  • Shared memory

  • Fine-grained shared-object state propagation

  • Shared-object locking & transactions using thread locking semantics

  • Virtual heap/Garbage Collector - Transparent data management from local in-memory to remote in-memory to disk

  • Fail-over safety via multi-nodes, disk persistence



It should be noted that most of the above is handled in a very transparent manner. It is either handled for you, or is easily configurable. For instance, locking & transactions are handled through bytecode injection into objects you declare to be shared. Here is an example:


synchronized (someObject) {
  someObject.data = "test";
}


In uninstrumented java, the above code obtains a lock on someObject, changes its data member variable to the value "test", and then releases its lock. If this is a managed object in Terracotta, the above code obtains a cluster-wide lock on someObject, changes its data member variable to the value "test" in the local VM only, then releases the cluster-wide lock. At this point, the change to the data member variable is published to the cluster. All other nodes with a reference to someObject will have its state changed automatically. Also of note is that this is a fine-grained operation. Only the value of the data field is propagated across the cluster, not the full serialization of someObject.

So whats the catch? Well, there are some clustering almost-necessities that apparently are not provided. At least, I haven't seen them. If anyone reads this and knows different, please correct me. I would include in this list:


  • Operational splitting (map/reduce)

  • Load balancing

  • Message routing

  • Sub-clustering



So I would say Terracotta is very strong in clustered data management, but short on operational clustering. Interestingly, I would reverse that statement when describing GridGain, another java clustering technology alternative. I'm actually looking at how feasible it is to integrate both to leverage their strengths together. I'll post more about GridGain and this experiment later.

Check out Terracotta for yourself: http://www.terracotta.org/

Check out GridGain for yourself: http://www.gridgain.com/

Sunday, November 16, 2008

Genesis

How's that for a self-important title? I've decided its time I start keeping a professional blog, for myself primarily. This will likely be a cluttered desk of assorted jottings and notes that I want to remember but won't unless they are written down. And I trust blogger's data safety more than the janitors that clean around my desk at work.

So by now you (I?) might be wondering what it is I do for a living. I'm a software engineer currently working for Software Technology Group (an outsourced project consulting firm) in Salt Lake City, UT. I work primarily in java, and have done so for the past 11 years give or take. 11 years is a long time to remember things. Hence this blog.

Currently I am working on a project for the LDS church utilizing Spring, JSF, Facelets, Seam, Hibernate/JPA, EHCache, jQuery and probably a few other things I can't remember. Hence this blog.

I am also starting a LLC to develop a software product that must be kept super-hush-hush lest it fall into the wrong hands (like someone who could actually bring it to fruition). To this end, I'm doing some proof-of-concept type work with a few technologies that are new to me -- Terracotta and GridGain. Both of these are pretty exciting, and cover some ground I've not trod, so there is a lot there to learn and retain. Hence this blog.

I suppose that is good enough for a first post. More to come later.