© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

Cache the data in Intermediate nodes Using Java

Description:

This program illustrates the concept of Datacache. In CacheData, intermediate nodes cache the data to serve future requests instead of fetching data from the data center.Node2 get the data from DataCenter and cache it on the hash table. When the intermediate node request the same data it will serve the request from its hashtable. Data cache retains popular data items for a long period of time and other data items are invalidated within a short period due to the limited cache space. Once the time period runs out data items are invalidated and removed from the data cache.Using the Socket programming Node and data center are connected and data transmitted between them using ObjectInputStream and ObjectOutputStream classes.

Cache the data in Intermediate nodes Using Java :

/*CompanyName.java*/ TCS INFOSYS WIPRO HCL CTS HP SATYAM POLARIS SASKEN IGATE HONEYWELL MICROSOFT SUN ORACLE IFLEX INTEL IBM DELL COVANSYS GOOGLE LASERSOFT CARITER LT BARCLAYS RAMCO BIRLASOFT COSMASOFT SSIINFOTECH VIRTUSHA SAVVYSYSTEMS VALLEYCREAK JJSOFTWARES EAGLECREAK GLAXOSMITHKLIEN RANETRW RELIENCE BHARTHI MITTALGROUP FIRSTSOFT DATAPATTERNS PMRSOFTWARES ABNAMRO SBI ICICI UTI CBI CANARABANK HDFC IDBI FORD MARUTI HEROHONDA TATAMOTORS BENZ TOYOTA /*ProductName*/ BOOK MOBILE TV CAR BIKE SHIRTS PANTS MONITOR LCD PRINTER DVD CYCLE PEN OS ZERONIC TYRE SAMSUNG PAPER XEROX MACHINE TOOTH PASTE HAIR OIL FAN LIGHTS CHAIRS TABLE SINTEX WASHING MACHINE FACIAL HERBAL POWDER STEBLIZER /*DataCenter.java*/ import javax.swing.*; import java.io.*; import java.sql.*; import java.net.*; import java.util.*; public class DataCenter extends javax.swing.JFrame implements Runnable { /** Creates new form DataCenter */ int login=0; ServerSocket server=null; Socket node1=null; ObjectInputStream in; ObjectOutputStream out; Vector Det=new Vector(); public DataCenter() { initComponents(); try { server=new ServerSocket(4500); } catch(Exception e) { System.out.println(" uuu "+e); } setBounds(150,150,500,570); } /* INIT COMPONENTS */ private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); getContentPane().setLayout(null); setTitle("DATACENTER"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jPanel1.setLayout(null); jPanel1.setBackground(new java.awt.Color(51, 204, 255)); jLabel1.setFont(new java.awt.Font("Dialog", 3, 24)); jLabel1.setText("DATA CENTER"); jPanel1.add(jLabel1); jLabel1.setBounds(100, 30, 180, 30); jLabel2.setText("REQUEST DETAILS"); jPanel1.add(jLabel2); jLabel2.setBounds(130, 90, 120, 16); jScrollPane1.setViewportView(jTextArea1); jPanel1.add(jScrollPane1); jScrollPane1.setBounds(20, 120, 350, 260); jButton1.setText("start"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton1.setBounds(160, 400, 61, 26); getContentPane().add(jPanel1); jPanel1.setBounds(0, 0, 1010, 530); pack(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { new Thread(this).start(); } catch(Exception e) { System.out.println("ffff "+e); } } private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } public static void main(String args[]) { new DataCenter().show(); } public void run() { try { while(true){ node1=server.accept(); in=new ObjectInputStream(node1.getInputStream()); java.util.Vector Comp=(java.util.Vector)in.readObject(); String Req=null; String Name=null; String stockval=null; String sdesc=null; String pdesc=null; String count = null; String pdet=null; String Pri=null; int d1=0; String dd1=null; int p1 = 0; String pp1 = null; try{ Det.removeAllElements(); String Sel=(String)Comp.elementAt(0); if(Sel.equalsIgnoreCase("Company")) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:adhocstock"); Statement st=con.createStatement(); Req=(String)Comp.elementAt(1); System.out.println("The first element of Comp vector is"+Req); Name=(String)Comp.elementAt(2); String path=(String)Comp.elementAt(3); sdesc=(String)Comp.elementAt(4);//detail description of stock System.out.println("The Path is:"+path); //***********************Log File************************ try{ login++; FileWriter out1=new FileWriter("log.log",true); BufferedWriter out2=new BufferedWriter(out1); out2.write( login+" Miss : "+Req ); out2.newLine(); out2.close(); out1.close(); } catch(Exception e){ System.out.println("iii "+e); } //***********************Log File************************ jTextArea1.append(Req+"\n"); ResultSet rs=st.executeQuery("select * from stock where company='"+Name+"'"); while(rs.next()) { stockval=String.valueOf(rs.getInt("stockval")); sdesc = rs.getString("description"); java.util.Date d=new java.util.Date(); d1 = d.getMinutes(); dd1 = String.valueOf(d1); System.out.println("CURRENT TIME:"+dd1); } Det.addElement(stockval); Det.addElement(dd1); Det.addElement(sdesc);//detail desc Det.addElement("DataCenter"); } else if(Sel.equalsIgnoreCase("Product")) { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:adhocstock"); Statement st=con.createStatement(); Req=(String)Comp.elementAt(1); Name=(String)Comp.elementAt(2); String path = (String)Comp.elementAt(3); System.out.println("The Path is:"+path); pdesc=(String)Comp.elementAt(4);//detail desc for produst System.out.println("PssssssPPP "+pdesc); //***********************Log File************************ try{ login++; FileWriter out1=new FileWriter("log.log",true); BufferedWriter out2=new BufferedWriter(out1); out2.write( login+" Miss : "+Req ); out2.newLine(); out2.close(); out1.close(); } catch(Exception e){ System.out.println("PPPP "+e); } //***********************Log File************************ jTextArea1.append(Req+"\n"); ResultSet rs1=st.executeQuery("select * from product where product='"+Name+"'"); while(rs1.next()) { pdet=rs1.getString("product details"); pdesc = rs1.getString("description"); Pri=String.valueOf(rs1.getInt("price")); java.util.Date p = new java.util.Date(); p1 = p.getMinutes(); pp1 = String.valueOf(p1); } Det.addElement(pdet); Det.addElement(Pri); Det.addElement(pp1); Det.addElement(pdesc);//pdesc Det.addElement("DataCenter"); } }catch(Exception e) { System.out.println("loploplp "+e); } out=new ObjectOutputStream(node1.getOutputStream()); out.writeObject(Det); out.close(); in.close(); } } catch(Exception f) { System.out.println("ggghh "+f); } } private javax.swing.JPanel jPanel1; private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel1; } //**************************NODE2.java**************************// import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.sql.*; public class Node2 extends javax.swing.JFrame implements Runnable { int j =0; int login = 0; int compcount=0; int prodcount=0; int compcount1=0; int prodcount1=0; int count=1; String node="Node2"; int port=4600; String s = ""; boolean a=false; boolean b=false; ObjectOutputStream out; ObjectInputStream in; ObjectOutputStream out1; ObjectInputStream in1; ServerSocket node2=null; Socket node5=null; String source=null; Hashtable comp=new Hashtable(); Hashtable Prod=new Hashtable(); Hashtable comp1=new Hashtable(); Hashtable Prod1=new Hashtable(); Hashtable ccomp=new Hashtable(); Vector Det=new Vector(); //Vector Comp=new Vector(); String Name=null; //String fPath="Node 2 to DataCenter"; //********** Creates new form Node2**********// public Node2() { initComponents(); setBounds(150,150,410,370); //setBounds(0,0,800,550); //*************set the Visible for Company*************// jLabel2.setVisible(a); jLabel4.setVisible(a); Company.setVisible(a); Stockvalue.setVisible(a); jLabel3.setVisible(b); jLabel5.setVisible(b); jLabel6.setVisible(b); Product.setVisible(b); Price.setVisible(b); name.setVisible(b); Request.setVisible(false); Select.addItem("Company"); Select.addItem("Product"); try { File Co=new File("CompanyName.txt"); File Pr=new File("ProductName.txt"); FileInputStream fin=new FileInputStream(Co); FileInputStream fin1=new FileInputStream(Pr); DataInputStream din=new DataInputStream(fin); DataInputStream din1=new DataInputStream(fin1); String SCom=din.readLine(); while(SCom!=null) { Company.addItem(SCom); SCom=din.readLine(); } String SPr=din1.readLine(); while(SPr!=null) { Product.addItem(SPr); SPr=din1.readLine(); } } catch(Exception r) { System.out.println("E1"+r); } try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:adhocstock"); Statement st1=con.createStatement(); int rs1=st1.executeUpdate("insert into Nodes values('"+node+"',"+port+")"); node2=new ServerSocket(4600); new Thread(this).start(); } catch(Exception f) { System.out.println("E2"+f); } } private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); Select = new javax.swing.JComboBox(); Push = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); Company = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); Product = new javax.swing.JComboBox(); jLabel5 = new javax.swing.JLabel(); name = new javax.swing.JTextField(); Price = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); Stockvalue = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); Request = new javax.swing.JButton(); Path = new javax.swing.JButton(); Exit = new javax.swing.JButton(); getContentPane().setLayout(null); setTitle("Node2"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jPanel1.setLayout(null); jPanel1.setBackground(new java.awt.Color(204, 255, 204)); jLabel1.setFont(new java.awt.Font("Dialog", 3, 18)); jLabel1.setForeground(new java.awt.Color(255, 0, 255)); jLabel1.setText("NODE 2"); jPanel1.add(jLabel1); jLabel1.setBounds(170, 10, 80, 24); jLabel7.setText("Select"); jPanel1.add(jLabel7); jLabel7.setBounds(100, 60, 41, 20); Select.setBackground(new java.awt.Color(255, 255, 255)); Select.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { SelectItemStateChanged(evt); } }); jPanel1.add(Select); Select.setBounds(150, 60, 110, 25); Push.setBackground(new java.awt.Color(255, 255, 255)); Push.setText("Push"); Push.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PushActionPerformed(evt); } }); jPanel1.add(Push); Push.setBounds(270, 60, 70, 26); jLabel2.setText("COMPANY"); jPanel1.add(jLabel2); jLabel2.setBounds(10, 120, 70, 16); Company.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.add(Company); Company.setBounds(90, 120, 90, 25); jLabel3.setForeground(new java.awt.Color(0, 0, 0)); jLabel3.setText("PRODUCT"); jPanel1.add(jLabel3); jLabel3.setBounds(210, 120, 56, 16); Product.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.add(Product); Product.setBounds(290, 120, 100, 25); jLabel5.setText("NAME"); jPanel1.add(jLabel5); jLabel5.setBounds(210, 180, 33, 16); name.setEditable(false); jPanel1.add(name); name.setBounds(290, 180, 100, 20); Price.setEditable(false); jPanel1.add(Price); Price.setBounds(290, 240, 100, 20); jLabel6.setText("PRICE"); jPanel1.add(jLabel6); jLabel6.setBounds(210, 240, 34, 16); Stockvalue.setEditable(false); jPanel1.add(Stockvalue); Stockvalue.setBounds(90, 210, 90, 20); jLabel4.setText("STOCKVALUE"); jPanel1.add(jLabel4); jLabel4.setBounds(0, 210, 80, 16); Request.setBackground(new java.awt.Color(255, 255, 255)); Request.setText("REQUEST"); Request.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RequestActionPerformed(evt); } }); jPanel1.add(Request); Request.setBounds(60, 310, 100, 26); Path.setBackground(new java.awt.Color(255, 255, 255)); Path.setText("DETAILS"); Path.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PathActionPerformed(evt); } }); jPanel1.add(Path); Path.setBounds(160, 310, 100, 26); Exit.setBackground(new java.awt.Color(255, 255, 255)); Exit.setText("Exit"); Exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ExitActionPerformed(evt); } }); //jPanel1.add(Exit); Exit.setBounds(260, 310, 100, 26); getContentPane().add(jPanel1); jPanel1.setBounds(0, 0, 400, 350); //jPanel1.setBounds(0, 0, 800, 550); pack(); } private void RequestActionPerformed(java.awt.event.ActionEvent evt) { String Sel=(String)Select.getSelectedItem(); String Name=null; String path = null; String desc = null; if(Sel.equalsIgnoreCase("Company")) { Name=(String)Company.getSelectedItem(); Vector DataVector=getData(Sel,"Node2 request "+Name+" details",Name, path,desc); String stockval=(String)DataVector.elementAt(0); String sdesc=(String)DataVector.elementAt(1);//sdesc Stockvalue.setText(stockval); } else if(Sel.equalsIgnoreCase("product")) { Name=(String)Product.getSelectedItem(); Vector DataVector=getData(Sel,"Node2 request "+Name+" details",Name, path,desc); String pdet=(String)DataVector.elementAt(0); String Pri=(String)DataVector.elementAt(1); String pdesc=(String)DataVector.elementAt(2);//pdesc name.setText(pdet); Price.setText(Pri); } } private void PathActionPerformed(java.awt.event.ActionEvent evt){ String Sel=(String)Select.getSelectedItem(); if(Sel.equalsIgnoreCase("Company")) { JOptionPane.showMessageDialog(null,"Data is coming from"+source+comp); }else if(Sel.equalsIgnoreCase("product")) { JOptionPane.showMessageDialog(null,"Info is coming from"+source+Prod); //JOptionPane.showMessageDialog(null,"The Datas in Node2 is"+DataVector); } } private void ExitActionPerformed(java.awt.event.ActionEvent evt){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:adhocstock"); Statement st=con.createStatement(); int res= st.executeUpdate("delete * from Nodes where Node='"+node+"'"); if(res==1){ System.exit(0); } }catch(Exception e){ } } private void SelectItemStateChanged(java.awt.event.ItemEvent evt) { a=false;b=false; //*************set the Visible for Company*************// jLabel2.setVisible(a); jLabel4.setVisible(a); Company.setVisible(a); Stockvalue.setVisible(a); //*************set the Visible for Product*************// jLabel3.setVisible(b); jLabel5.setVisible(b); jLabel6.setVisible(b); Product.setVisible(b); Price.setVisible(b); name.setVisible(b); Request.setVisible(false); Path.setVisible(false); } private void PushActionPerformed(java.awt.event.ActionEvent evt) { String Name=(String)Select.getSelectedItem(); if(Name.equalsIgnoreCase("Company")){ a=true; jLabel2.setVisible(a); jLabel4.setVisible(a); Company.setVisible(a); Stockvalue.setVisible(a); } else if(Name.equalsIgnoreCase("Product")){ b=true; jLabel3.setVisible(b); jLabel5.setVisible(b); jLabel6.setVisible(b); Product.setVisible(b); Price.setVisible(b); name.setVisible(b); } Request.setVisible(true); Path.setVisible(true); Stockvalue.setText(""); Price.setText(""); name.setText(""); } private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); } public static void main(String args[]) { new Node2().show(); } //********************run()********************// public void run() { try { while(true) { node5=node2.accept(); Vector Det1=new Vector(); in1=new ObjectInputStream(node5.getInputStream()); java.util.Vector Comp1=(java.util.Vector)in1.readObject(); String Sel=(String)Comp1.elementAt(0); String Cname=null; if(Sel.equalsIgnoreCase("Company")) { String nodename=(String)Comp1.elementAt(1); String Name = (String)Comp1.elementAt(2); String path = (String)Comp1.elementAt(3); String desc = (String)Comp1.elementAt(4); Vector DataVector=getData(Sel,nodename,Name,path,desc); Det1=DataVector; } else if(Sel.equalsIgnoreCase("product")) { String nodename=(String)Comp1.elementAt(1); String Name=(String)Comp1.elementAt(2); String path = (String)Comp1.elementAt(3); String desc = (String)Comp1.elementAt(4); Vector DataVector=getData(Sel,nodename,Name,path,desc); Det1=DataVector; } out1=new ObjectOutputStream(node5.getOutputStream()); out1.writeObject(Det1); out1.close(); } }catch(Exception g) { System.out.println("E4"+g); } } //******************** getData()********************// public Vector getData(String Sel,String nodename,String Name,String path,String desc) { Vector Comp=new Vector(); Vector Det=new Vector(); Vector DataVector=new Vector(); int d1 = 0; int ttime = 0; try { Comp.addElement(Sel); if(Sel.equalsIgnoreCase("Company")) { Comp.addElement(nodename); Comp.addElement(Name); Comp.addElement(path+"Node2 to Datacenter"); Comp.addElement(desc); } else if(Sel.equalsIgnoreCase("product")) { Comp.addElement(nodename); Comp.addElement(Name); Comp.addElement(path+"Node2 to Datacenter"); Comp.addElement(desc); } String pr=(String)Prod.get(Name); String co=(String)comp.get(Name); if(co!=null) { StringTokenizer tr = new StringTokenizer(co,":"); String t = tr.nextToken(); String t1 = tr.nextToken(); String t2 = tr.nextToken(); String t5 = tr.nextToken(); String sd = tr.nextToken();//sdesc int tt5 = Integer.parseInt(t5); int ccount = Integer.parseInt(t2); ccount = ccount + 1; String t3 = String.valueOf(ccount); String t4 = t+":"+t1+":"+t3+":"+t5+":"+sd;//sd for sdesc comp.put(Name,t4); comp1.put(path,Name); DataVector.addElement(t); DataVector.addElement(t5); DataVector.addElement(sd);//sdesc DataVector.addElement("Node2"); //******************* LOG FILE*******************// try{ login++; FileWriter out1=new FileWriter("log.log",true); BufferedWriter out2=new BufferedWriter(out1); out2.write(login+ " Hit : "+nodename ); out2.newLine(); out2.close(); out1.close(); } catch(Exception e){ System.out.println("jjj "+e); } } else if(pr!=null) { StringTokenizer so=new StringTokenizer(pr,":"); String pname = so.nextToken(); String prdet =so.nextToken(); String ppri = so.nextToken(); String pc=so.nextToken(); String protime=so.nextToken(); String pd=so.nextToken();//pdesc int prcount = Integer.parseInt(pc); prcount = prcount + 1; String prcnt1 = String.valueOf(prcount); String pro = pname+":"+prdet+":"+ppri+":"+prcnt1+":"+protime+":"+pd;//pd for pdesc Prod.put(Name,pro); DataVector.addElement(prdet); DataVector.addElement(ppri); DataVector.addElement(protime); DataVector.addElement(pd);//pdesc DataVector.addElement("Node2"); //********************LOGFILE********************// try{ login++; FileWriter out1=new FileWriter("log.log",true); BufferedWriter out2=new BufferedWriter(out1); out2.write( login+" Hit : "+nodename ); out2.newLine(); out2.close(); out1.close(); } catch(Exception e){ System.out.println("ooooo "+e); } } else { Socket node2c=new Socket("localhost",4500);//node1 out=new ObjectOutputStream(node2c.getOutputStream());//node1 out.writeObject(Comp); in=new ObjectInputStream(node2c.getInputStream());//node1 Det=(Vector)in.readObject(); if(Sel.equalsIgnoreCase("Company")) { String delKeyName=null; int delKeyValue=0; StringTokenizer st=null; if(compcount>=5) { System.out.println("\n\tThe Computer Hash Table is full\n\t"); Enumeration e1 = comp.keys(); while(e1.hasMoreElements()) { try{ String keyName=(String)e1.nextElement(); String keyValue=(String)comp.get(keyName); System.out.println("KEYNAME\n\t"+keyName+"\nKEY VALUE\n\t"+keyValue); System.out.println("The Delkeyname is:\t"+delKeyName+"\nThe DelKeyvalue is:\t"+delKeyValue); if(j==0){ delKeyName=keyName; st=new StringTokenizer(keyValue,":"); st.nextToken(); st.nextToken(); st.nextToken();//sdesc String va=(String)st.nextToken(); delKeyValue=Integer.parseInt(va); j++; } else{ st=new StringTokenizer(keyValue,":"); st.nextToken(); st.nextToken(); st.nextToken();//sdesc String va=(String)st.nextToken(); int che=Integer.parseInt(va); int gettime=Integer.parseInt(st.nextToken()); System.out.println("Get time from comp hash table:"+gettime); java.util.Date systime=new java.util.Date(); int stime=systime.getMinutes(); System.out.println("Get system time from system:"+stime); //**************We need to get time and check the time**************// if(che=5) { Enumeration e1 = Prod.keys(); while(e1.hasMoreElements()) { try{ String keyName=(String)e1.nextElement(); String keyValue=(String)Prod.get(keyName); System.out.println("KEYNAME\n\t"+keyName+"\nKEY VALUE\n\t"+keyValue); System.out.println("The Delkeyname is:\t"+delKeyName+"\nThe DelKeyvalue is:\t"+delKeyValue); if(j==0) { delKeyName=keyName; st=new StringTokenizer(keyValue,":"); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken();//pdesc String va=(String)st.nextToken(); delKeyValue=Integer.parseInt(va); j++; } else { st=new StringTokenizer(keyValue,":"); st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken();//pdesc String va=(String)st.nextToken(); int che=Integer.parseInt(va); if(che

Sample ScreenShot:

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom