/* 9P Protocol JAVA
 * P9P/msg/Twalk9P.java
 * Autor: Jaime Garzon (2006) jgarzon@gmail.com
 *
 * Esta clase es una implementacion del mensaje
 * Twalk, usado en el protocolo 9P de Plan9P, descrito
 * en la seccion 5 del manual de Plan 9: "Plan 9 File Protocol"
 *
 * Los metodos Write y Read, permiten escribir el valor
 * en un Buffer o leerlo de un array de bytes, respectivamente.
 * El metodo debug() es usado para depuracion de los mensajes.
*/

package P9P.msg;
import P9P.type.*;
import java.io.*;

public class Twalk9P extends tmsg9P {

   public int9P fid = new int9P();
   public int9P newfid = new int9P();
   public short9P nwname = new short9P();
   public String9P[] wname;

   public void write(ByteArrayOutputStream buf) {
      type.write(buf);
      tag.write(buf);
      fid.write(buf);
      newfid.write(buf);
      nwname.write(buf);
      wname = new String9P[nwname.value];
      for (int i=0;i<nwname.value;i++) {
         wname[i].write(buf);
      }
   }

   public void read (byte[] msg, int i) {
      type.read(msg,i);
      tag.read(msg,i+=1);
      fid.read(msg,i+=2);
      newfid.read(msg,i+=4);
      nwname.read(msg,i+=4);
      i+=2;
      wname = new String9P[nwname.value];
      for (int j=0;j<nwname.value;j++) {
         wname[j] = new String9P();
         wname[j].read(msg,i);
         i+=wname[j].len();
      }
   }

   public String toString() {
      String cad="";
      if (nwname.value>0)
         cad=wname[0].value;
      return  tagDb()+"Twalk fid:"+fid+
         "newfid:"+newfid+"nwname:"+nwname+cad;
   }
}