Help with CORBA Client...
I am trying to create a server and client where the client can contact the server to create an account and deposit and withdraw into the account. The client should be able to open more than one account.
I have no idea how to code the the Client bit.
Code Java:
// The package containing our stubs.
import AccountApp.*;
// HelloServer will use the naming service.
import org.omg.CosNaming.*;
// The package containing special exceptions thrown by the name service.
import org.omg.CosNaming.NamingContextPackage.*;
// All CORBA applications need these classes.
import org.omg.CORBA.*;
import java.util.List;
import java.util.LinkedList;
public class Server
{
public static void main(String args[])
{
try{
// Create and initialize the ORB
ORB orb = ORB.init(args, null);
// Create the servant and register it with the ORB
AccountImpl accRef = new AccountImpl();
orb.connect(accRef);
// Get the root naming context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Bind the object reference in naming
// Make sure there are no spaces between ""
NameComponent nc = new NameComponent("Account", "");
NameComponent path[] = {nc};
ncRef.rebind(path, accRef);
// Wait for invocations from clients
java.lang.Object sync = new java.lang.Object();
synchronized(sync){
sync.wait();
}
} catch(Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}
}
class AccountImpl extends _AccountImplBase{
private String accountNo;
private String ownersName;
private String pin;
private float balance;
//private List<Account> accounts;
//private int number_of_accounts;
public AccountImpl(){
accountNo = "0987";
pin = "1234";
ownersName = "John";
balance = 0;
//accounts = new LinkedList<Account>();
//number_of_accounts = 0;
}
public float getBalance(String PIN)
throws WrongPIN{
pin = PIN;
if (!PIN.equals(pin))
throw new WrongPIN();
return balance;
}
public float deposit(String PIN, float amount)
throws WrongPIN {
pin = PIN;
if (!PIN.equals(pin))
throw new WrongPIN();
balance += amount;
return balance;
}
public float withdraw(String PIN, float amount)
throws WrongPIN, InsufficientFunds {
pin = PIN;
if (!PIN.equals(pin))
throw new WrongPIN();
if (amount > balance)
throw new InsufficientFunds();
balance -= amount;
return balance;
}
public void create(String name, float balance, String PIN){
//++number_of_accounts;
// Account res = new AccountImpl( Integer.toString(number_of_accounts)
// , PIN
// , name
// , balance);
// accounts.add( res );
}
}
Code Java:
class Client
{
public static void main(String args[])
{
try{
// Create and initialize the ORB
ORB orb = ORB.init(args, null);
// Get the root naming context
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// Resolve the object reference in naming
// make sure there are no spaces between ""
NameComponent nc = new NameComponent("Account", "");
NameComponent path[] = {nc};
Account accRef = AccountHelper.narrow(ncRef.resolve(path));
} catch(Exception e) {
System.out.println("ERROR : " + e);
e.printStackTrace(System.out);
}
}
}
Re: Help with CORBA Client...
What are your issues exactly? You need to give us a detailed description of the problems you are facing.
How far have you got? Does any of it work etc..
Seeing as you are using CORBA imports, you are very limited to the people who can actually compile this.
Re: Help with CORBA Client...
The problem I am having is in the Client class, I am unsure how to implement the methods that the Client can invoke, for instance deposit, withdraw and getBalance.
Re: Help with CORBA Client...
Basically what I want is methods to call Server objects such as deposit, withdraw, and getBalance from the Client class, I just do not know how to write them.