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

Cache path information in Intermediate node Using Java

Description:

This program illustrates the concept of Pathcache. . In CachePath, mobile nodes cache the data path and use it to redirect future requests to the nearby node, which has the data instead of the faraway data center.Node1 get the data from DataCenter and cache its path on the hash table. When the intermediate node request the same data it will serve the path information of the data. . By caching the data path for each data item, bandwidth and the query delay can be reduced since the data can be obtained through less number of hops. Using the Socket programming Node and data center are connected and data transmitted between them using ObjectInputStream and ObjectOutputStream classes.

Cache the path information in Intermediate node 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 /*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; } /*Node1.java*/ import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.sql.*; public class Node1 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="Node1"; int port=4700; String s = ""; boolean a=false; boolean b=false; ObjectOutputStream out; ObjectInputStream in; ObjectOutputStream out1; ObjectInputStream in1; ServerSocket node1=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; public Node1() { initComponents(); setBounds(150,150,410,370); 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); } } 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("Node1"); 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 1"); 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,"Node1 request "+Name+" details",Name, path,desc); String stockval=(String)DataVector.elementAt(1); String sdesc=(String)DataVector.elementAt(2);//sdesc Stockvalue.setText(stockval); } else if(Sel.equalsIgnoreCase("product")) { Name=(String)Product.getSelectedItem(); Vector DataVector=getData(Sel,"Node1 request "+Name+" details",Name, path,desc); String pdet=(String)DataVector.elementAt(1); String Pri=(String)DataVector.elementAt(2); String pdesc=(String)DataVector.elementAt(3);//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); }else if(Sel.equalsIgnoreCase("product")) { JOptionPane.showMessageDialog(null,"Data is coming from "+source); //JOptionPane.showMessageDialog(null,"The Datas in Node1 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 Node1().show(); } //********************run()********************// public void run() { try { while(true) { node5=node1.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+"Node1 to Datacenter"); Comp.addElement(desc); } else if(Sel.equalsIgnoreCase("product")) { Comp.addElement(nodename); Comp.addElement(Name); Comp.addElement(path+"Node1 to Datacenter"); Comp.addElement(desc); } String pr=(String)Prod.get(Name); String co=(String)comp.get(Name); if(co!=null) { DataVector.addElement("Node1"); //******************* 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) { DataVector.addElement("Node1"); //********************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 { DataVector.addElement("path"); } 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; String stockval=(String)Det.elementAt(0); String ctime = (String)Det.elementAt(1); String sdesc = (String)Det.elementAt(2);//sdesc source=(String)Det.elementAt(3); DataVector.addElement(stockval); DataVector.addElement(ctime); DataVector.addElement(sdesc);//sdesc DataVector.addElement("DataCenter"); comp.put(Name,"DataCenter"); } else if(Sel.equalsIgnoreCase("product")) { String pdet = (String)Det.elementAt(0); String Pri = (String)Det.elementAt(1); String ptime = (String)Det.elementAt(2); String pdesc = (String)Det.elementAt(3);//pdesc source=(String)Det.elementAt(4); DataVector.addElement(pdet); DataVector.addElement(Pri); DataVector.addElement(ptime); DataVector.addElement(pdesc);//pdesc DataVector.addElement("DataCenter"); Prod.put(Name,"DataCenter"); } in.close(); out.close(); } catch(ConnectException e) { JOptionPane.showMessageDialog(null,"Start the Server"); System.out.println("E3"+e); } catch(Exception fff) { System.out.println("E3"+fff); } return DataVector; } public void delete() { System.out.println("Inside the Delete Method"); System.out.println("The hase table value is:"+comp); comp.clear(); compcount=0; System.out.println("The hase table value is(After clear):"+comp); } private javax.swing.JTextField Stockvalue; private javax.swing.JButton Push; private javax.swing.JPanel jPanel1; private javax.swing.JButton Request; private javax.swing.JButton Path; private javax.swing.JButton Exit; private javax.swing.JTextField Price; private javax.swing.JComboBox Company; private javax.swing.JComboBox Select; private javax.swing.JComboBox Product; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel6; private javax.swing.JTextField name; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel1; } /*ProductName.java*/ 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

Sample ScreenShot:

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom