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

Caching the data in Proxy while getting data from Main Server Using Java


Client request the data from main server through proxy. Request of client is passed to proxy, it checks the cache memory. If the data is available on the cache memory then the requested file will be sent by the proxy. Otherwise, it will pass the request to main server. Main server gives the client requested file to the proxy. Proxy will cache the data for future use and it will give it to the client. In this program we are requesting the mpg file and playing the file on the client side using JMF (Java Media Framework).Using the socket program Server, proxy and client are connected with each other. ServerSocket and Socket classes are used for the connection. Port no must be specified in the code, you have to give the server and proxy system names as input.

Caching the data in Proxy while getting data from Main Server Using Java:

/*FileByte.java*/ //package MServer; import java.io.*; /** * * @author student */ public class FileByte implements Serializable { private byte by[]; /** Creates a new instance of FileByte */ public FileByte() { } public void setByte(byte a[]) { by=a; } public byte[] getByte(){ return by; } } /*Client2.java*/ import java.util.*; import java.net.*; import java.io.*; import javax.media.*; import javax.swing.*; import java.awt.*; public class Client2 extends javax.swing.JFrame implements ControllerListener,Runnable{ Socket clientSocket; Hashtable FileDetails = new Hashtable(); Hashtable clientDetails = new Hashtable(); Hashtable FileRecentAccTime = new Hashtable(); String count="1"; FileByte fby; String Dest; String Dest1; String FileName; String fnm; String fnam; String IServerName; int replacementcount = 3; int pcahecapacity=1000000; //folder capacity ServerSocket cient1ServerSocket; Socket client1Socket; /* player */ String filepath; Player player; Component visualComponent; Component controlComponent; Component progressBar; boolean firstTime; long CachingSize; int controlPanelHeight; int videoWidth; int videoHeight; /** Creates new form Client2 */ public Client2(String IServerName) { System.out.println("Inside the Client2 Constructor"); this.IServerName=IServerName; initComponents(); setBounds(50,50,880,550); File fi=new File("C:/Content Processing/Client2/PCache/"); File fary[]=fi.listFiles(); for(int i=0;i=pcahecapacity){ System.out.println("The Folder Capacity Size is increase\n\n"); Hashtable temp=new Hashtable(); Enumeration e1=FileDetails.keys(); while(e1.hasMoreElements()){ FName=(String)e1.nextElement(); System.out.println("The value11 of tempval "+tempval); File ff=new File("C:/Content Processing/IServer/PCache/"+FName); double FileCapacity=(double)ff.length(); System.out.println("The Capacity of the "+FName+"is:"+FileCapacity); String s1=(String)FileRecentAccTime.get(FName); StringTokenizer st1=new StringTokenizer(s1,":"); String s2=st1.nextToken(); double ds2 = Double.parseDouble(s2)*60*60; String s3=st1.nextToken(); double ds3 = Double.parseDouble(s3)*60; String s4=st1.nextToken(); double ds4 = Double.parseDouble(s4); double RecentaccessTime= ds2+ds3+ds4; System.out.println("The Recent Access Time of the "+FName+"is:"+RecentaccessTime); String ts1=(String)FileDetails.get(FName); System.out.println("filedetails in chk length"+FileDetails); st1=new StringTokenizer(ts1,":"); String ts2=st1.nextToken(); double dts2 = Double.parseDouble(ts2)*60*60; String ts3=st1.nextToken(); double dts3 = Double.parseDouble(ts3)*60; String ts4=st1.nextToken(); double dts4 = Double.parseDouble(ts4); String ts5=st1.nextToken(); System.out.println("ts4 value is"+ts4); double FileStoredTime=dts2+dts3+dts4; System.out.println("The Stored time of the "+FName+"is:"+FileStoredTime); Date d=new Date(); String ss1=String.valueOf(d.getHours()); double dss1 = Double.parseDouble(ss1)*60*60; String ss2=String.valueOf(d.getMinutes()); double dss2 = Double.parseDouble(ss2)*60; String ss3=String.valueOf(d.getSeconds()); double dss3 = Double.parseDouble(ss3); double CurrentTime=dss1+dss2+dss3; System.out.println("The Current Time of the "+FName+"is:"+CurrentTime); double NumberofRequest=Double.parseDouble(ts5); System.out.println("The Number of the Request "+FName+"is:"+NumberofRequest); double data=CurrentTime - RecentaccessTime; System.out.println("The Number of data "+data); if(tempval == 0){ System.out.println("The value22 of tempval "+tempval); }else if(tempval < data){ System.out.println("inside if loop"); System.out.println("The value of tempval "+tempval); FileDetails.remove(FName); FileRecentAccTime.remove(FName); File fi1=new File("C:/Content Processing/Client2/PCache/"+FName); fi1.delete(); System.out.println("success1111111"); }else if(tempval > data){ System.out.println("inside elseif2222 loop"); System.out.println("The value of tempval "+tempval); FileDetails.remove(tfname); FileRecentAccTime.remove(tfname); File fi1=new File("C:/Content Processing/Client2/PCache/"+tfname); fi1.delete(); System.out.println("success22222222"); } tfname = FName; System.out.println("The value of tfname "+tfname); tempval = data; System.out.println("The value of tempval "+tempval); System.out.println("The value data of data "+data); System.out.println("The current File Popularity is:"+data); temp.put(FName,(String.valueOf(data))); System.out.println("The Temporary HashTable value is:"+temp); }//End OF While Loop }//End of len check fos=new FileOutputStream("C:\\Content Processing\\Client2\\PCache\\"+fnam); txtArea.setText(" "); txtArea.append("RFile: "+FileName+" from "+Dest+"\n"); fos.write(byt); System.out.println("File write sucessfully "); fos.flush(); fos.close(); Date d=new Date(); String min=String.valueOf(d.getHours())+":"+String.valueOf(d.getMinutes())+":"+String.valueOf(d.getSeconds()); String con=min+":"+count; System.out.println("The Collection(Time and count is) value is:"+con); FileDetails.put(FileName,con); //list.add(FileName+"***"+count); FileRecentAccTime.put(FileName,min); System.out.println("The Hash Table(FileDetails) Output is"+FileDetails); list.removeAll(); list.add("FileName"+"***"+"Count"+"***"+"Time"); Set set = FileDetails.keySet(); Iterator ite = set. iterator(); while(ite.hasNext()){ String stfil = (String)ite.next(); String str = (String)FileDetails.get(stfil); System.out.println(str); StringTokenizer sto=new StringTokenizer(str,":"); String rs1=sto.nextToken(); String rs2=sto.nextToken(); String rs3=sto.nextToken(); String rs5=rs1+":"+rs2+":"+rs3; String rs4=sto.nextToken(); System.out.println("stfil is::"+stfil+"get(stfil) is"+rs4+"time is"+rs5); list.add(stfil+"***"+rs4+"***"+rs5); } System.out.println("The File is Write to PCahe"); } fos=new FileOutputStream("C:\\Content Processing\\Client2\\TCache\\Temp.mpg"); fos.write(byt); fos.flush(); fos.close(); if(FileName.endsWith(".mpg")){ ImageIcon img1=null; l_show_img.setIcon(img1); l_show_img.setVisible(false); VedioAudioPlayer("C:\\Content Processing\\Client2\\TCache\\Temp.mpg"); } } }catch(Exception e){ System.out.println("The display error"+e); e.printStackTrace(); } } /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.out.println("Client2 is ready to Exit"); Vector VCli=new Vector(); VCli.addElement("ClientDisConnect"); VCli.addElement("Client2"); System.out.println("The Clinet Joind vector output is:"+VCli); Vector Receive=sendreceive(VCli,4000,this.IServerName); System.out.println("The CF value is:"+Receive); String Data=(String)Receive.elementAt(0); System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { System.out.println("Client2 is Ready to Start"); String IServerName=null; if(args.length==0){ IServerName=JOptionPane.showInputDialog("Please Enter the ProxyServer Name"); if(IServerName.length()==0) System.exit(0); } else{ IServerName=args[0]; } new Client2(IServerName).show(); } public void VedioAudioPlayer(String file) { //playing video code destroy(); System.out.println("The Asking File is ready to Play"); player = null; visualComponent = null; controlComponent = null; progressBar = null; firstTime = true; CachingSize = 0L; controlPanelHeight = 0; videoWidth = 0; videoHeight = 0; filepath=file; System.out.println("all parameter initalied"); init(); } public void init() { String s = null; MediaLocator medialocator = null; Object obj = null; URL url; if(filepath == null) Fatal("Invalid media file parameter"); try { //url = new URL(filepath); if((medialocator = new MediaLocator("file:"+filepath)) == null) Fatal("Can't build URL for " + filepath); try { player = Manager.createPlayer(medialocator); player.start(); player.addControllerListener(this); player.realize(); } catch(NoPlayerException noplayerexception) { System.out.println(noplayerexception); Fatal("Could not create player for " + medialocator); } } catch(MalformedURLException _ex) { Fatal("Invalid media file URL!"); } catch(IOException _ex) { Fatal("IO exception creating player for " + medialocator); } } public synchronized void controllerUpdate(ControllerEvent controllerevent) { if(player == null) return; if(controllerevent instanceof RealizeCompleteEvent) { if(progressBar != null) { panel.remove(progressBar); progressBar = null; } int i = 320; int j = 0; if(controlComponent == null && (controlComponent = player.getControlPanelComponent()) != null) { controlPanelHeight = controlComponent.getPreferredSize().height; panel.add(controlComponent); j += controlPanelHeight; } if( (visualComponent = player.getVisualComponent()) != null) { panel.add(visualComponent); Dimension dimension = visualComponent.getPreferredSize(); videoWidth = dimension.width; videoHeight = dimension.height; i = videoWidth; j += videoHeight; visualComponent.setBounds(0, 0, videoWidth, videoHeight); //setting video bounds } //panel.setBounds(0, 0, i, j); if(controlComponent != null) { controlComponent.setBounds(0, videoHeight, i, controlPanelHeight); controlComponent.invalidate(); } } else if(controllerevent instanceof CachingControlEvent) { if(player.getState() > 200) return; CachingControlEvent cachingcontrolevent = (CachingControlEvent)controllerevent; CachingControl cachingcontrol = cachingcontrolevent.getCachingControl(); if(progressBar == null && (progressBar = cachingcontrol.getControlComponent()) != null) { panel.add(progressBar); panel.setSize(progressBar.getPreferredSize()); validate(); } } else if(controllerevent instanceof EndOfMediaEvent) { player.setMediaTime(new Time(0L)); player.stop(); } else if(controllerevent instanceof ControllerErrorEvent) { player = null; Fatal(((ControllerErrorEvent)controllerevent).getMessage()); } else if(controllerevent instanceof ControllerClosedEvent) panel.removeAll(); } void Fatal(String s) { System.err.println("FATAL ERROR: " + s); throw new Error(s); } public void destroy() { if(player != null) player.close(); } public void start() { if(player != null) player.start(); } public void stop() { if(player != null) { player.stop(); player.deallocate(); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JButton getFile; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private java.awt.TextArea txtArea; private java.awt.List list; private javax.swing.JLabel l_show_img; private javax.swing.JPanel jPanel1; private javax.swing.JButton ExitButton; private javax.swing.JButton play; private javax.swing.JTextField FileNametxtField; public java.awt.Panel panel; // End of variables declaration//GEN-END:variables } /*Proxy2.java*/ import java.net.*; import java.io.*; import java.util.*; import javax.swing.*; import java.awt.*; public class Proxy2 extends javax.swing.JFrame implements Runnable{ ServerSocket ss; Socket client; Socket server; String fnf; Vector v=new Vector(); String filevalue; String count="1"; Hashtable fileDetails=new Hashtable(); Hashtable fileTime=new Hashtable(); Hashtable FileRecentAccTime=new Hashtable(); Hashtable clientDetails=new Hashtable(); String MainServer; long pcacheCapacity=4500000; int portNo=4000; /** Creates new form Proxy2 */ public Proxy2(String MS) { MainServer=MS; initComponents(); setBounds(50,50,440,550); String username=JOptionPane.showInputDialog(null,"Enter the user name"); String PWord=JOptionPane.showInputDialog(null,"Enter the user Password"); try{ server=new Socket(MainServer,5001); ObjectOutputStream outs = new ObjectOutputStream(server.getOutputStream()); Vector coll=new Vector(); coll.addElement("ProxyVerification"); coll.addElement("proxy2"); coll.addElement(username); coll.addElement(PWord); outs.writeObject(coll); outs.flush(); System.out.println("Send to Server data is"+coll); ObjectInputStream objis=new ObjectInputStream(server.getInputStream()); Vector vob=(Vector)objis.readObject(); System.out.println("Received output is:"+vob); String ch=(String)vob.elementAt(0); if(!ch.equals("Success")){ JOptionPane.showMessageDialog(null,"UNAUTHORIZED USER"); System.exit(0); } File fi=new File("C:/Content Processing/PServer2/PCache/"); File fary[]=fi.listFiles(); for(int i=0;i=pcacheCapacity){ System.out.println("\n\n\tThe Folder Capacity Size is increase\n\n"); Hashtable temp=new Hashtable(); Enumeration e1=fileDetails.keys(); while(e1.hasMoreElements()){ String FName=(String)e1.nextElement(); File ff=new File("C:/Content Processing/PServer2/PCache/"+FName); double FileCapacity=(double)ff.length(); System.out.println("The Capacity of the "+FName+"is:"+FileCapacity); String s1=(String)FileRecentAccTime.get(FName); StringTokenizer st1=new StringTokenizer(s1,":"); String s2=st1.nextToken(); String s3=st1.nextToken(); double RecentaccessTime=Double.parseDouble(s3); System.out.println("The Recent Access Time of the "+FName+"is:"+RecentaccessTime); s1=(String)fileTime.get(FName); st1=new StringTokenizer(s1,":"); s2=st1.nextToken(); s3=st1.nextToken(); double FileStoredTime=Double.parseDouble(s3); System.out.println("The Stored time of the "+FName+"is:"+FileStoredTime); Date d=new Date(); String ss1=String.valueOf(d.getHours()); String ss2=String.valueOf(d.getMinutes()); double CurrentTime=Double.parseDouble(ss2); System.out.println("The Current Time of the "+FName+"is:"+CurrentTime); double NumberofRequest=Double.parseDouble((String)fileDetails.get(FName)); System.out.println("The Number of the Request "+FName+"is:"+NumberofRequest); double data=RecentaccessTime-FileStoredTime; if(data==0){ data=1; } double data1=FileCapacity/data; if(Double.isInfinite(data1)){ data1=FileCapacity; } System.out.println("The First Data1 is:"+data1); double data2=data/NumberofRequest; System.out.println("The Second Data2 is:"+data2); double data6=CurrentTime-RecentaccessTime; if(data6==0){ data6=1; } double data3=data2/data6; System.out.println("The Second Data3 is:"+data3 ); if(Double.isInfinite(data3)){ System.out.println("Inside Not a Number"); data3=1; } else if(data3<1){ data3=data3; } else{ data3=1; } System.out.println("The data3 is:"+data3); double data4=data1*data3; System.out.println("The current File Popularity is:"+data4); temp.put(FName,(String.valueOf(data4))); System.out.println("The Temporary HashTable value is:"+temp); }//End OF While Loop e1=temp.keys(); String delfileName=null; double delfilevalue=0; while(e1.hasMoreElements()){ String FName=(String)e1.nextElement(); String value=(String)temp.get(FName); double value1=Double.parseDouble(value); if(delfileName==null){ delfileName=FName; delfilevalue=value1; } else{ if(value1

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑