lpr-b:remotepacket
no way to compare when less than two revisions
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
— | lpr-b:remotepacket [19/11/2007 alle 15:16 (17 anni fa)] (versione attuale) – creata Sonia Campa | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
+ | <code java> | ||
+ | package nfs; | ||
+ | import java.io.BufferedReader; | ||
+ | import java.io.ByteArrayInputStream; | ||
+ | import java.io.ByteArrayOutputStream; | ||
+ | import java.io.IOException; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.io.PrintStream; | ||
+ | import java.net.DatagramPacket; | ||
+ | |||
+ | public class RemotePacket { | ||
+ | |||
+ | public final static int MAXPACKET = 1024; | ||
+ | |||
+ | String op = null; | ||
+ | String [] args = null; | ||
+ | DatagramPacket dp = null; | ||
+ | |||
+ | public static DatagramPacket RemotePacket(String op, String ... params) { | ||
+ | /* creates a datagram packet given a number of parameters | ||
+ | */ | ||
+ | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
+ | PrintStream ps = new PrintStream(baos); | ||
+ | // print the operation type | ||
+ | ps.println(op); | ||
+ | // print the lenght of byte involved | ||
+ | ps.println(params.length); | ||
+ | // print the parameters | ||
+ | for(int i=0; i< | ||
+ | ps.println(params[i]); | ||
+ | ps.println(" | ||
+ | ps.flush(); | ||
+ | byte [] buffer = baos.toByteArray(); | ||
+ | DatagramPacket dp1 = new DatagramPacket(buffer, | ||
+ | return dp1; | ||
+ | } | ||
+ | |||
+ | public RemotePacket(DatagramPacket dp) { | ||
+ | /* building of a remote packet from a datagram packet by separating | ||
+ | * - the operation requested (READ, WRITE, OPEN, CLOSE) | ||
+ | * - the arguments related to the operation (filenames, handlers, etc.) | ||
+ | * */ | ||
+ | try { | ||
+ | this.dp = dp; | ||
+ | byte [] buffer = dp.getData(); | ||
+ | ByteArrayInputStream bais = new ByteArrayInputStream(buffer); | ||
+ | BufferedReader br = new BufferedReader(new InputStreamReader(bais)); | ||
+ | / | ||
+ | this.op = br.readLine(); | ||
+ | int numargs = Integer.parseInt(br.readLine()); | ||
+ | if(numargs> | ||
+ | this.args = new String[numargs]; | ||
+ | for(int i=0; i< | ||
+ | this.args[i] = br.readLine(); | ||
+ | } | ||
+ | } | ||
+ | } catch(IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | public String getOp() { | ||
+ | return op; | ||
+ | } | ||
+ | |||
+ | public String [] getArgs() { | ||
+ | return args; | ||
+ | } | ||
+ | |||
+ | public String toString() { | ||
+ | String s = op+"::"; | ||
+ | for(int i=0; | ||
+ | s = s+" " | ||
+ | return s; | ||
+ | } | ||
+ | } | ||
+ | </ |
lpr-b/remotepacket.txt · Ultima modifica: 19/11/2007 alle 15:16 (17 anni fa) da Sonia Campa