Strumenti Utente

Strumenti Sito


lpr-b-2007-2008:ilserver
package nfs;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.HashMap;
 
public class NfsServer {
 
	public static final int port = 23232; 
	public static int key=0;
	private HashMap<String,StreamRef> handles = null; 
 
	public NfsServer() {
		handles = new HashMap<String,StreamRef>();
	}
 
	public DatagramPacket open(RemotePacket rp) {
		DatagramPacket dp = null; 
		int mapkey=key++;
		String strkey=null;
		boolean err=false;
		InputStream is=null;
		OutputStream os=null;
 
		try {
			is = new FileInputStream(rp.getArgs()[0]);
			os = new FileOutputStream(rp.getArgs()[0], true); // in appending mode
		} catch (FileNotFoundException e) {
			err=true;
		}
		// a fresh handle is needed for avoiding double entries
		strkey = new String(rp.getArgs()[0]+"_"+mapkey);
		if (err){
			dp = RemotePacket.RemotePacket("ERROR","FILENOTFOUNDEXCEPTION",strkey);
		}else{
			handles.put(strkey, new StreamRef(is,os));
			dp = RemotePacket.RemotePacket("HANDLE", strkey);
		}
		dp.setAddress(rp.dp.getAddress());
		dp.setPort(rp.dp.getPort());
		return dp;
	}
 
	public DatagramPacket read(RemotePacket rp) {
		DatagramPacket dp = null; 
		String handleref = rp.getArgs()[0];
		System.out.println("handle:"+handleref);
		StreamRef  sr = handles.get(handleref);
		if(sr == null) {
			dp = RemotePacket.RemotePacket("ERROR", "HANDLE NOT IN CACHE", handleref);
		} else {			
			InputStream is = sr.getIn();
			int len = Integer.parseInt(rp.getArgs()[1]);	
			byte [] buffer = new byte[len];
			try {
				int letti = is.read(buffer,0,len);
				if (letti==-1)
					// the file is empty
					letti=0;
				byte [] buf = new byte[letti];
				for(int i=0; i<letti; i++)
					buf[i] = buffer[i];
				dp = new DatagramPacket(buf,letti);
			} catch (IOException e) {
				e.printStackTrace();
				dp = RemotePacket.RemotePacket("ERROR", "IOEXCEPTION", handleref);
			}
		}
		dp.setAddress(rp.dp.getAddress());
		dp.setPort(rp.dp.getPort());
		return dp;
	}
 
	public DatagramPacket close(RemotePacket rp) {
		DatagramPacket dp = null; 
		StreamRef sr = handles.get(rp.getArgs()[0]);
		if (sr==null){
			dp = RemotePacket.RemotePacket("ERROR", "HANDLE NOT IN CACHE", rp.getArgs()[0]);
		} else {
			InputStream is = sr.getIn();
			OutputStream os = sr.getOut();
			try {
				is.close();
				os.close();
				handles.remove(rp.getArgs()[0]);
				dp = RemotePacket.RemotePacket("OK");
			} catch (IOException e) {
				dp = RemotePacket.RemotePacket("ERROR", "IOEXCEPTION", rp.getArgs()[0]);
			}
 
		}
		dp.setAddress(rp.dp.getAddress());
		dp.setPort(rp.dp.getPort());
		return dp;
	}
 
 
	private DatagramPacket write(RemotePacket rp) {
		// the rp.getArgs() contains the handle and the content to be written
		DatagramPacket dp = null; 
		String handleref = rp.getArgs()[0];
		StreamRef sr = handles.get(handleref);
		if (sr==null) {
			dp = RemotePacket.RemotePacket("ERROR", "HANDLE NOT IN CACHE", handleref);
		} else {
			OutputStream os = sr.getOut();
			String msg_err=null;
			byte [] buffer = (rp.getArgs()[1]).getBytes();
			try {
				os.write(buffer);
				os.flush();
				os.close();
			} catch (IOException e) {
				msg_err = e.toString();
			}
			if (msg_err!=null)
				buffer = ("ERROR \n"+"WRITE \n"+handleref+" \n"+msg_err).getBytes();
			else
				buffer = "OK \n".getBytes();
 
			dp = new DatagramPacket(buffer,buffer.length);
		}
		dp.setAddress(rp.dp.getAddress());
		dp.setPort(rp.dp.getPort());
		return dp;
	}
 
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
 
		try {
			// create the server socket
			DatagramSocket ds = new DatagramSocket(port);
			NfsServer processor = new NfsServer();
			// process requests
			while (true) {
				DatagramPacket dp = new DatagramPacket(new byte[UDPServerComm.LIMIT], UDPServerComm.LIMIT);
				System.out.println("-----------------------------\n[Server] in attesa...");
				ds.receive(dp);
				// process the request
 
				RemotePacket rp = new RemotePacket(dp);
				System.out.println("[Server] "+rp.toString()+" da IP:"+dp.getAddress());
 
				if(rp.getOp().equals("OPEN")) {
					ds.send(processor.open(rp));
				}
				if(rp.getOp().equals("CLOSE")) {
					ds.send(processor.close(rp));
				}
				if(rp.getOp().equals("READ")) {
					ds.send(processor.read(rp));
				}
				// go processing next request
				if(rp.getOp().equals("WRITE")) {
					ds.send(processor.write(rp));
				}
				System.out.println(rp.getOp()+" terminata...");
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
}
lpr-b-2007-2008/ilserver.txt · Ultima modifica: 19/09/2008 alle 14:08 (16 anni fa) da 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki