Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C++

A Namespace Extension Toolkit

Rate me:
Please Sign up or sign in to vote.
4.78/5 (10 votes)
21 Mar 2006CPOL12 min read 84.5K   1.7K   42  
This article shows you how to build your own Windows Explorer interfaces to custom data.
/*
  Galaxy Filesystem Toolkit
  Copyright (C) 2005 Chad Yoshikawa
  http://www.ececs.uc.edu/~yoshikco
  yoshikco@ececs.uc.edu

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package galaxy.test;

import galaxy.nfs.Util;
import galaxy.java.IOLibrary;
import galaxy.java.CommandLine;
import java.io.*;
import java.net.*;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.*;


public class SocketTest {
    public static Logger logger = Logger.getLogger("GalaxyTest");

    public static void main(String args[]) {
        try {
            //BasicConfigurator.configure(new FileAppender(new SimpleLayout(), "log4j.out"));
            BasicConfigurator.configure();
        } catch (Exception e) {
            System.err.println("Could not configure logger");
            BasicConfigurator.configure();
        }

        Properties props = CommandLine.Parse(args);
        if (props.getProperty("log","off").equals("on")) {
            logger.setLevel(Level.ALL);
        } else if (props.getProperty("log","off").equals("info")) {
            logger.setLevel(Level.INFO);
        } else if (props.getProperty("log","off").equals("warn")) {
            logger.setLevel(Level.WARN);
        } else {
            logger.setLevel(Level.FATAL);
        }

        // give us a port, a size, and a number of times to read data
        int port = Integer.parseInt(props.getProperty("port","8053"));
        logger.info("Starting server on port "+port+" and waiting...");
        StartTest(port);
    }

    public static void StartTest(int port) {
        ServerSocket mySocket = null;
        try {
            InetAddress localAddr = InetAddress.getByName("127.0.0.1");
            mySocket = new ServerSocket(port,0,null /*null means any i/f on a multihomed machine*/); //,0/*backlog*/,localAddr);

            while (true) {
                try {
                    // how long does this take?
                    Socket client = mySocket.accept();
                    client.setTcpNoDelay(true);

                    logger.info("Socket receive buffer size is "+client.getReceiveBufferSize());
                    logger.info("Socket send buffer size is "+client.getSendBufferSize());

                    // XXX For now - get rid of these two 
                    // client.setSoTimeout(0);
                    // client.setKeepAlive(true);
                    logger.debug("+++ Gateway: run: Accepted client socket #");
                    HandleClient(client);
                } catch (Exception e) {
                    logger.debug("Error accepting client socket "+e);
                }
            }

        }	catch (Exception e) {
            logger.warn("SocketTest: Exception: ", e);
        } finally {
            try {
                if (mySocket!=null) 
                    mySocket.close(); // close the server socket...
            } catch (Exception ex) {
                ; // ignore it..
            }
        }
    } // end StartTest

    public static void HandleClient(Socket myClient) {
        double time_elapsed=0;
        int bytes_count = 0;

        try {
            DataInputStream myReader = new DataInputStream(myClient.getInputStream());
            DataOutputStream myWriter = new DataOutputStream(myClient.getOutputStream());

            //Sleep for 10 secs while
//             try {
//                 Thread.currentThread().sleep(10000);
//             } catch (InterruptedException iex) {
//                 ;
//             }
            int n = IOLibrary.ReadInt(myReader);
            for (int i=0;i<n;i++) {
                double start = Util.TraceTime("WriteCommand: Starting read of data buffer from network ");
                byte[] buffer = IOLibrary.ReadBuffer(myReader);
                double stop = Util.TraceTime("WriteCommand: Finished read of "+buffer.length+" bytes from network ");
                // if (!VerifyBuffer(buffer)) {
                //                     System.err.println("Invalid buffer read!!!");
                //                 }
                time_elapsed+=(stop-start);
                bytes_count+=buffer.length;
                IOLibrary.WriteInt(myWriter,0xdeadbeef); // write an ok signal
            }
            // Now we are done
            Util.ClearTrace();
            //Util.PrintTrace();
            logger.info("Received "+bytes_count+" total bytes and "+bytes_count/n+" bytes per message");
            logger.info("Average Latency "+(double)time_elapsed/(double)n);
            logger.info("Average BW "+(bytes_count/1000)/time_elapsed+" mb/s");
        } catch (Exception ex) {
            logger.warn("Error - "+ex);
            ex.printStackTrace();
        }
    }

    public static boolean VerifyBuffer(byte[] buffer) {
        for (int i=0;i<buffer.length;i++) {
            if (buffer[i]!=(i%128)) {
                System.err.println("Error reading value "+i+" which is "+buffer[i]);
                return false;
            }
        }
        return true;
    }

} // end class
 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions