1) Compute.java
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
<T> T executeTask(Task<T> t) throws RemoteException;
}
2) Task.java
package compute;
public interface Task<T> {
T execute();
}
cd c:\home\waldo\src
javac compute\Compute.java compute\Task.java
jar cvf compute.jar compute\*.class
3) ComputeEngine.java
package engine;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;
public class ComputeEngine implements Compute{
public ComputeEngine(){
super();
}
public<T> T executeTask(Task<T> t){
return t.execute();
}
public static void main(String[] args){
if(System.getSecurityManager() == null){
System.setSecurityManager(new SecurityManager());
}
try{
String name = "Compute";
Compute engine = new ComputeEngine();
Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(name, stub);
} catch(Exception e){
System.err.println("ComputeEngine exception: ");
e.printStackTrace();
}
}
}
cd c:\home\ann\src
javac -cp c:\home\ann\public_html\classes\compute.jar
engine\ComputeEngine.java
Pi.java
package client;
import compute.Task;
import java.io.Serializable;
import java.math.BigDecimal;
public class Pi implements Task<BigDecimal>, Serializable{
private final int digits;
public Pi(int digits){
this.digits = digits;
}
public BigDecimal execute(){
return computePi(digits);
}
public static BigDecimal computePi(int digits){
return result;
}
}
ComputPi.java
package client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.math.BigDecimal;
import compute.Compute;
public class ComputePi{
public static void main(String args[]){
if(System.getSecurityManager() == null){
System.setSecurityManager(new SecurityManager());
}
try{
String name = "Compute";
Registry registry = LocateRegistry.getRegistry(args[0]);
Compute comp = (Compute) registry.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]);
BigDecimal pi = comp.executeTask(task);
System.out.println(pi);
} catch(Exception e){
System.err.println("ComputerPi exception:");
e.printStackTrace();
}
}
}
cd c:\home\jones\src
javac -cp c:\home\jones\public_html\classes\compute.jar
client\ComputePi.java client\Pi.java
mkdir c:\home\jones\public_html\classes\client
cp client\Pi.class
c:\home\jones\public_html\classes\client
server.policy
grant codeBase "file:/home/ann/src/" {
permission java.security.AllPermission;
};
client.policy
grant codeBase "file:/home/jones/src/" {
permission java.security.AllPermission;
};
start rmiregistry (or javaw if start is not available)
java -cp c:\home\ann\src;c:\home\ann\public_html\classes\compute.jar
-Djava.rmi.server.codebase=file:/c:/home/ann/public_html/classes/compute.jar
-Djava.rmi.server.hostname=mycomputer.example.com
-Djava.security.policy=server.policy
engine.ComputeEngine
java -cp c:\home\jones\src;c:\home\jones\public_html\classes\compute.jar
-Djava.rmi.server.codebase=file:/c:/home/jones/public_html/classes/
-Djava.security.policy=client.policy
client.ComputePi mycomputer.example.com 45
Wednesday, November 21, 2012
Thursday, November 8, 2012
Objective-C Header and Class Implementation
Person.h
#import <Foundation/Foundation.h>
@interface Person:NSObject{
NSString *name;
int age;
}
- (NSString *)name;
- (void) setName: (NSString *) newName;
- (int)age;
- (void) setAge: (int) newAge;
//actions
- (BOOL) isAllowedToVote;
- (void castBallot;
@end
Person.m
#import "Person.h"
@implementation Person
- (int) age{
return age;
}
- (void) setAge: (int) value{
age = value;
}
//... and other methods
@end
Using a class
id anObject = [[SomeClass alloc] init];
- (id) init
{
self = [super init];
if(self){
//initializations
}
return self;
}
- (Person *) createPersonWithName: (NSString *) aName{
Person *aPerson = [[Person alloc] init];
[aPerson setName:aName];
[aPerson setAge:21];
return aPerson;
}
Class Inheritance
@interface Student : Person {
NSString *major;
}
@end
@implementation Student
// Constructor for a student
- (id) init
{
self = [super init];
if(self != nil){
//set attributes
[self setMajor:@"Undefined"];
}
return self;
}
@end
Objective-C Selector
id object;
SEL anAction = @selector(action:);
if([object respondsToSelector:anAction]){
[object performSelector:anAction withObject:self];
}
SEL anAction = @selector(action:);
if([object respondsToSelector:anAction]){
[object performSelector:anAction withObject:self];
}
Objective-C Object Messaging
[receiver message]
[receiver message:argument]
[receiver message:arg1 argument2:arg2]
int status = [receiver message];
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
[rectangle draw]
[rectangle setColor:blue]
[rectangle setStrokeColor:blue andThickness:2.0]
double length = [rectangle circumference];
[NSArray arrayWithObjects:one, two, three, nil];
[receiver message:argument]
[receiver message:arg1 argument2:arg2]
int status = [receiver message];
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
[rectangle draw]
[rectangle setColor:blue]
[rectangle setStrokeColor:blue andThickness:2.0]
double length = [rectangle circumference];
[NSArray arrayWithObjects:one, two, three, nil];
Java vs. Objective-C
Java
Rectangle rect = Rectangle(someWidth, someHeight);
println(rect.getHeight());
boolean inside = rect.contains(x,y);
Objective-C
Rectangle *rect = [[Rectangle alloc] initWithWidth:someWidth andHeight:someHeight];
NSLog(@"%f", [rect height]);
BOOL inside = [rect containsPointWithX:x andY:y]
Rectangle rect = Rectangle(someWidth, someHeight);
println(rect.getHeight());
boolean inside = rect.contains(x,y);
Objective-C
Rectangle *rect = [[Rectangle alloc] initWithWidth:someWidth andHeight:someHeight];
NSLog(@"%f", [rect height]);
BOOL inside = [rect containsPointWithX:x andY:y]
Objective-C Identiy vs. Equality
// test for object identity:
BOOL identical = @"Peter" == @"Peter"; //YES
identical = @"Peter" == @" Peter "; //NO
// test for string equality: will return YES
NSString *aName = @"Peter";
NSMutableString *anotherName = [NSMutableString stringWithString:@"P"];
[anotherName appendString:@"eter"];
BOOL same = [aName isEqualToString:anotherName]; //YES
BOOL equal = (aName == anotherName); //NO
// test for existence
BOOL exists = (object != nil);
BOOL identical = @"Peter" == @"Peter"; //YES
identical = @"Peter" == @" Peter "; //NO
// test for string equality: will return YES
NSString *aName = @"Peter";
NSMutableString *anotherName = [NSMutableString stringWithString:@"P"];
[anotherName appendString:@"eter"];
BOOL same = [aName isEqualToString:anotherName]; //YES
BOOL equal = (aName == anotherName); //NO
// test for existence
BOOL exists = (object != nil);
Subscribe to:
Posts (Atom)