Wednesday, June 23, 2010

Customized Diskless Computer Environment

Synopsis

Over the years, the evolution of computers has evolved with diskless computers too. Diskless computer does not contain any hard disk. Such a computer is dependent upon another computer (may be a server) for booting. Thus it gets booted through The Preboot execution Environment (PXE, pronounced pixie and also known as Pre-Execution Environment) which is an environment to Boot Computer using a network interface. Diskless booting uses a remote system or systems to store the kernel and the file system that will be used on other computer(s).
Now a day a diskless environment is present for only single server and multiple clients, wherein the clients get booted with one server either Linux or Windows and get access to that particular OS. In this project, that is, “Customized Diskless Computer Environment”, the system has been setup for more than one operating system which may be available in the network, for e.g. Linux & Windows respectively. The uniqueness of this system is to implement a diskless client will get started before getting booted to one operating system and will give an option to the user which operating system he/she wants to use, and will proceed to boot the desired one.

Problem Definition

The main problem faced by most of the system administrators is setting up the lab with required operating systems and other necessary softwares. The current research project is to determine proper solutions in handling the above scenario by minimizing the time, cost and the labour. Ones such solution is to have a diskless computer environment which will enable the administrators to set the system by installing the operating system and softwares in respective servers alone.
The project tries to establish such an environment in which we have to build only two Computer systems, one PC with Windows operating system and other one with Linux operating system or any other operating system that you want. After installation of OS in the two servers, install required softwares and other applications needed by client, and connect to the environment and it is available with full featured Operating system. Thus we are able to create two computers with desired OS, instead of going to each client and installing OS and other applications. Any client connected to the server can now access the OS.

Objective & Scopes
The Aim behind developing a customized diskless computer environment is to save the time of system administrator as well as to bring down the cost incurred in client computers.
In a situation where a system administrator is responsible for setting up and managing nearly 30 to 40 computers in a lab or work environment, it is very time consuming to configure and install each client computer with necessary software’s and network configurations. This is where the role of customized diskless computer environment comes into picture. Here, the administrator just has to prepare two computer machines with desired operating system and all required applications. In this Environment any updates or changes have to be made or can be made on server machine and it will reflect the same updates and changes to all other clients or computers in this Network.
The main objective of the customized diskless environment is very useful in organization or any computer institute or departmental computer lab to make them hardware cost effective.
Through customized diskless computer environment the hardware cost gets reduced. Suppose any organization or institute have some old computers with LAN or PXE booting, then these systems can also be connected to the same network to function as clients and this saves the purchase of new computers.
Due to high flair for computer networking and related environments, we thought of doing a research in existing network settings of the systems and come with a more optimized solution. For this, we had done a survey of some organizations, Institutions (may be a private computer centre or a college computer lab) where we found that there are a number of PCs which are used tremendously for various purposes and application needs are also different. Every system needs a basic Operating system and requires other necessary applications on each one of them. The process of updating the system by the administrator took a long time and that made the users of the system waiting for the job to be done. This motivated us to think of a diskless computing environment.
Our Environment can handle large quantity of clients and requires a good connection with the connectivity say 100 tbase Ethernet. As number of clients increase the booting process will surely get affected. It is better to have high capacity Ethernet hub. Our environment provides two operating systems, and gives an option to choose between two operating systems. In future enhancements we are trying for more operating system i.e., to give more than two operating systems for e.g. Linux, Windows, Mac, and any other version of windows and linux.

One of the major scopes of my customized environment is instead expending several minutes on each client installing Operating system and other necessary software & application we just have to build two computers holding our desired operating system which we offer to client to use. And one more advantage of this is that consider a person coming with diskless CPU and get connected to our environment will get access to more than one operating system.
We just have to remember that we have to keep the server storage enough to hold the load of the network and good speed network connection because same time multiple clients will have access the same operating system

Requirements

· A windows server 2003 system
· An Ubuntu system with (preferably) DHCP and tftpd server.
· At least one PXE-bootable system (the client)
· Enough disk space on the server to hold the client file system
· A fast network connection between the client and the server
· A DHCP server which is capable of supporting PXE clients, or a separate network segment where you can run a dedicated DHCP server
· A good understanding of Linux


Working

Diskless booting requires a DHCP server which is a bootable PXE network card. The client will query to get its configuration and location of the bootable file from tftp server. After booting the PXE image the client will boot the kernel image (with arguments specified in the pxe configuration). Those arguments will tell the kernel how to configure itself, and the path to mount the NFS share where its directory is located. I our environment we are using both LTSP & MTSP for Linux & Windows respectively. As client get Booted it gets an option to choose the desired operating system.


Getting Started
Windows Server 2003 Installation

Start by installing Windows Server 2003 to a physical disk

You can now press Enter to install Windows Server 2003 to this partition. The rest of the installation will proceed as normal.

Windows Server 2003 Startup

Now Windows Server 2003 is setup to use

Ubuntu Server Installation

Ubuntu 9.10, also known as the Karmic Koala, arrived exactly on October 29, 2009 and is the eleventh release of Ubuntu OS. We've created the following tutorial to teach Linux newcomers how to install the
Ubuntu 9.10 operating system on their personal

The tutorial will make things very simple for you, but if you get stuck somewhere in the middle of the installation and you need help, do not hesitate to use our commenting system at the end of the article!
Requirements:

You will need the Ubuntu 9.10 Desktop ISO image that corresponds to your hardware architecture (i386 or amd64). When the download is over, burn the ISO image with your favorite CD/DVD burning application (Nero, CDBurnerXP, Roxio) on a blank CD at 8x speed.

Now u can use Ubuntu 9.10

After Ubuntu installation we have to update through the command (Internet Connection required)
#sudo su
#apt-get update

Now Installing LTSP on top of an already running desktop system through command

#sudo apt-get install ltsp-server-standalone openssh-server

Now create your Thin Client environment on the server with.

sudo ltsp-build-client

this will create a thin client environment on your linux server

Comment: - If you change the IP data after you have done the initial setup, please run the command sudo ltsp-update-sshkeys to make the ssh server aware of the change. This workstation isn't authorized to connect to server error message on client, please run commands sudo ltsp-update-sshkeys and sudo ltsp-update-image.

Preboot eXecution Environment (PXE)

PXE is an open industry standard developed by a number of software and hardware vendors. It was initially designed by Intel, with input from several other vendors including 3Com, HP, Dell, Compaq, and Phoenix Technologies. PXE works with a network interface card (NIC) in the PC, and makes the NIC a boot device. The PXE vision is to "Make the network interface a standard, industry-accepted PC boot device." This means adding the NIC to the traditional list of standard boot devices, such as floppy drives, hard disks, and CD-ROMs, that load the operating system or set up programs on the PC. It allows the client PC to "network boot." Booting from the network opens up a vast array of management and support features.
PXE boots the client PC from the network by transferring a "boot image file" from a server. This file can be the operating system for the client PC or a pre-OS agent (see the section, "Pre-OS," later in this paper) that performs client management tasks. Since PXE is not operating system-specific, the image file can load any OS. It provides support for network booting , of embedded and other operating systems.
Because PXE works with the NIC, it requires a PXE-enabled NIC. Most currently available NICs do support PXE, including those from 3Com, Intel, Digital, RealTek, and SMC. PXE is available either as a boot ROM chip that you add to the NIC, or as part of the system BIOS if the network interface is on the motherboard. PXE is specific to a type of NIC; a boot ROM for one type (for example, a 3C905C) will not work on another type of NIC.



DHCP Configuration
Configure your DHCP server

You need to set up the DHCP server to offer /var/lib/tftpboot/pxelinux.0 as a boot file as a minimum. You also assign a fixed IP to the machine you want to boot with PXE (the client). A range of other options are also available but are beyond the scope of this article. Your dhcpd.conf might look like this assuming your subnet is 192.168.2.0
# gedit /etc/ltsp/dhcpd.conf
After configuration dhcpd.conf file will look something like this.
allow booting;
allow bootp;

subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.xxx 192.168.2.xxx;
option broadcast-address 192.168.2.255;
option routers 192.168.2.xxx;
option domain-name-servers 192.168.2.xxx;

filename "/pxelinux.0";
}

# force the client to this ip for pxe.
# This is only necessary assuming you want to send different images to different computers.
host pxe_client
{
hardware ethernet xx:xx:xx:xx:xx:xx;
fixed-address 192.168.2.xxx;
}

NOTE 1: You will need to replace the 'xx:xx:xx:xx:xx:xx' and the '192.168.2.xxx' with your own values
NOTE 2: the filename is a relative path to the root of the tftp server.


Pxelinux.cfg/default file Configuration

This is very important file now we have to configure this file so that when PXE booting start its get its instruction through this file, our tftp boot folder is in the directory /var/lib/tftpboot/ltsp/i386/pxelinux.cfg/
Now we have to create default file in this directory
# gedit /var/lib/tftpboot/ltsp/i386/pxelinux.cfg/default
In my environment it look something like this
default vesamenu.c32
Menu Background howtoforge_pxe.png
Menu Title Boot Menu

label Linux
menu label ^Linux
menu default
kernel vmlinuz
append vmlinuz ro initrd=initrd.img – quiet splash

label Windows
menu label ^Windows
kernel vmlinuz2
append ramdisk_size=17232 ro initrd=initrd2.img root=/dev/ram console=null SESSION_DEFAULT=rdp


Now we have put some file required for PXE bootin,some files I have in my /home/mhs directory.
# cp /home/mhs/isolinux/vmlinuz2 /var/lib/tftpboot/ltsp/i386/
# cp /home/mhs/isolinux/initrd2.img /var/lib/tftpboot/ltsp/i386/
# cp /home/mhs/isolinux/vesamenu.c32 /var/lib/tftpboot/ltsp/i386/
# cp /home/mhs/isolinux/MP.png /var/lib/tftpboot/ltsp/i386/
Some files is already present as we install LTSP for terminal services


Tftpboot directory tree will look this
/var
/lib
/tffpboot
/ltsp
/i386/pxelinux.cfg ……………. (it is directory not file)
/default…………. ( This is main config file)
/pxelinux.0
/vmlinuz ……………………. (LTSP kernel)
/initrd.img ………………… (LTSP RAM image file)
/vmlinuz2 …………………… (Our custom kernel)
/initrd2.img ………………… (Our custom RAM image file)
/vesamenu.c32
/MP.png


1.We have to start linux server & put up the static IP Address
In my case I have put
IP = 192.168.2.2
Net mask = 255.255.255.0
DNS Server = 192.168.2.1
After putting this value start the DHCP server
/etc/init.d/dhcp3-server start
/etc/init.d/tftp-hpa start

Booting the Clients
This is our option before getting booted to certain os now we have to choose our desire operating system you want.

Video of this project i will upload it soon.
If u need any extra information on this project feel free to email me.

Monday, June 14, 2010

b. Implementation of queue

-------------------------------------------------------------------------------------------------------------
SOURCE CODE
----------------------------------------------------------------------------------------------


Server Program :

import java.io.*;
import java.net.*;

//class implementing Queue operations
class Queue{

private int queue[];
private int toq;
private int eoq;

// allocate and initialize queue
Queue(int size){
queue = new int[size];
toq = 0;
eoq = -1;
}
//check whether queue is full
boolean isFull(){
if(eoq == queue.length-1)
return true;
else
return false;
}
//check whether queue is empty
boolean isEmpty(){
if(eoq < 0)
return true;
else
return false;
}

// Push an item into queue
void push(int item){
for(int i=eoq;i>-1;i--){
queue[i+1]=queue[i];
}
++eoq;
queue[0] = item;
}

// Pop an item from the queue
int pop(){
return queue[eoq--];
}
//get all elements of queue
int[] getQueue(){
return queue;
}
//get current size of queue
int getCurrentSize(){
return (eoq+1);
}
}

//class implementing Queue Server operations
class QueueServer implements Runnable{

static ServerSocket serverSocket;
Socket socket;
BufferedReader read,from;
PrintWriter to;
Thread thread;
Queue queue;
int clientID;
static int clientCounter=0;

//constructor
QueueServer(Socket socket)throws Exception{

this.socket=socket;
clientID=++clientCounter;
System.out.println("New Client connection accepted. ClientID: "+clientID);
from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
to=new PrintWriter(socket.getOutputStream(),true);
thread=new Thread(this);
thread.start();
}

//run() method for each client request thread
public void run(){

try{
to.println("Enter size of the queue: ");
int size=Integer.parseInt((from.readLine()).trim());
queue=new Queue(size);
int item=0;
String action=" ";
to.println("You can \"push\" \"pop\" \"display\" elements or \"exit\" What next??");

do{

action=(from.readLine()).toLowerCase();

if(action.equals("push")){
if(queue.isFull())
to.println("Queue Full. What next??");
else{
to.println("Enter element: ");
item=Integer.parseInt((from.readLine()).trim());
queue.push(item);
to.println("Pushed item. What next??");
}
}
else if(action.equals("pop")){
if(queue.isEmpty())
to.println("Stack Empty. What next??");
else
to.println("Popped item: "+queue.pop()+" What next??");
}
else if(action.equals("display")){
if(queue.isEmpty())
to.println("Queue Empty. What next??");
else{
int[] q=queue.getQueue();
String queueStr="";
for(int i=0;i queueStr=queueStr+" "+q[i];
to.println("Queue contains: "+queueStr+" What next??");
}
}
else if(action.equals("exit")){
socket.close();
break;
}
else
to.println("Invalid option. What next??");

}while(!(action=="exit"));

System.out.println("Client "+clientID+" exited.");

}catch(Exception e){System.out.println(e);}
}

//main() method
public static void main(String args[])throws Exception{

serverSocket=new ServerSocket(2222);

System.out.println("Server Started. Press Ctrl+C to close server.");
while(true){
Socket s=serverSocket.accept();
QueueServer server=new QueueServer(s);
}
}
}


Client Program :

import java.io.*;
import java.net.*;

//class implementing client operations
class QueueClient{

//main() method
public static void main(String args[]){

try{
String in="";
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter server address: ");
Socket socket=new Socket(read.readLine(),2222);
BufferedReader from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter to=new PrintWriter(socket.getOutputStream(),true);

//read write loop of client
while(true){
System.out.println(from.readLine());
in=read.readLine();
to.println(in);
if(in.equalsIgnoreCase("exit")){
socket.close();
break;
}
}
System.out.println("Client closing.");
}catch(Exception e){e.printStackTrace(System.out);}
}
}

Simulating Network Operating System (NOS) commands

Simulating Network Operating System (NOS) commands
Concept:
In this practical make use of socket programming. On the client side design an application that will give the list of commands. The client will invoke these commands. On the server side these commands will be executed. The implementation of the above commands will be on the server side.
a. Implementation of stack
Commands: PUSH, POP, DISPLAY, EXIT, etc.
b. Implementation of queue.
Commands: INSERT, DELETE, DISPLAY, EXIT, etc.

a. Implementation of Stack

-----------------------------------------------------------------------------------------------------------
SOURCE CODE
-----------------------------------------------------------------------------------------------------------


Server Program :

import java.io.*;
import java.net.*;

//class implementing Stack operations
class Stack{

private int stck[];
private int tos;

// allocate and initialize stack
Stack(int size){
stck = new int[size];
tos = -1;
}
//check whether stack is full
boolean isFull(){
if(tos == stck.length-1)
return true;
else
return false;
}
//check whether stack is empty
boolean isEmpty(){
if(tos < 0)
return true;
else
return false;
}

// Push an item onto the stack
void push(int item){
if(! (tos==stck.length-1)) // use length member
stck[++tos] = item;
}

// Pop an item from the stack
int pop(){
return stck[tos--];
}
//get entire stack
int[] getStack(){
return stck;
}
//get current size of the stack
int getCurrentSize(){
return (tos+1);
}
}

//class implementing Stack Server operations
class StackServer implements Runnable{

static ServerSocket serverSocket;
Socket socket;
BufferedReader read,from;
PrintWriter to;
Thread thread;
Stack stack;
int clientID;
static int clientCounter=0;

//constructor
StackServer(Socket socket)throws Exception{

this.socket=socket;
clientID=++clientCounter;
System.out.println("New Client connection accepted. ClientID: "+clientID);
from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
to=new PrintWriter(socket.getOutputStream(),true);
thread=new Thread(this);
thread.start();
}

//run() method for each client request thread
public void run(){

try{
to.println("Enter size of the stack: ");
int size=Integer.parseInt((from.readLine()).trim());
stack=new Stack(size);
int item=0;
String action=" ";
to.println("You can \"push\" \"pop\" \"display\" elements or \"exit\" What next??");

do{

action=(from.readLine()).toLowerCase();

if(action.equals("push")){
if(stack.isFull())
to.println("Stack Full. What next??");
else{
to.println("Enter element: ");
item=Integer.parseInt((from.readLine()).trim());
stack.push(item);
to.println("Pushed item. What next??");
}
}
else if(action.equals("pop")){
if(stack.isEmpty())
to.println("Stack Empty. What next??");
else
to.println("Popped item: "+stack.pop()+" What next??");
}
else if(action.equals("display")){
if(stack.isEmpty())
to.println("Stack Empty. What next??");
else{
int[] stck=stack.getStack();
String stckStr="";
for(int i=0;i stckStr=stckStr+" "+stck[i];
to.println("Stack contains: "+stckStr+" What next??");
}
}
else if(action.equals("exit")){
socket.close();
break;
}
else
to.println("Invalid option. What next??");

}while(!(action=="exit"));

System.out.println("Client "+clientID+" exited.");

}catch(Exception e){System.out.println(e);}
}

//main() method
public static void main(String args[])throws Exception{

serverSocket=new ServerSocket(2222);

System.out.println("Server Started. Press Ctrl+C to close server.");
while(true){
Socket s=serverSocket.accept();
StackServer server=new StackServer(s);
}
}
}

Client Program :

import java.io.*;
import java.net.*;

//class implementing Stack Client operations
class StackClient{

//main() method
public static void main(String args[]){

try{
String in="";
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter server address: ");
Socket socket=new Socket(read.readLine(),2222);
BufferedReader from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter to=new PrintWriter(socket.getOutputStream(),true);

//read write loop
while(true){

System.out.println(from.readLine());
in=read.readLine();
to.println(in);
if(in.equalsIgnoreCase("exit")){
socket.close();
break;
}
}
System.out.println("Client closing.");
}catch(Exception e){e.printStackTrace(System.out);}
}
}

Saturday, January 16, 2010

Interface.java

import java.rmi.*; // Provides remote method invocation.
import java.sql.*; //Communicates with a database.

//creation of interface
public interface interfacestud extends Remote
{
void Connect() throws RemoteException;
void setData() throws RemoteException;

String[] getIDstud() throws RemoteException;
String[] getNamestud() throws RemoteException;
String[] getCitystud() throws RemoteException;

String[] getIDBook() throws RemoteException;
String[] getNameBook() throws RemoteException;
String[] getCostBook() throws RemoteException;
}


Implstud.java

import java.rmi.*; // Provides remote method invocation.
import java.rmi.server.*; // supports remote method invocation.
import java.sql.*; //Communicates with a database.
import java.io.*; //provides support for I/O operations

public class implstud extends UnicastRemoteObject implements interfacestud //implementation of interfacestud interface
{
//creation of resultset
ResultSet std=null;
ResultSet lib=null;

//variable declaration
int i=0;
String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];

String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];

Statement st,st1=null; //statement creation
public implstud() throws RemoteException
{
}

//connection establishment with database
public void Connect() throws RemoteException
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:stud");
st=con.createStatement();
st1=con.createStatement();

String sav1="Select * from stud";
String sav2="Select * from Library";

std=st.executeQuery(sav1);
lib=st1.executeQuery(sav2);

}catch(ClassNotFoundException e){}
catch(SQLException e){}

}
//method to insert data in database
public void setData() throws RemoteException
{
try
{

int i=0;
while(std.next())
{
Idstud[i]=std.getString(1);
Namestud[i]=std.getString(2);
Citystud[i]=std.getString(3);
i=i+1;
}
i=0;
while(lib.next())
{
Idbook[i]=lib.getString(1);
Namebook[i]=lib.getString(2);
Costbook[i]=lib.getString(3);
i=i+1;
}

}catch(Exception e){}

}

//methods to read data from database
public String[] getIDstud() throws RemoteException
{
return(Idstud);
}

public String[] getNamestud() throws RemoteException
{
return(Namestud);
}

public String[] getCitystud() throws RemoteException
{
return(Citystud);
}

public String[] getIDBook() throws RemoteException
{
return(Idbook);
}

public String[] getNameBook() throws RemoteException
{
return(Namebook);
}

public String[] getCostBook() throws RemoteException
{
return(Costbook);
}

}


Server.java

//server program with main method
import java.net.*;
import java.rmi.*; // Provides remote method invocation.
public class server
{
public static void main(String args[])
{
try
{
implstud dispImpl1 = new implstud();
Naming.rebind("Display1", dispImpl1);

implstud dispImpl2 = new implstud();
Naming.rebind("Display2", dispImpl2);
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
}

}


Client.java

import java.rmi.*;
import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



class dClient extends JFrame
{

JTabbedPane jtp=null;

dClient()
{
super("Database Details");


Container c = getContentPane();

jtp= new JTabbedPane();
jtp.addTab("Student Details", new StudentPanel());
jtp.addTab("Library", new LibraryPanel());
c.add(jtp);

setVisible(true);
setSize(850,850);
}

}




class StudentPanel extends JPanel implements ActionListener
{
JButton Save,Exit;

String Idstud[]=new String[5];
String Namestud[]=new String[5];
String Citystud[]=new String[5];

String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","City"};

JScrollPane jsp=null;
JTable table=null;
int i=0;

//function to communicate with server
public StudentPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display1");

dispIntf.Connect();
dispIntf.setData();


Save=new JButton("Save");
Exit=new JButton("Exit");

Idstud=dispIntf.getIDstud();
Namestud=dispIntf.getNamestud();
Citystud=dispIntf.getCitystud();

for(i=0;i<=4;i++)
{
System.out.print("ID="+Idstud[i]);
System.out.print(" Name="+Namestud[i]);
System.out.print(" City="+Citystud[i]);
System.out.println("");
}
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idstud[i];
data[i][j+1]=Namestud[i];
data[i][j+2]=Citystud[i];
}

}

}catch(Exception e){}


table = new JTable(data, colHeads);

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);



add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);

}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{

String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{

File in=new File("File1.txt");
fos=new FileOutputStream(in);

}catch(FileNotFoundException e){}


try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{

fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes());

}
fos.write(line.getBytes());

}
}catch(IOException e){}
}
}
}

class LibraryPanel extends JPanel implements ActionListener
{
JButton Save,Exit;

String Idbook[]=new String[5];
String Namebook[]=new String[5];
String Costbook[]=new String[5];

String[][] data=new String[5][3];
String[] colHeads = {"Id","Name","Cost"};

JScrollPane jsp=null;
JTable table=null;
int i=0;

public LibraryPanel()
{
try
{
interfacestud dispIntf =(interfacestud)Naming.lookup("rmi://localhost/Display2");
dispIntf.Connect();
dispIntf.setData();
Save=new JButton("Save");
Exit=new JButton("Exit");
Idbook=dispIntf.getIDBook();
Namebook=dispIntf.getNameBook();
Costbook=dispIntf.getCostBook();
for(i=0;i<=4;i++)
{
System.out.print("ID="+Idbook[i]);
System.out.print(" Name="+Namebook[i]);
System.out.print(" Cost="+Costbook[i]);
System.out.println("");
}

for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
data[i][j]=Idbook[i];
data[i][j+1]=Namebook[i];
data[i][j+2]=Costbook[i];
}

}

}catch(Exception e){}


table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
jsp = new JScrollPane(table, v, h);

add(jsp);
add(Save);
add(Exit);
Exit.addActionListener(this);
Save.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Exit)
System.exit(0);
if(ae.getSource()==Save)
{
String space="\t";
String line="\n";
FileOutputStream fos=null;
DataOutputStream dos=null;
try
{
File in=new File("File2.txt");
fos=new FileOutputStream(in);

}catch(FileNotFoundException e){}
try
{
for(i=0;i<=4;i++)
{
for(int j=0;j<1;j++)
{
fos.write(data[i][j].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+1].getBytes());
fos.write(space.getBytes());
fos.write(data[i][j+2].getBytes()); }
fos.write(line.getBytes());

}
}catch(IOException e){}
}
}

}
//class of client program with main method
public class client
{
public static void main(String args[])
{
dClient sc=new dClient();
}
}

Remote Method Invocation supporting the distributed computing in java

A) CalcIntf.java

import java.rmi.*; // Provides remote method invocation.
import java.util.*; // Contains common utilities.

//creation of interface
interface CalcIntf extends Remote
{
double Calculator(float a,float b,char ch) throws RemoteException;
}


CalcImpl.java

import java.rmi.*; // Provides remote method invocation.
import java.rmi.server.*; // Supports remote method invocation.
import java.io.*; //provides support for I/O operations
import java.util.*; // Contains common utilities.

public class CalcImpl extends UnicastRemoteObject implements CalcIntf //implementation of CalcIntf class
{
CalcImpl() throws RemoteException
{}
public double Calculator(float x,float y,char op) throws RemoteException
{
double z=0;
switch(op)
{
case '+': //addition
z=x+y;
break;
case '-': //substraction
z=x-y;
break;
case '*': //multiplication
z=x*y;
break;
case '/': //division
z=x/y;
break;
}
return z; //returns the result
}
}
server.java

//server program with main method
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class server
{
public static void main(String arg[]) throws Exception
{
CalcImpl obj=new CalcImpl();
Naming.rebind("Calci",obj);
}
}


client.java

//client program to communicate with server
import javax.swing.*;
import java.rmi.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.rmi.server.*;

public class client
{
public static void main(String arg[]) throws Exception
{
new Calci();
}
}

class Calci extends JFrame implements ActionListener,KeyListener
{
// variable declaration
float a,b;
char op;
GridBagLayout gb=new GridBagLayout();
//sets layout of screen
GridBagConstraints gbc=new GridBagConstraints();
Container cp=getContentPane();
Component com;
//textfield
JTextField t1=new JTextField(10);

//buttons
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton nine=new JButton("9");
JButton zero=new JButton("0");
JButton dot=new JButton(".");
JButton add=new JButton("+");
JButton sub=new JButton("-");
JButton mul=new JButton("*");
JButton div=new JButton("/");
JButton eql=new JButton("=");
JButton Clear=new JButton("Clear");

void addcom(int row,int col,int w,int h,Component cm)
{
gbc.gridx=col;
gbc.gridy=row;
gbc.gridwidth=w;
gbc.gridheight=h;
gb.setConstraints(cm,gbc);
cp.add(cm);
}

public Calci()
{
cp.setLayout(gb);
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(5,5,5,5);
gbc.weightx=1.0;
gbc.weighty=1.0;

t1.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
dot.addActionListener(this);
eql.addActionListener(this);
Clear.addActionListener(this);

t1.addKeyListener(this);
one.addKeyListener(this);
two.addKeyListener(this);
three.addKeyListener(this);
four.addKeyListener(this);
five.addKeyListener(this);
six.addKeyListener(this);
seven.addKeyListener(this);
eight.addKeyListener(this);
nine.addKeyListener(this);
zero.addKeyListener(this);
add.addKeyListener(this);
sub.addKeyListener(this);
mul.addKeyListener(this);
div.addKeyListener(this);
dot.addKeyListener(this);
eql.addKeyListener(this);
Clear.addKeyListener(this);

addcom(0,0,3,1,t1);
t1.setEnabled(false);
addcom(0,3,2,1,Clear);
addcom(1,0,1,1,one);
addcom(1,1,1,1,two);
addcom(1,2,1,1,three);
addcom(1,3,1,1,sub);
addcom(1,4,1,1,mul);
addcom(2,0,1,1,four);
addcom(2,1,1,1,five);
addcom(2,2,1,1,six);
addcom(2,3,1,3,eql);
addcom(2,4,1,1,div);
addcom(3,0,1,1,seven);
addcom(3,1,1,1,eight);
addcom(3,2,1,1,nine);
addcom(3,4,1,2,add);
addcom(4,0,2,1,zero);
addcom(4,2,1,1,dot);
requestFocus();
setSize(380,240);
setTitle("Calculator");
setVisible(true);
}


//methods to handle key events
public void keyReleased(KeyEvent ke)
{
char key = ke.getKeyChar();
if ( key == '+' )
{
t1.setText("");
}
else if ( key == '-' )
{
t1.setText("");
}
else if ( key == '*' )
{
t1.setText("");
}
else if ( key == '/' )
{
t1.setText("");
}
else if ( key == 'c' )
{
t1.setText("");
}
}

public void keyTyped(KeyEvent ke){}

public void keyPressed(KeyEvent ke)
{
char key = ke.getKeyChar();
if(ke.getKeyChar()>=ke.VK_0 && ke.getKeyChar()<=ke.VK_9)
{
t1.setText(t1.getText()+ke.getKeyChar());
}
if ( key == '+' )
{
a = Float.parseFloat(t1.getText());
op = '+';
t1.setText("");
}
if ( key == '-' )
{
a = Float.parseFloat(t1.getText());
op = '-';
}
if ( key == '*' )
{
a = Float.parseFloat(t1.getText());
op = '*';
}
if ( key == '/' )
{
a = Float.parseFloat(t1.getText());
op = '/';
}
if ( key == ke.VK_DELETE)
{
t1.setText("");
}
if ( key == ke.VK_ESCAPE)
{
t1.setText("");
}
if ( key == ke.VK_BACK_SPACE)
{
int bk;
bk=t1.getText().trim().length();
bk=bk-1;
if(bk<=0)
{
t1.setText("0.");
}
else
{
t1.setText(t1.getText().trim().substring(0,bk));
}
}
if ( key == ke.VK_ENTER)
{
b=(Float.valueOf(t1.getText())).floatValue();
try
{
CalcIntf A=(CalcIntf)Naming.lookup("Calci");
double ans=A.Calculator(a,b,op);
t1.setText(ans+"");
System.out.print(a);
System.out.print(op);
System.out.print(b);
System.out.println("="+ans);
}
catch(Exception e)
{}
}
if ( key == 'c' )
{
t1.setText("");
}
}

//methods to handle button events
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();

if(str.equals("1"))
t1.setText(t1.getText()+"1");
if(str.equals("2"))
t1.setText(t1.getText()+"2");
if(str.equals("3"))
t1.setText(t1.getText()+"3");
if(str.equals("4"))
t1.setText(t1.getText()+"4");
if(str.equals("5"))
t1.setText(t1.getText()+"5");
if(str.equals("6"))
t1.setText(t1.getText()+"6");
if(str.equals("7"))
t1.setText(t1.getText()+"7");
if(str.equals("8"))
t1.setText(t1.getText()+"8");
if(str.equals("9"))
t1.setText(t1.getText()+"9");
if(str.equals("0"))
t1.setText(t1.getText()+"0");
if(str.equals("."))
t1.setText(t1.getText()+".");
if(str.equals("Clear"))
t1.setText("");
if(str.equals("+"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='+';
t1.setText("");
}
if(str.equals("-"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='-';
t1.setText("");
}
if(str.equals("*"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='*';
t1.setText("");
}
if(str.equals("/"))
{
a=(Float.valueOf(t1.getText())).floatValue();
op='/';
t1.setText("");
}
if(str.equals("="))
{
b=(Float.valueOf(t1.getText())).floatValue();
try{
CalcIntf A=(CalcIntf)Naming.lookup("Calci");
double ans=A.Calculator(a,b,op);
t1.setText(" "+ans);
System.out.print(a);
System.out.print(op);
System.out.print(b);
System.out.println("="+ans);
t1.requestFocus();
}
catch(Exception e){}
}
}
}

IMPLEMENTATION OF QUEUE

/*Server Program */

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qser
{
//Declaration of variables
ServerSocket ss=null;
String queue[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on queue

public void check()
{
for(i=0;i<=4;i++)
{ queue[i]="";
}
try{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try{
System.out.println("Connection Established......");
s=ss.accept();

while(true)
{
//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);

if(x==1)//For the insert operation on queue
{
System.out.println("Insert");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(queue[i]=="")
break;
}

if(queue[4]!="")
System.out.println("Queue full...");
else
{
System.out.println("Insert Operation Performed");
queue[i]=str;
}
}

if(x==2)//For the delete operation on queue
{
System.out.println("Delete");
for(i=1;i>=5;i--)
{
if(queue[i]!="")
break;
}
System.out.println("Delete Operation Performed -" +queue[i]);
queue[i]="";
}

if(x==3) //returns the values to the client
{
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{

pw.println(queue[i]);
pw.flush();
}
}

if(x==4)
{
System.exit(0);
//terminates the program
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

class QueueServer //class with main method
{
public static void main(String args[])
{
qser qs=new qser();
qs.check();
}
}



/* Client Program*/

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class qclient
{
//Declaration of variables
Socket s=null;
String queue[]=new String[5];
String st;
String str;
int i,x;

public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
queue[i]="";
}
try{
//establish connection with server
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}

public void Contents() throws IOException
{
System.out.println("1 Insert");
System.out.println("2 Delete");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}

public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);

if(x==1)
{
Insert(); // insert operation
}
if(x==2)
{
Values(); // delete operation
}
if(x==3)
{
Display();// displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}

public void Insert() throws IOException
{
System.out.println("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}

public void Display() throws IOException
{
System.out.println("Items in queue");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
queue[i]=br2.readLine();
System.out.println(queue[i]);
}
Values();
}
}
class QueueClient
{
public static void main(String args[]) throws IOException
{
qclient cl=new qclient();
cl.check();
cl.Contents();
}
}

Simulating Network Operating System commands

A) IMPLEMENTATION OF STACK

/*Server Program */

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stser
{
//Declaration of variables
ServerSocket ss=null;
String stack[]=new String[5];
int x;
Socket s=null;
String str=null;
int i;
//Function to perform operations on stack
public void check()
{
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
//Creates server socket on the specified port
ss=new ServerSocket(2000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
System.out.println("Connection Established......");
s=ss.accept();

while(true)
{//Input stream that translates bytes to characters
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
str=br.readLine();
x=Integer.parseInt(str);

if(x==1) //For the Push operation on stack
{
System.out.println("Push");
str=br.readLine();
for(i=0;i<=4;i++)
{
if(stack[i]=="")
break;
}
if(stack[4]!="")
System.out.println("Stack full...");
else
{
System.out.println("Push Operation Performed");
stack[i]=str;
}
}

if(x==2) //For the Pop operation on stack
{
System.out.println("Pop");
for(i=4;i>=0;i--)
{
if(stack[i]!="")
break;
}
System.out.println("Pop Operation Performed-" +stack[i]);
stack[i]="";
}

if(x==3)
//returns the values to the client
{//Output stream that contains print( ) and println( )
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
for(i=4;i>=0;i--)
{
pw.println(stack[i]);
pw.flush();
}
}
if(x==4) //terminates the program
{
System.exit(0);
}
}
}

catch(IOException e)

{
System.out.println(e);
}
}
}


class StackServer //class which contains the main method
{
public static void main(String args[])
{
stser st=new stser();
st.check();
}
}


/* Client Program */

import java.net.*; //provides support for networking
import java.io.*; //provides support for I/O operations
class stclient
{
//Declaration of variables
Socket s=null;
String stack[]=new String[5];
String st;
String str;
int i,x;

public void check()
{
//initialization of stack
for(i=0;i<=4;i++)
{
stack[i]="";
}
try
{
s=new Socket(InetAddress.getLocalHost(),2000);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Contents() throws IOException
{
System.out.println("1 Push");
System.out.println("2 Pop");
System.out.println("3 Display");
System.out.println("4 Exit");
Values();
}



public void Values() throws IOException
{
System.out.print("Select any 1 operation: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
x=Integer.parseInt(str);
if(x==1)
{
Push();//push operation

}
if(x==2)
{
Values(); // pop operation
}
if(x==3)
{
Display (); // displays the result
}
if(x==4)
{
System.exit(0); //exit from the program
}
}
public void Push() throws IOException
{
System.out.print("Enter data: ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
str=br1.readLine();
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println(str);
Values();
}
public void Display() throws IOException
{
System.out.println("Items in Stack");
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(i=0;i<=4;i++)
{
stack[i]=br2.readLine();
System.out.println(stack[i]);
}
Values();
}
}

class StackClient
{
public static void main(String args[]) throws IOException
{
stclient cl=new stclient();
cl.check();
cl.Contents();
}
}