[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/rincon/tools/java/com/rincon/blackbook/bfilewrite BFileWriteEvents.class, NONE, 1.1 BFileWrite.class, NONE, 1.1 BFileWrite.java, NONE, 1.1 BFileWriteEvents.java, NONE, 1.1 BFileWriteArgParser.java, NONE, 1.1 BFileWriteArgParser.class, NONE, 1.1 BFileWriteCommands.class, NONE, 1.1 BFileWriteCommands.java, NONE, 1.1

dmm rincon at users.sourceforge.net
Thu Apr 20 16:02:54 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/rincon/tools/java/com/rincon/blackbook/bfilewrite
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15414/contrib/rincon/tools/java/com/rincon/blackbook/bfilewrite

Added Files:
	BFileWriteEvents.class BFileWrite.class BFileWrite.java 
	BFileWriteEvents.java BFileWriteArgParser.java 
	BFileWriteArgParser.class BFileWriteCommands.class 
	BFileWriteCommands.java 
Log Message:
Fixed the java/com directory, and added in two more packages:  Blackbook, and EavesLogger.

--- NEW FILE: BFileWriteEvents.class ---
Êþº¾
SourceFile

--- NEW FILE: BFileWrite.class ---
Êþº¾



	







get_length

get_result






set_length




SourceFile








--- NEW FILE: BFileWrite.java ---
package com.rincon.blackbook.bfilewrite;

/*
 * Copyright (c) 2004-2006 Rincon Research Corporation.  
 * All rights reserved.
 * 
 * Rincon Research will permit distribution and use by others subject to
 * the restrictions of a licensing agreement which contains (among other things)
 * the following restrictions:
 * 
 *  1. No credit will be taken for the Work of others.
 *  2. It will not be resold for a price in excess of reproduction and 
 *      distribution costs.
 *  3. Others are not restricted from copying it or using it except as 
 *      set forward in the licensing agreement.
 *  4. Commented source code of any modifications or additions will be 
 *      made available to Rincon Research on the same terms.
 *  5. This notice will remain intact and displayed prominently.
 * 
 * Copies of the complete licensing agreement may be obtained by contacting 
 * Rincon Research, 101 N. Wilmot, Suite 101, Tucson, AZ 85711.
 * 
 * There is no warranty with this product, either expressed or implied.  
 * Use at your own risk.  Rincon Research is not liable or responsible for 
 * damage or loss incurred or resulting from the use or misuse of this software.
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.rincon.blackbook.Commands;
import com.rincon.blackbook.Util;
import com.rincon.blackbook.messages.BlackbookConnectMsg;

import net.tinyos.message.Message;
import net.tinyos.message.MessageListener;
import net.tinyos.message.MoteIF;
import net.tinyos.util.Messenger;

public class BFileWrite implements BFileWriteCommands, MessageListener {

	/** Communication with the mote */
	private MoteIF comm = new MoteIF((Messenger) null);
	
	/** Command to send */
	private BlackbookConnectMsg command = new BlackbookConnectMsg();
	
	/** List of FileTransferEvents listeners */
	private static List listeners = new ArrayList();
	
	/** Current destination address */
	private int dest = Commands.TOS_BCAST_ADDR;
	
	/** Return amount for the getRemaining command */
	private long returnAmount;

	/**
	 * Constructor
	 *
	 */
	public BFileWrite() {
		comm.registerListener(new BlackbookConnectMsg(), this);
	}

	/**
	 * Set the destination address of the next send command
	 * @param destination
	 */
	public void setDestination(int destination) {
		dest = destination;
	}
	
	/**
	 * Send a message
	 * @param dest
	 * @param m
	 */
	private synchronized void send(Message m) {
		try {
			comm.send(dest, m);
		} catch (IOException e) {
			System.err.println("Couldn't contact the mote");
		}
	}

	/**
	 * Add a FileTransferEvents listener
	 * @param listener
	 */
	public void addListener(BFileWriteEvents listener) {
		if(!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}
	
	/**
	 * Remoe a FileTransferEvents listener
	 * @param listener
	 */
	public void removeListener(BFileWriteEvents listener) {
		listeners.remove(listener);
	}

	public synchronized void messageReceived(int to, Message m) {
		BlackbookConnectMsg inMsg = (BlackbookConnectMsg) m;
		
		switch(inMsg.get_cmd()) {
		case Commands.REPLY_BFILEWRITE_OPEN:
			for(Iterator it = listeners.iterator(); it.hasNext(); ) {
				((BFileWriteEvents) it.next()).opened(Util.dataToFilename(inMsg.get_data()), inMsg.get_length(), inMsg.get_result() == Commands.SUCCESS);
			}
			break;
			
		case Commands.REPLY_BFILEWRITE_CLOSE:
			for(Iterator it = listeners.iterator(); it.hasNext(); ) {
				((BFileWriteEvents) it.next()).closed(inMsg.get_result() == Commands.SUCCESS);
			}
			break;
			
		case Commands.REPLY_BFILEWRITE_SAVE:
			for(Iterator it = listeners.iterator(); it.hasNext(); ) {
				((BFileWriteEvents) it.next()).saved(inMsg.get_result() == Commands.SUCCESS);
			}
			break;
			
		case Commands.REPLY_BFILEWRITE_APPEND:
			for(Iterator it = listeners.iterator(); it.hasNext(); ) {
				((BFileWriteEvents) it.next()).appended((int) inMsg.get_length(), inMsg.get_result() == Commands.SUCCESS);
			}
			break;
			
		case Commands.REPLY_BFILEWRITE_REMAINING:
			returnAmount = inMsg.get_length();
			notify();
			break;
		
			
			
		case Commands.ERROR_BFILEWRITE_OPEN:
		case Commands.ERROR_BFILEWRITE_CLOSE:
		case Commands.ERROR_BFILEWRITE_SAVE:
		case Commands.ERROR_BFILEWRITE_APPEND:
		case Commands.ERROR_BFILEWRITE_REMAINING:
			System.err.println("Command immediately failed");
			System.exit(1);
			
		default:
		
		}
		
	}
	
	
	
	public void open(String fileName, long minimumSize) {
		command.set_data(Util.filenameToData(fileName));
		command.set_cmd(Commands.CMD_BFILEWRITE_OPEN);
		command.set_length(minimumSize);
		send(command);
	}

	public void close() {
		command.set_cmd(Commands.CMD_BFILEWRITE_CLOSE);
		send(command);
	}

	public void save() {
		command.set_cmd(Commands.CMD_BFILEWRITE_SAVE);
		send(command);
	}

	public void append(short[] data, int amount) {
		command.set_data(data);
		command.set_length(amount);
		command.set_cmd(Commands.CMD_BFILEWRITE_APPEND);
		send(command);
	}

	public synchronized long getRemaining() {
		command.set_cmd(Commands.CMD_BFILEWRITE_REMAINING);
		send(command);
		try {
			wait(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnAmount;
	}


}

--- NEW FILE: BFileWriteEvents.java ---
package com.rincon.blackbook.bfilewrite;

/*
 * Copyright (c) 2004-2006 Rincon Research Corporation.  
 * All rights reserved.
 * 
 * Rincon Research will permit distribution and use by others subject to
 * the restrictions of a licensing agreement which contains (among other things)
 * the following restrictions:
 * 
 *  1. No credit will be taken for the Work of others.
 *  2. It will not be resold for a price in excess of reproduction and 
 *      distribution costs.
 *  3. Others are not restricted from copying it or using it except as 
 *      set forward in the licensing agreement.
 *  4. Commented source code of any modifications or additions will be 
 *      made available to Rincon Research on the same terms.
 *  5. This notice will remain intact and displayed prominently.
 * 
 * Copies of the complete licensing agreement may be obtained by contacting 
 * Rincon Research, 101 N. Wilmot, Suite 101, Tucson, AZ 85711.
 * 
 * There is no warranty with this product, either expressed or implied.  
 * Use at your own risk.  Rincon Research is not liable or responsible for 
 * damage or loss incurred or resulting from the use or misuse of this software.
 */

public interface BFileWriteEvents {


	/**
	 * Signaled when a file has been opened, with the results
	 * @param fileName - the name of the opened write file
	 * @param len - The total reserved length of the file
	 * @param result - SUCCSES if the file was opened successfully
	 */
	public void opened(String fileName, long len, boolean result);

	/** 
	 * Signaled when the opened file has been closed
	 * @param result - SUCCESS if the file was closed properly
	 */
	public void closed(boolean result);

	/**
	 * Signaled when this file has been saved.
	 * This does not require the save() command to be called
	 * before being signaled - this would happen if another
	 * file was open for writing and that file was saved, but
	 * the behavior of the checkpoint file required all files
	 * on the system to be saved as well.
	 * @param fileName - name of the open write file that was saved
	 * @param result - SUCCESS if the file was saved successfully
	 */
	public void saved(boolean result);

	/**
	 * Signaled when data is written to flash. On some media,
	 * the data is not guaranteed to be written to non-volatile memory
	 * until save() or close() is called.
	 * @param fileName
	 * @param data The buffer of data appended to flash
	 * @param amountWritten The amount written to flash
	 * @param result
	 */
	public void appended(int amountWritten, boolean result);

}

--- NEW FILE: BFileWriteArgParser.java ---
package com.rincon.blackbook.bfilewrite;

/*
 * Copyright (c) 2004-2006 Rincon Research Corporation.  
 * All rights reserved.
 * 
 * Rincon Research will permit distribution and use by others subject to
 * the restrictions of a licensing agreement which contains (among other things)
 * the following restrictions:
 * 
 *  1. No credit will be taken for the Work of others.
 *  2. It will not be resold for a price in excess of reproduction and 
 *      distribution costs.
 *  3. Others are not restricted from copying it or using it except as 
 *      set forward in the licensing agreement.
 *  4. Commented source code of any modifications or additions will be 
 *      made available to Rincon Research on the same terms.
 *  5. This notice will remain intact and displayed prominently.
 * 
 * Copies of the complete licensing agreement may be obtained by contacting 
 * Rincon Research, 101 N. Wilmot, Suite 101, Tucson, AZ 85711.
 * 
 * There is no warranty with this product, either expressed or implied.  
 * Use at your own risk.  Rincon Research is not liable or responsible for 
 * damage or loss incurred or resulting from the use or misuse of this software.
 */

import com.rincon.blackbook.Util;
import com.rincon.blackbook.messages.BlackbookConnectMsg;

public class BFileWriteArgParser implements BFileWriteEvents {

	/** Transceiver communication with the mote */
	private BFileWrite bFileWrite;
	
	public BFileWriteArgParser(String[] args) {
		bFileWrite = new BFileWrite();
		bFileWrite.addListener(this);
		
		if(args.length < 1) {
			reportError("Not enough arguments");
		}
		
		if(args[0].toLowerCase().matches("-open")) {
			if (args.length > 2) {
				bFileWrite.open(args[1], Util.parseLong(args[2]));
			} else {
				reportError("Missing parameter(s)");
			}
			
		} else if(args[0].toLowerCase().matches("-close")) {
			bFileWrite.close();
			
		} else if(args[0].toLowerCase().matches("-save")) {
			bFileWrite.save();
			
		} else if(args[0].toLowerCase().matches("-append")) {
			// We can append any length of data we want,
			// but since our TOS_Msg can only hold so many bytes,
			// we'll stick with that maximum.  And because we
			// can only type characters into the command line, we'll
			// stick with that too.  Keep in mind, for testing purposes,
			// it would also be easy to have an argument to this command
			// line function to say how many bytes to append.
			if (args.length > 1) {
				int length = args[1].length();
				if(length >= BlackbookConnectMsg.totalSize_data()) {
					length = BlackbookConnectMsg.totalSize_data();
				}
				bFileWrite.append(Util.stringToData(args[1]), length);
				
			} else {
				reportError("Missing parameter(s)");
			}
			
		} else if(args[0].toLowerCase().matches("-getremaining")) {
			System.out.println(bFileWrite.getRemaining() + " bytes available for writing");
			System.exit(0);
			
		} else {
			System.err.println("Unknown argument: " + args[0]);
			System.err.println(getUsage());
			System.exit(1);
		}
	}
	
	private void reportError(String error) {
		System.err.println(error);
		System.err.println(getUsage());
		System.exit(1);
	}
	
	public static String getUsage() {
		String usage = "";
		usage += "  BFileWrite\n";
		usage += "\t-open <filename>\n";
		usage += "\t-close\n";
		usage += "\t-save\n";
		usage += "\t-append <written data>\n";
		usage += "\t-getRemaining\n";
		return usage;
	}
	

	/***************** BFileWrite Events ****************/
	public void opened(String fileName, long len, boolean result) {
		System.out.print("BFileWrite opened ");
		if(result) {
			System.out.println("SUCCESS: " + fileName);
			System.out.println("\n" + len + " bytes");
		} else {
			System.out.println("FAIL");
		}
		System.exit(0);
	}


	public void saved(boolean result) {
		System.out.print("BFileWrite save ");
		if(result) {
			System.out.println("SUCCESS");
		} else {
			System.out.println("FAIL");
		}
		System.exit(0);
	}


	public void appended(int amountWritten, boolean result) {
		System.out.print("BFileWrite append ");
		
		if(result) {
			System.out.println("SUCCESS: " + amountWritten + " bytes");
		} else {
			System.out.println("FAIL");
		}
		System.exit(0);
	}

	public void closed(boolean result) {
		System.out.print("Closed ");
		if(result) {
			System.out.println("SUCCESS");
		} else {
			System.out.println("FAIL");
		}
		System.exit(0);
	}
	
}

--- NEW FILE: BFileWriteArgParser.class ---
Êþº¾
bFileWrite

































SourceFile

--- NEW FILE: BFileWriteCommands.class ---
Êþº¾
SourceFile

--- NEW FILE: BFileWriteCommands.java ---
package com.rincon.blackbook.bfilewrite;

/*
 * Copyright (c) 2004-2006 Rincon Research Corporation.  
 * All rights reserved.
 * 
 * Rincon Research will permit distribution and use by others subject to
 * the restrictions of a licensing agreement which contains (among other things)
 * the following restrictions:
 * 
 *  1. No credit will be taken for the Work of others.
 *  2. It will not be resold for a price in excess of reproduction and 
 *      distribution costs.
 *  3. Others are not restricted from copying it or using it except as 
 *      set forward in the licensing agreement.
 *  4. Commented source code of any modifications or additions will be 
 *      made available to Rincon Research on the same terms.
 *  5. This notice will remain intact and displayed prominently.
 * 
 * Copies of the complete licensing agreement may be obtained by contacting 
 * Rincon Research, 101 N. Wilmot, Suite 101, Tucson, AZ 85711.
 * 
 * There is no warranty with this product, either expressed or implied.  
 * Use at your own risk.  Rincon Research is not liable or responsible for 
 * damage or loss incurred or resulting from the use or misuse of this software.
 */

public interface BFileWriteCommands {

	/**
	 * Open a file for writing. 
	 * 
	 * The reservedBytes must be specified to ensure enough memory
	 * exists in flash for the operation. This does not necessarily
	 * reflect how many bytes must be written - if less bytes
	 * are actually written, the physical file size is automatically
	 * adjusted to the nearest page boundary when the file is finalized.
	 * For example, to create a log file that will be able to hold a 
	 * maximum of 64000 bytes, specify the reserveBytes to be 64000. 
	 * Then if you only log 32000 bytes and close the file, the 
	 * physical file size on flash will be around 32k on flash 
	 * instead of 64k.
	 * 
	 * If the file does already exist on flash, 
	 * giving a reserveBytes value that is less than the existing
	 * file size will not affect the original file size.This may give you only
	 * a few bytes of free space to write to, but only a maximum of a page
	 * of flash.You can use BFileDir.getDataLength(String fileName) to 
	 * get the length of an existing file. So say you have
	 * a file already written and closed, and you expect
	 * to write 0x1000 more bytes to it:
	 *
	 * call BFileWrite.open("myFile", 
	 * call BFileDir.getDataLength("myFile") + 0x1000);
	 *
	 * @param fileName - name of the file to write to
	 * @param minimumSize The minimum requested amount of total space
	 *to reserve in the file.The physical size on the
	 *flash may be more by one page of flash.
	 */ 
	public void open(String fileName, long minimumSize);

	/**
	 * Close any currently opened write file.
	 */
	public void close();

	/**
	 * Save the current state of the file, guaranteeing the next time
	 * we experience a catastrophic failure, we will at least be able to
	 * recover data from the open write file up to the point
	 * where save was called.
	 *
	 * If data is simply being logged for a long time, use save() 
	 * periodically but probably more infrequently.
	 *
	 * @return SUCCESS if the currently open file will be saved.
	 */
	public void save();

	/**
	 * Append the specified amount of data from a given buffer
	 * to the open write file.
	 *
	 * @param data - the buffer of data to append
	 * @param amount - the amount of data in the buffer to write.
	 * @return SUCCESS if the data will be written, FAIL if there
	 * is no open file to write to.
	 */ 
	public void append(short[] data, int amount);

	/**
	 * Obtain the remaining bytes available to be written in this file
	 * This is the total reserved length minus your current 
	 * write position
	 * @return the remaining length of the file.
	 */
	public long getRemaining();


}



More information about the Tinyos-contrib-commits mailing list