Google Ads
Wednesday, March 10, 2010
STOCK MARKET EXCHANGE INFORMATION USING CORBA
StockMarket.idl
module SimpleStocks
{
interface StockMarket
{
float get_price(in string symbol);
};
};
StockMarketImpl.java
import org.omg.CORBA.*;
import SimpleStocks.*;
public class StockMarketImpl extends StockMarketPOA
{
private ORB orb;
public StockMarketImpl(ORB orb)
{
this.orb=orb; }
public float get_price(String symbol)
{
float price = 0;
for(int i=0;i < symbol.length();i++)
{
price += (int)symbol.charAt(i);
}
price /= 5;
return price;
}
}
StockMarketServer.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import SimpleStocks.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import org.omg.CosNaming.NamingContextPackage.*;
public class StockMarketServer
{
public static void main(String[] args)
{
try
{
ORB orb = ORB.init(args,null);
StockMarketImpl stockMarketImpl = new StockMarketImpl(orb);
POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
org.omg.CORBA.Object ref=rootpoa.servant_to_reference(stockMarketImpl);
StockMarket href=StockMarketHelper.narrow(ref);
org.omg.CORBA.Object object = orb.resolve_initial_references("NameService");
NamingContextExt root = NamingContextExtHelper.narrow(object);
NameComponent name = new NameComponent("NASDAQ","");
NameComponent path[] = {name};
root.rebind(path,href);
System.out.println("Server is Ready.............");
orb.run();
}
catch(Exception e)
{
System.err.println("ERROR"+e);
}
}}
StockMarketClient.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import SimpleStocks.*;
import org.omg.CosNaming.NamingContextPackage.*;
public class StockMarketClient{
public static void main(String[] args)
{
try
{
ORB orb = ORB.init(args,null);
org.omg.CORBA.Object object=orb.resolve_initial_references("NameService");
NamingContextExt root = NamingContextExtHelper.narrow(object);
NameComponent name = new NameComponent("NASDAQ","");
NameComponent path[] = {name};
StockMarket market = StockMarketHelper.narrow(root.resolve(path));
System.out.println("Price of MY COMPANY is " + market.get_price("MY_COMPANY"));
}
catch(Exception e )
{
System.err.println( e ); } }}
Output:
C:\jdk1.5.0\bin>javac StockMarketServer.java
C:\jdk1.5.0\bin>javac StockMarketClient.java
C:\jdk1.5.0\bin>java StockMarketServer ORBInitialPort-2500
Server is Ready.............
C:\jdk1.5.0\bin>java StockMarketClient ORBInitialPort-2500
Price of MY COMPANY is 159.2
WEATHER FORECAST INFORMATION USING CORBA
module Weather1
{
interface Weather
{
double FahToCel(in double degree);
};
};
WeatherImpl.java
import Weather1.*;
import org.omg.CORBA.*;
public class WeatherImpl extends WeatherPOA
{
private ORB orb;
public WeatherImpl(ORB orb)
{
this.orb = orb;
}
public double FahToCel(double F)
{
double c;
c = ((F-32)*5)/9;
return c;
}
}
WeatherServer.java
import Weather1.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
public class WeatherServer
{
public static void main(String args[])
{
try
{
ORB orb = ORB.init(args,null);
WeatherImpl impl = new WeatherImpl(orb);
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(impl);
Weather wref = WeatherHelper.narrow(ref);
org.omg.CORBA.Object objref = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objref);
NameComponent nc = new NameComponent("Weather","");
NameComponent path[] = {nc};
ncRef.rebind(path,wref);
System.out.println("Server ready & waiting.....");
orb.run();
}
catch(Exception e)
{
System.err.println("ERROR"+e);
}
System.out.println("Server Exiting............");
}
}
WeatherClient.java
import Weather1.*;
import java.io.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
public class WeatherClient
{
public static void main(String args[])
{
double F;
BufferedReader stdin;
try
{
ORB orb = ORB.init(args,null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
NameComponent nc = new NameComponent("Weather","");
NameComponent path[] = {nc};
Weather impl = WeatherHelper.narrow(ncRef.resolve(path));
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the temperature in Fah");
F = Double.parseDouble(stdin.readLine());
double cel = impl.FahToCel(F);
System.out.println("Temperature in Celcius:"+String.valueOf(cel));
}
catch(Exception e)
{
System.out.println("ERROR:"+e);
}
}
}
Output
C:\jdk1.5\bin>idlj -fall Weather.idl
C:\jdk1.5\bin>javac WeatherImpl.java
C:\jdk1.5\bin>javac WeatherServer.java
C:\jdk1.5\bin>javac WeatherClient.java
C:\jdk1.5\bin>orbd ORBInitialPort-2500
C:\jdk1.5\bin>java WeatherServer ORBInitialPort-2500
Server ready & waiting.....
C:\>cd jdk1.5\bin
C:\jdk1.5\bin>java WeatherClient ORBInitialPort-2500
Enter the temperature in Fah
45
Temperature in Celcius:7.222222222222222
Monday, January 4, 2010
PIPES
PIPES
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ errno.h “
#include “ fcntl.h “
#include “ stdio.h “
#include “ sys/stat.h “
#include “ unistd.h “
#include “ string.h “
#define MAXLINE 50
void client(int,int);
void server(int,int);
int main(void)
{
int pipe1[2],pipe2[2];
pid_t childpid;
pipe(pipe1);
pipe(pipe2);
if((childpid=fork())==0)
{
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0],pipe2[1]);
exit(0);
}
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0],pipe1[1]);
waitpid(childpid,NULL,0);
exit(0);
}
void client(int readfd,int writefd)
{
size_t len;
size_t n;
char buf[MAXLINE];
printf("\nEnter Filename:");
fgets(buf,MAXLINE,stdin);
len=strlen(buf);
if(buf[len-1]=='\n')
len--;
write(writefd,buf,len);
while((n=read(readfd,buf,MAXLINE)) “ 0)
write(STDOUT_FILENO,buf,n);
}
void server(int readfd,int writefd)
{
int fd;
size_t n;
char buf[MAXLINE+1];
if((n=read(readfd,buf,MAXLINE))==0)
buf[n]='\0';
if((fd=open(buf,O_RDONLY)) “ 0)
{
snprintf(buf+n,sizeof(buf)-n,"Can't open %s :",strerror(errno));
n=strlen(buf);
write(writefd,buf,n);
}
else
{
while((n=read(fd,buf,MAXLINE)) “ 0)
write(writefd,buf,n);
printf("%s",buf);
close(fd);
}
}
OUTPUT
[user1@localhost user1]$ cc pipe.c
[user1@localhost user1]$ ./a.out
Enter Filename:pipes.c
#include “ stdio.h “
main()
{
printf("\nWELCOME\n");
}
[user1@localhost user1]$
Sunday, January 3, 2010
MESSAGE QUEUES
INTERPROCESS COMMUNICATION USING
MESSAGE QUEUES
#include “ stdlib.h “
#include “ sys/types.h ”
#include “ sys/ipc.h ”
#include “ stdio.h “
#include " unistd.h “
#define msgtxtlen 100
#define sizeofmsg sizeof(msg)
#define msglen strlen(msg.mtext)
struct msgbuf
{
long mtype;
char mtext[msgtxtlen];
}msg;
main()
{
int ch;
int msgqid,cont=1,ren;
printf("\n\tMessage Queue Manipulation");
while(cont)
{
printf("\n\t1.Create message queue");
printf("\n\t2.Add a message into the queue");
printf("\n\t3.Retrieve a message from the queue");
printf("\n\t4.Remove a message queue");
printf("\n\t5.Exit");
printf("\n\tEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
msgqid=msgget(0x25,IPC_CREAT0644);
printf("\n\tMessage queue is created");
break;
case 2:
printf("\n\tMessage sending:");
printf("\n\tEnter the message type:");
scanf("%d",&msg.mtype);
printf("\n\tEnter the message:");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeof(msg),0);
if(ren!=1)
printf("\n\tMessage posted successfully");
else
printf("\n\tSorry u made mistake");
break;
case 3:
printf("\n\tMessage receiving and removing from the queue");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeof(msg),msg.mtype,0);
printf("\n\tMessage type :%d",msg.mtype);
printf("\n\tMessage length :%d",msglen);
printf("\n\tMessage content :%s",msg.mtext);
break;
case 4:
ren=msgctl(msgqid,IPC_RMID,NULL);
if(ren!=1)
printf("\n\tMessage queue was removed");
else
printf("\n\tSorry");
break;
case 5:
exit(0);
default:
printf("\n\tEnter the value choice\n");
break;
}
printf("\n\tDo u wish to continue. yes=1,no=0 :");
scanf("%d",&cont);
}
}
OUTPUT
[user1@localhost user1]$ cc msgq.c
[user1@localhost user1]$ ./a.out
Message Queue Manipulation
1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit
Enter the choice:1
Message queue is created
Do u wish to continue. yes=1,no=0 :1
1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit
Enter the choice:2
Message sending:
Enter the message type:0
Enter the message:hello
Message posted successfully
Do u wish to continue. yes=1,no=0 :1
1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit
Enter the choice:3
Message receiving and removing from the queue
Enter the message type0
Message type :0
Message length :5
Message content :hello
Do u wish to continue. yes=1,no=0 :1
1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit
Enter the choice:4
Message queue was removed
Do u wish to continue. yes=1,no=0 :1
1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit
Enter the choice:5
[user1@localhost user1]$