# HG changeset patch # User kazz # Date 1328691760 -32400 # Node ID 2a64c13e743aa14e9fa6eca661d4ad4f78e96ee7 # Parent fce61ee25d2040242693665c890f7e219d1058dd# Parent 4150c16e93fd3a6775d924af02908e22ffb0f5b1 merge diff -r fce61ee25d20 -r 2a64c13e743a .classpath --- a/.classpath Wed Feb 08 18:02:00 2012 +0900 +++ b/.classpath Wed Feb 08 18:02:40 2012 +0900 @@ -2,6 +2,7 @@ - + + diff -r fce61ee25d20 -r 2a64c13e743a StartFDLindaServ2.py --- a/StartFDLindaServ2.py Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -#! /usr/bin/env python -"""start process -""" -import pexpect -import re -import time -import sys -import os - -def main(base_port, ldserv_num): - - if not os.environ.has_key('LDSERV'): - ldserv = "java -classpath FedLinda.jar fdl.FDLindaServ" - else: - ldserv = os.environ['LDSERV'] - - for num in range(0,ldserv_num): - cmd = "".join([ldserv + ' -p ' + str(base_port+num) + ' &']) - os.system(cmd) - print 'start :', cmd - - -if __name__ == "__main__": - - if (len(sys.argv) != 3): - print 'Usage : %s ' % sys.argv[0] - sys.exit(1) - - main(int(sys.argv[1]), int(sys.argv[2])) diff -r fce61ee25d20 -r 2a64c13e743a StopProcess.py --- a/StopProcess.py Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -#! /usr/bin/env python -"""find and kill processes -""" -import pexpect -import re -import sys -import os - -def main(proc): - child = pexpect.spawn('ps aucxwww') - child.setwinsize(100,300) - child.expect(pexpect.EOF) - - pslist = child.before.splitlines() - # separate string by space - fmt = re.split(' +', pslist[0]) - - if fmt[-1] == '': - fmt.pop() - if fmt[0] == '': - fmt.pop(0) - lenfmt = len(fmt) - - # indexing, key is format of output of ps command, value is number of list - index = dict([(k,v) for (k,v) in zip(fmt, range(0,lenfmt))]) - - # find ".*.*" in 'COMMAND' part - pars = re.compile(".*"+proc+".*") - - for l in pslist[1:]: - elm = re.split(' +',l ,lenfmt) - cmd = elm[index['COMMAND']] - result = pars.match(cmd) - - if (result is not None): - os.system('kill -KILL ' + elm[index['PID']]) - print 'stop', elm[index['PID']], ':', cmd - - -if __name__== "__main__": - if (len(sys.argv) != 2): - print "Usage : %s " % sys.argv[0] - sys.exit(1) - - main(sys.argv[1]) - diff -r fce61ee25d20 -r 2a64c13e743a build.xml --- a/build.xml Wed Feb 08 18:02:00 2012 +0900 +++ b/build.xml Wed Feb 08 18:02:40 2012 +0900 @@ -18,7 +18,9 @@ - + + + @@ -27,6 +29,10 @@ + + + + diff -r fce61ee25d20 -r 2a64c13e743a document/License.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/document/License.txt Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,10 @@ +Entire source code 's license is under + + GNU Lesser General Public License v2 (LGPLv2) + + http://www.gnu.org/licenses/lgpl-2.1.html + +Shinji KONO + kono@ie.u-ryukyu.ac.jp + Sun Oct 2 15:28:13 JST 2011 + diff -r fce61ee25d20 -r 2a64c13e743a lib/junit.jar Binary file lib/junit.jar has changed diff -r fce61ee25d20 -r 2a64c13e743a lib/protobuf-java-2.3.0.jar Binary file lib/protobuf-java-2.3.0.jar has changed diff -r fce61ee25d20 -r 2a64c13e743a pexpect.py --- a/pexpect.py Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1203 +0,0 @@ -"""Pexpect is a Python module for spawning child applications; -controlling them; and responding to expected patterns in their output. -Pexpect can be used for automating interactive applications such as -ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts -for duplicating software package installations on different servers. -It can be used for automated software testing. Pexpect is in the spirit of -Don Libes' Expect, but Pexpect is pure Python. Other Expect-like -modules for Python require TCL and Expect or require C extensions to -be compiled. Pexpect does not use C, Expect, or TCL extensions. It -should work on any platform that supports the standard Python pty -module. The Pexpect interface focuses on ease of use so that simple -tasks are easy. - -There are two main interfaces to Pexpect. You can call the function: - pexpect.run() -to execute a command and return the output. -Do no use this on interactive commands that expect input. -This is a handy replacment for os.system(). -The more useful interface is the class: - pexpect.spawn() -This creates a spawn instance. This will start the command that you specify. -You can then interact with the child command through the spawn instance. -Most commands, including ssh, cannot tell that they are being run inside -of a script. This works even for commands that ask for passwords or -other input outside of the normal stdio streams. - -Pexpect is Open Source, Free, and all that good stuff. -License: Python Software Foundation License - http://www.opensource.org/licenses/PythonSoftFoundation.html - -Noah Spurrier -Richard Holden -Marco Molteni -Kimberley Burchett -Robert Stone -Mike Snitzer -Marti Raudsepp -Matt -Hartmut Goebel -Chad Schroeder -Erick Tryzelaar -Dave Kirby -Ids vander Molen -George Todd -Noel Taylor -Nicolas D. Cesar -(Let me know if I forgot anyone.) - -$Revision: 1.1.1.1 $ -$Date: 2006/04/01 06:03:29 $ -""" - -try: - import os, sys, time - import select - import string - import re - import struct - import resource - import types - import pty - import tty - import termios - import fcntl - import errno - import traceback - import signal -except ImportError, e: - raise ImportError (str(e) + """ -A critical module was not found. Probably this operating system does not support it. -Pexpect is intended for UNIX-like operating systems.""") - -__version__ = '2.0' -__revision__ = '$Revision: 1.1.1.1 $' -__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which', 'split_command_line', - '__version__', '__revision__'] - -# Exception classes used by this module. -class ExceptionPexpect(Exception): - """Base class for all exceptions raised by this module. - """ - def __init__(self, value): - self.value = value - def __str__(self): - return str(self.value) - def get_trace(self): - """This returns an abbreviated stack trace with lines that only concern the caller. - In other words, the stack trace inside the Pexpect module is not included. - """ - tblist = traceback.extract_tb(sys.exc_info()[2]) - tblist = filter(self.__filter_not_pexpect, tblist) - tblist = traceback.format_list(tblist) - return ''.join(tblist) - def __filter_not_pexpect(self, trace_list_item): - if trace_list_item[0].find('pexpect.py') == -1: - return 1 - else: - return 0 -class EOF(ExceptionPexpect): - """Raised when EOF is read from a child. - """ -class TIMEOUT(ExceptionPexpect): - """Raised when a read time exceeds the timeout. - """ -##class TIMEOUT_PATTERN(TIMEOUT): -## """Raised when the pattern match time exceeds the timeout. -## This is different than a read TIMEOUT because the child process may -## give output, thus never give a TIMEOUT, but the output -## may never match a pattern. -## """ -##class MAXBUFFER(ExceptionPexpect): -## """Raised when a scan buffer fills before matching an expected pattern.""" - -def run (command, timeout=-1, withexitstatus=0, events=None, extra_args=None): - """This function runs the given command; waits for it to finish; - then returns all output as a string. STDERR is included in output. - If the full path to the command is not given then the path is searched. - - Note that lines are terminated by CR/LF (\\r\\n) combination - even on UNIX-like systems because this is the standard for pseudo ttys. - If you set withexitstatus to true, then run will return a tuple of - (command_output, exitstatus). If withexitstatus is false then this - returns just command_output. - - Examples: - Start the apache daemon on the local machine: - from pexpect import * - run ("/usr/local/apache/bin/apachectl start") - Check in a file using SVN: - from pexpect import * - run ("svn ci -m 'automatic commit' my_file.py") - Run a command and capture exit status: - from pexpect import * - (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1) - - Tricky Examples: - The following will run SSH and execute 'ls -l' on the remote machine. - The password 'secret' will be sent if the '(?i)password' pattern is ever seen. - run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'}) - - This will start mencoder to rip a video from DVD. This will also display - progress ticks every 5 seconds as it runs. - from pexpect import * - def print_ticks(d): - print d['event_count'], - run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5) - - The 'events' argument should be a dictionary of patterns and responses. - Whenever one of the patterns is seen in the command out - run() will send the associated response string. Note that you should - put newlines in your string if Enter is necessary. - The responses may also contain callback functions. - Any callback is function that takes a dictionary as an argument. - The dictionary contains all the locals from the run() function, so - you can access the child spawn object or any other variable defined - in run() (event_count, child, and extra_args are the most useful). - A callback may return True to stop the current run process otherwise - run() continues until the next event. - A callback may also return a string which will be sent to the child. - 'extra_args' is not used by directly run(). It provides a way to pass data to - a callback function through run() through the locals dictionary passed to a callback. - """ - if timeout == -1: - child = spawn(command, maxread=2000) - else: - child = spawn(command, timeout=timeout, maxread=2000) - if events is not None: - patterns = events.keys() - responses = events.values() - else: - patterns=None # We assume that EOF or TIMEOUT will save us. - responses=None - child_result_list = [] - event_count = 0 - while 1: - try: - index = child.expect (patterns) - if type(child.after) is types.StringType: - child_result_list.append(child.before + child.after) - else: # child.after may have been a TIMEOUT or EOF, so don't cat those. - child_result_list.append(child.before) - if type(responses[index]) is types.StringType: - child.send(responses[index]) - elif type(responses[index]) is types.FunctionType: - callback_result = responses[index](locals()) - sys.stdout.flush() - if type(callback_result) is types.StringType: - child.send(callback_result) - elif callback_result: - break - else: - raise TypeError ('The callback must be a string or function type.') - event_count = event_count + 1 - except TIMEOUT, e: - child_result_list.append(child.before) - break - except EOF, e: - child_result_list.append(child.before) - break - child_result = ''.join(child_result_list) - if withexitstatus: - child.close() - return (child_result, child.exitstatus) - else: - return child_result - -class spawn: - """This is the main class interface for Pexpect. - Use this class to start and control child applications. - """ - - def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None): - """This is the constructor. The command parameter may be a string - that includes a command and any arguments to the command. For example: - p = pexpect.spawn ('/usr/bin/ftp') - p = pexpect.spawn ('/usr/bin/ssh user@example.com') - p = pexpect.spawn ('ls -latr /tmp') - You may also construct it with a list of arguments like so: - p = pexpect.spawn ('/usr/bin/ftp', []) - p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com']) - p = pexpect.spawn ('ls', ['-latr', '/tmp']) - After this the child application will be created and - will be ready to talk to. For normal use, see expect() and - send() and sendline(). - - The maxread attribute sets the read buffer size. - This is maximum number of bytes that Pexpect will try to read from a TTY at one time. - The default buffer size is 1 (unbuffered). Setting this value higher - will help performance in cases where large amounts of output are read back from the child. - This feature is useful in conjunction with searchwindowsize. - - The searchwindowsize attribute sets the how far back in - the incomming seach buffer Pexpect will search for pattern matches. - Every time Pexpect reads some data from the child it will append the data to - the incomming buffer. The default is to search from the beginning of the - imcomming buffer each time new data is read from the child. - But this is very inefficient if you are running a command that - generates a large amount of data where you want to match - The searchwindowsize does not effect the size of the incomming data buffer. - You will still have access to the full buffer after expect() returns. - - The logfile member turns on or off logging. - All input and output will be copied to the given file object. - Set logfile to None to stop logging. This is the default. - Set logfile to sys.stdout to echo everything to standard output. - The logfile is flushed after each write. - Example 1: - child = pexpect.spawn('some_command') - fout = file('mylog.txt','w') - child.logfile = fout - Example 2: - child = pexpect.spawn('some_command') - child.logfile = sys.stdout - - The delaybeforesend helps overcome weird behavior that many users were experiencing. - The typical problem was that a user would expect() a "Password:" prompt and - then immediately call sendline() to send the password. The user would then - see that their password was echoed back to them. Of course, passwords don't - normally echo. The problem is caused by the fact that most applications - print out the "Password" prompt and then turn off stdin echo, but if you - send your password before the application turned off echo, then you get - your password echoed. Normally this wouldn't be a problem when interacting - with a human at a real heyboard. If you introduce a slight delay just before - writing then this seems to clear up the problem. This was such a common problem - for many users that I decided that the default pexpect behavior - should be to sleep just before writing to the child application. - 1/10th of a second (100 ms) seems to be enough to clear up the problem. - You can set delaybeforesend to 0 to return to the old behavior. - - Note that spawn is clever about finding commands on your path. - It uses the same logic that "which" uses to find executables. - - If you wish to get the exit status of the child you must call - the close() method. The exit or signal status of the child will be - stored in self.exitstatus or self.signalstatus. - If the child exited normally then exitstatus will store the exit return code and - signalstatus will be None. - If the child was terminated abnormally with a signal then signalstatus will store - the signal value and exitstatus will be None. - If you need more detail you can also read the self.status member which stores - the status returned by os.waitpid. You can interpret this using - os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. - """ - self.STDIN_FILENO = pty.STDIN_FILENO - self.STDOUT_FILENO = pty.STDOUT_FILENO - self.STDERR_FILENO = pty.STDERR_FILENO - self.stdin = sys.stdin - self.stdout = sys.stdout - self.stderr = sys.stderr - - self.patterns = None - self.before = None - self.after = None - self.match = None - self.match_index = None - self.terminated = 1 - self.exitstatus = None - self.signalstatus = None - self.status = None - self.flag_eof = 0 - self.pid = None - self.child_fd = -1 # initially closed - self.timeout = timeout - self.delimiter = EOF - self.logfile = logfile - self.maxread = maxread # Max bytes to read at one time into buffer. - self.buffer = '' # This is the read buffer. See maxread. - self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched. - self.delaybeforesend = 0.1 # Sets sleep time used just before sending data to child. - self.softspace = 0 # File-like object. - self.name = '<' + repr(self) + '>' # File-like object. - self.encoding = None # File-like object. - self.closed = 1 # File-like object. - - if type (args) != type([]): - raise TypeError ('The second argument, args, must be a list.') - - if args == []: - self.args = split_command_line(command) - self.command = self.args[0] - else: - self.args = args[:] # work with a copy - self.args.insert (0, command) - self.command = command - - command_with_path = which(self.command) - if command_with_path == None: - raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command) - self.command = command_with_path - self.args[0] = self.command - - self.name = '<' + ' '.join (self.args) + '>' - self.__spawn() - - def __del__(self): - """This makes sure that no system resources are left open. - Python only garbage collects Python objects. OS file descriptors - are not Python objects, so they must be handled explicitly. - If the child file descriptor was opened outside of this class - (passed to the constructor) then this does not close it. - """ - if self.closed: - return - self.close() - - def __str__(self): - """This returns the current state of the pexpect object as a string. - """ - s = [] - s.append(repr(self)) - s.append('version: ' + __version__ + ' (' + __revision__ + ')') - s.append('command: ' + str(self.command)) - s.append('args: ' + str(self.args)) - if self.patterns is None: - s.append('patterns: None') - else: - s.append('patterns:') - for p in self.patterns: - if type(p) is type(re.compile('')): - s.append(' ' + str(p.pattern)) - else: - s.append(' ' + str(p)) - s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:]) - s.append('before (last 100 chars): ' + str(self.before)[-100:]) - s.append('after: ' + str(self.after)) - s.append('match: ' + str(self.match)) - s.append('match_index: ' + str(self.match_index)) - s.append('exitstatus: ' + str(self.exitstatus)) - s.append('flag_eof: ' + str(self.flag_eof)) - s.append('pid: ' + str(self.pid)) - s.append('child_fd: ' + str(self.child_fd)) - s.append('timeout: ' + str(self.timeout)) - s.append('delimiter: ' + str(self.delimiter)) - s.append('logfile: ' + str(self.logfile)) - s.append('maxread: ' + str(self.maxread)) - s.append('searchwindowsize: ' + str(self.searchwindowsize)) - s.append('delaybeforesend: ' + str(self.delaybeforesend)) - return '\n'.join(s) - - def __spawn(self): - """This starts the given command in a child process. - This does all the fork/exec type of stuff for a pty. - This is called by __init__. - """ - # The pid and child_fd of this object get set by this method. - # Note that it is difficult for this method to fail. - # You cannot detect if the child process cannot start. - # So the only way you can tell if the child process started - # or not is to try to read from the file descriptor. If you get - # EOF immediately then it means that the child is already dead. - # That may not necessarily be bad because you may haved spawned a child - # that performs some task; creates no stdout output; and then dies. - - assert self.pid == None, 'The pid member should be None.' - assert self.command != None, 'The command member should not be None.' - - try: - self.pid, self.child_fd = pty.fork() - except OSError, e: - raise ExceptionPexpect('Pexpect: pty.fork() failed: ' + str(e)) - - if self.pid == 0: # Child - try: # Some platforms do not like setwinsize (Cygwin). - self.child_fd = sys.stdout.fileno() # used by setwinsize() - self.setwinsize(24, 80) - except: - pass - # Do not allow child to inherit open file descriptors from parent. - max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0] - for i in range (3, max_fd): - try: - os.close (i) - except OSError: - pass - - # I don't know why this works, but ignoring SIGHUP fixes a - # problem when trying to start a Java daemon with sudo - # (specifically, Tomcat). - signal.signal(signal.SIGHUP, signal.SIG_IGN) - - os.execv(self.command, self.args) - - # Parent - self.terminated = 0 - self.closed = 0 - - def fileno (self): # File-like object. - """This returns the file descriptor of the pty for the child. - """ - return self.child_fd - - def close (self, force=0): # File-like object. - """This closes the connection with the child application. - Note that calling close() more than once is valid. - This emulates standard Python behavior with files. - Set force to 1 if you want to make sure that the child is terminated - (SIGKILL is sent if the child ignores SIGHUP and SIGINT). - """ - if self.child_fd != -1: - self.flush() - os.close (self.child_fd) - self.child_fd = -1 - self.closed = 1 - time.sleep(0.1) # Give kernel time to update process status. - if self.isalive(): - if not self.terminate(force): - raise ExceptionPexpect ('close() could not terminate the child using terminate()') - - def flush (self): # File-like object. - """This does nothing. It is here to support the interface for a File-like object. - """ - pass - - def isatty (self): # File-like object. - """This returns 1 if the file descriptor is open and connected to a tty(-like) device, else 0. - """ - return os.isatty(self.child_fd) - - def setecho (self, on): - """This sets the terminal echo mode on or off. - Note that anything the child sent before the echo will be lost, so - you should be sure that your input buffer is empty before you setecho. - For example, the following will work as expected. - p = pexpect.spawn('cat') - p.sendline ('1234') # We will see this twice (once from tty echo and again from cat). - p.expect (['1234']) - p.expect (['1234']) - p.setecho(0) # Turn off tty echo - p.sendline ('abcd') # We will set this only once (echoed by cat). - p.sendline ('wxyz') # We will set this only once (echoed by cat) - p.expect (['abcd']) - p.expect (['wxyz']) - The following WILL NOT WORK because the lines sent before the setecho - will be lost: - p = pexpect.spawn('cat') - p.sendline ('1234') # We will see this twice (once from tty echo and again from cat). - p.setecho(0) # Turn off tty echo - p.sendline ('abcd') # We will set this only once (echoed by cat). - p.sendline ('wxyz') # We will set this only once (echoed by cat) - p.expect (['1234']) - p.expect (['1234']) - p.expect (['abcd']) - p.expect (['wxyz']) - """ - self.child_fd - new = termios.tcgetattr(self.child_fd) - if on: - new[3] = new[3] | termios.ECHO - else: - new[3] = new[3] & ~termios.ECHO - # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent - # and blocked on some platforms. TCSADRAIN is probably ideal if it worked. - termios.tcsetattr(self.child_fd, termios.TCSANOW, new) - - def read_nonblocking (self, size = 1, timeout = -1): - """This reads at most size characters from the child application. - It includes a timeout. If the read does not complete within the - timeout period then a TIMEOUT exception is raised. - If the end of file is read then an EOF exception will be raised. - If a log file was set using setlog() then all data will - also be written to the log file. - - If timeout==None then the read may block indefinitely. - If timeout==-1 then the self.timeout value is used. - If timeout==0 then the child is polled and - if there was no data immediately ready then this will raise a TIMEOUT exception. - - The "timeout" refers only to the amount of time to read at least one character. - This is not effected by the 'size' parameter, so if you call - read_nonblocking(size=100, timeout=30) and only one character is - available right away then one character will be returned immediately. - It will not wait for 30 seconds for another 99 characters to come in. - - This is a wrapper around os.read(). - It uses select.select() to implement a timeout. - """ - if self.child_fd == -1: - raise ValueError ('I/O operation on closed file in read_nonblocking().') - - if timeout == -1: - timeout = self.timeout - - # Note that some systems such as Solaris do not give an EOF when - # the child dies. In fact, you can still try to read - # from the child_fd -- it will block forever or until TIMEOUT. - # For this case, I test isalive() before doing any reading. - # If isalive() is false, then I pretend that this is the same as EOF. - if not self.isalive(): - r, w, e = select.select([self.child_fd], [], [], 0) # timeout of 0 means "poll" - if not r: - self.flag_eof = 1 - raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.') - elif sys.platform.lower().find('irix') >= 0: - # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive. - # This adds a 2 second delay, but only when the child is terminated - r, w, e = select.select([self.child_fd], [], [], 2) - if not r and not self.isalive(): - self.flag_eof = 1 - raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.') - - r, w, e = select.select([self.child_fd], [], [], timeout) - if not r: - if not self.isalive(): - # Some platforms, such as Irix, will claim that their processes are alive; - # then timeout on the select; and then finally admit that they are not alive. - self.flag_eof = 1 - raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.') - else: - raise TIMEOUT ('Timeout exceeded in read_nonblocking().') - - if self.child_fd in r: - try: - s = os.read(self.child_fd, size) - except OSError, e: # Linux does this - self.flag_eof = 1 - raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.') - if s == '': # BSD style - self.flag_eof = 1 - raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.') - - if self.logfile != None: - self.logfile.write (s) - self.logfile.flush() - - return s - - raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().') - - def read (self, size = -1): # File-like object. - """This reads at most "size" bytes from the file - (less if the read hits EOF before obtaining size bytes). - If the size argument is negative or omitted, - read all data until EOF is reached. - The bytes are returned as a string object. - An empty string is returned when EOF is encountered immediately. - """ - if size == 0: - return '' - if size < 0: - self.expect (self.delimiter) # delimiter default is EOF - return self.before - - # I could have done this more directly by not using expect(), but - # I deliberately decided to couple read() to expect() so that - # I would catch any bugs early and ensure consistant behavior. - # It's a little less efficient, but there is less for me to - # worry about if I have to later modify read() or expect(). - # Note, it's OK if size==-1 in the regex. That just means it - # will never match anything in which case we stop only on EOF. - cre = re.compile('.{%d}' % size, re.DOTALL) - index = self.expect ([cre, self.delimiter]) # delimiter default is EOF - if index == 0: - return self.after ### self.before should be ''. Should I assert this? - return self.before - - def readline (self, size = -1): # File-like object. - """This reads and returns one entire line. A trailing newline is kept in - the string, but may be absent when a file ends with an incomplete line. - Note: This readline() looks for a \\r\\n pair even on UNIX because this is - what the pseudo tty device returns. So contrary to what you may be used to - you will receive a newline as \\r\\n. - An empty string is returned when EOF is hit immediately. - Currently, the size agument is mostly ignored, so this behavior is not - standard for a file-like object. If size is 0 then an empty string is returned. - """ - if size == 0: - return '' - index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF - if index == 0: - return self.before + '\r\n' - else: - return self.before - - def __iter__ (self): # File-like object. - """This is to support interators over a file-like object. - """ - return self - - def next (self): # File-like object. - """This is to support iterators over a file-like object. - """ - result = self.readline() - if result == "": - raise StopIteration - return result - - def readlines (self, sizehint = -1): # File-like object. - """This reads until EOF using readline() and returns a list containing - the lines thus read. The optional "sizehint" argument is ignored. - """ - lines = [] - while 1: - line = self.readline() - if not line: - break - lines.append(line) - return lines - - def write(self, str): # File-like object. - """This is similar to send() except that there is no return value. - """ - self.send (str) - - def writelines (self, sequence): # File-like object. - """This calls write() for each element in the sequence. - The sequence can be any iterable object producing strings, - typically a list of strings. This does not add line separators - There is no return value. - """ - for str in sequence: - self.write (str) - - def send(self, str): - """This sends a string to the child process. - This returns the number of bytes written. - If a log file was set then the data is also written to the log. - """ - time.sleep(self.delaybeforesend) - if self.logfile != None: - self.logfile.write (str) - self.logfile.flush() - c = os.write(self.child_fd, str) - return c - - def sendline(self, str=''): - """This is like send(), but it adds a line feed (os.linesep). - This returns the number of bytes written. - """ - n = self.send(str) - n = n + self.send (os.linesep) - return n - - def sendeof(self): - """This sends an EOF to the child. - This sends a character which causes the pending parent output - buffer to be sent to the waiting child program without - waiting for end-of-line. If it is the first character of the - line, the read() in the user program returns 0, which - signifies end-of-file. This means to work as expected - a sendeof() has to be called at the begining of a line. - This method does not send a newline. It is the responsibility - of the caller to ensure the eof is sent at the beginning of a line. - """ - ### Hmmm... how do I send an EOF? - ###C if ((m = write(pty, *buf, p - *buf)) < 0) - ###C return (errno == EWOULDBLOCK) ? n : -1; - fd = sys.stdin.fileno() - old = termios.tcgetattr(fd) # remember current state - new = termios.tcgetattr(fd) - new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF - try: # use try/finally to ensure state gets restored - termios.tcsetattr(fd, termios.TCSADRAIN, new) - if 'CEOF' in dir(termios): - os.write (self.child_fd, '%c' % termios.CEOF) - else: - os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D - finally: # restore state - termios.tcsetattr(fd, termios.TCSADRAIN, old) - - def eof (self): - """This returns 1 if the EOF exception was raised at some point. - """ - return self.flag_eof - - def terminate(self, force=0): - """This forces a child process to terminate. - It starts nicely with SIGHUP and SIGINT. If "force" is 1 then - moves onto SIGKILL. - This returns true if the child was terminated. - This returns false if the child could not be terminated. - """ - if not self.isalive(): - return 1 - self.kill(signal.SIGHUP) - time.sleep(0.1) - if not self.isalive(): - return 1 - self.kill(signal.SIGCONT) - time.sleep(0.1) - if not self.isalive(): - return 1 - self.kill(signal.SIGINT) - time.sleep(0.1) - if not self.isalive(): - return 1 - if force: - self.kill(signal.SIGKILL) - time.sleep(0.1) - if not self.isalive(): - return 1 - else: - return 0 - return 0 - #raise ExceptionPexpect ('terminate() could not terminate child process. Try terminate(force=1)?') - - def isalive(self): - """This tests if the child process is running or not. - This is non-blocking. If the child was terminated then this - will read the exitstatus or signalstatus of the child. - This returns 1 if the child process appears to be running or 0 if not. - It can take literally SECONDS for Solaris to return the right status. - """ - if self.terminated: - return 0 - - if self.flag_eof: - # This is for Linux, which requires the blocking form of waitpid to get - # status of a defunct process. This is super-lame. The flag_eof would have - # been set in read_nonblocking(), so this should be safe. - waitpid_options = 0 - else: - waitpid_options = os.WNOHANG - - try: - pid, status = os.waitpid(self.pid, waitpid_options) - except OSError, e: # No child processes - if e[0] == errno.ECHILD: - raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?') - else: - raise e - - # I have to do this twice for Solaris. I can't even believe that I figured this out... - # If waitpid() returns 0 it means that no child process wishes to - # report, and the value of status is undefined. - if pid == 0: - try: - pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris! - except OSError, e: # This should never happen... - if e[0] == errno.ECHILD: - raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?') - else: - raise e - - # If pid is still 0 after two calls to waitpid() then - # the process really is alive. This seems to work on all platforms, except - # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking - # take care of this situation (unfortunately, this requires waiting through the timeout). - if pid == 0: - return 1 - - if pid == 0: - return 1 - - if os.WIFEXITED (status): - self.status = status - self.exitstatus = os.WEXITSTATUS(status) - self.signalstatus = None - self.terminated = 1 - return 0 - elif os.WIFSIGNALED (status): - self.status = status - self.exitstatus = None - self.signalstatus = os.WTERMSIG(status) - self.terminated = 1 - return 0 - elif os.WIFSTOPPED (status): - raise ExceptionPexpect ('isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?') - - raise ExceptionPexpect ('isalive() reached unexpected condition where waitpid matched child pid, but status was not matched by WIFEXITED, WIFSIGNALED, or WIFSTOPPED.') - - - def kill(self, sig): - """This sends the given signal to the child application. - In keeping with UNIX tradition it has a misleading name. - It does not necessarily kill the child unless - you send the right signal. - """ - # Same as os.kill, but the pid is given for you. - if self.isalive(): - os.kill(self.pid, sig) - - def compile_pattern_list(self, patterns): - """This compiles a pattern-string or a list of pattern-strings. - Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or - a list of those. Patterns may also be None which results in - an empty list. - - This is used by expect() when calling expect_list(). - Thus expect() is nothing more than:: - cpl = self.compile_pattern_list(pl) - return self.expect_list(clp, timeout) - - If you are using expect() within a loop it may be more - efficient to compile the patterns first and then call expect_list(). - This avoid calls in a loop to compile_pattern_list(): - cpl = self.compile_pattern_list(my_pattern) - while some_condition: - ... - i = self.expect_list(clp, timeout) - ... - """ - if patterns is None: - return [] - if type(patterns) is not types.ListType: - patterns = [patterns] - - compiled_pattern_list = [] - for p in patterns: - if type(p) is types.StringType: - compiled_pattern_list.append(re.compile(p, re.DOTALL)) - elif p is EOF: - compiled_pattern_list.append(EOF) - elif p is TIMEOUT: - compiled_pattern_list.append(TIMEOUT) - elif type(p) is type(re.compile('')): - compiled_pattern_list.append(p) - else: - raise TypeError ('Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p))) - - return compiled_pattern_list - - def expect(self, pattern, timeout = -1, searchwindowsize=None): - """This seeks through the stream until a pattern is matched. - The pattern is overloaded and may take several types including a list. - The pattern can be a StringType, EOF, a compiled re, or - a list of those types. Strings will be compiled to re types. - This returns the index into the pattern list. If the pattern was - not a list this returns index 0 on a successful match. - This may raise exceptions for EOF or TIMEOUT. - To avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to - the pattern list. - - After a match is found the instance attributes - 'before', 'after' and 'match' will be set. - You can see all the data read before the match in 'before'. - You can see the data that was matched in 'after'. - The re.MatchObject used in the re match will be in 'match'. - If an error occured then 'before' will be set to all the - data read so far and 'after' and 'match' will be None. - - If timeout is -1 then timeout will be set to the self.timeout value. - - Note: A list entry may be EOF or TIMEOUT instead of a string. - This will catch these exceptions and return the index - of the list entry instead of raising the exception. - The attribute 'after' will be set to the exception type. - The attribute 'match' will be None. - This allows you to write code like this: - index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT]) - if index == 0: - do_something() - elif index == 1: - do_something_else() - elif index == 2: - do_some_other_thing() - elif index == 3: - do_something_completely_different() - instead of code like this: - try: - index = p.expect (['good', 'bad']) - if index == 0: - do_something() - elif index == 1: - do_something_else() - except EOF: - do_some_other_thing() - except TIMEOUT: - do_something_completely_different() - These two forms are equivalent. It all depends on what you want. - You can also just expect the EOF if you are waiting for all output - of a child to finish. For example: - p = pexpect.spawn('/bin/ls') - p.expect (pexpect.EOF) - print p.before - - If you are trying to optimize for speed then see expect_list(). - """ - compiled_pattern_list = self.compile_pattern_list(pattern) - return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) - - def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1): - """This takes a list of compiled regular expressions and returns - the index into the pattern_list that matched the child's output. - The list may also contain EOF or TIMEOUT (which are not - compiled regular expressions). This method is similar to - the expect() method except that expect_list() does not - recompile the pattern list on every call. - This may help if you are trying to optimize for speed, otherwise - just use the expect() method. This is called by expect(). - If timeout==-1 then the self.timeout value is used. - If searchwindowsize==-1 then the self.searchwindowsize value is used. - """ - self.patterns = pattern_list - - if timeout == -1: - timeout = self.timeout - if timeout != None: - end_time = time.time() + timeout - if searchwindowsize == -1: - searchwindowsize = self.searchwindowsize - - try: - incoming = self.buffer - while 1: # Keep reading until exception or return. - # Sequence through the list of patterns looking for a match. - first_match = -1 - for cre in pattern_list: - if cre is EOF or cre is TIMEOUT: - continue # The patterns for PexpectExceptions are handled differently. - if searchwindowsize is None: # search everything - match = cre.search(incoming) - else: - startpos = max(0, len(incoming) - searchwindowsize) - match = cre.search(incoming, startpos) - if match is None: - continue - if first_match > match.start() or first_match == -1: - first_match = match.start() - self.match = match - self.match_index = pattern_list.index(cre) - if first_match > -1: - self.buffer = incoming[self.match.end() : ] - self.before = incoming[ : self.match.start()] - self.after = incoming[self.match.start() : self.match.end()] - return self.match_index - # No match at this point - if timeout < 0 and timeout is not None: - raise TIMEOUT ('Timeout exceeded in expect_list().') - # Still have time left, so read more data - c = self.read_nonblocking (self.maxread, timeout) - incoming = incoming + c - if timeout is not None: - timeout = end_time - time.time() - except EOF, e: - self.buffer = '' - self.before = incoming - self.after = EOF - if EOF in pattern_list: - self.match = EOF - self.match_index = pattern_list.index(EOF) - return self.match_index - else: - self.match = None - self.match_index = None - raise EOF (str(e) + '\n' + str(self)) - except TIMEOUT, e: - self.before = incoming - self.after = TIMEOUT - if TIMEOUT in pattern_list: - self.match = TIMEOUT - self.match_index = pattern_list.index(TIMEOUT) - return self.match_index - else: - self.match = None - self.match_index = None - raise TIMEOUT (str(e) + '\n' + str(self)) - except Exception: - self.before = incoming - self.after = None - self.match = None - self.match_index = None - raise - - def getwinsize(self): - """This returns the window size of the child tty. - The return value is a tuple of (rows, cols). - """ - if 'TIOCGWINSZ' in dir(termios): - TIOCGWINSZ = termios.TIOCGWINSZ - else: - TIOCGWINSZ = 1074295912L # Assume - s = struct.pack('HHHH', 0, 0, 0, 0) - x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s) - return struct.unpack('HHHH', x)[0:2] - - def setwinsize(self, r, c): - """This sets the window size of the child tty. - This will cause a SIGWINCH signal to be sent to the child. - This does not change the physical window size. - It changes the size reported to TTY-aware applications like - vi or curses -- applications that respond to the SIGWINCH signal. - """ - # Check for buggy platforms. Some Python versions on some platforms - # (notably OSF1 Alpha and RedHat 7.1) truncate the value for - # termios.TIOCSWINSZ. It is not clear why this happens. - # These platforms don't seem to handle the signed int very well; - # yet other platforms like OpenBSD have a large negative value for - # TIOCSWINSZ and they don't have a truncate problem. - # Newer versions of Linux have totally different values for TIOCSWINSZ. - # Note that this fix is a hack. - if 'TIOCSWINSZ' in dir(termios): - TIOCSWINSZ = termios.TIOCSWINSZ - else: - TIOCSWINSZ = -2146929561 - if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2. - TIOCSWINSZ = -2146929561 # Same bits, but with sign. - # Note, assume ws_xpixel and ws_ypixel are zero. - s = struct.pack('HHHH', r, c, 0, 0) - fcntl.ioctl(self.fileno(), TIOCSWINSZ, s) - - def interact(self, escape_character = chr(29)): - """This gives control of the child process to the interactive user - (the human at the keyboard). - Keystrokes are sent to the child process, and the stdout and stderr - output of the child process is printed. - When the user types the escape_character this method will stop. - The default for escape_character is ^] (ASCII 29). - This simply echos the child stdout and child stderr to the real - stdout and it echos the real stdin to the child stdin. - - Note that if you change the window size of the parent - the SIGWINCH signal will not be passed through to the child. - If you want the child window size to change when the parent's - window size changes then do something like the following example: - import pexpect, struct, fcntl, termios, signal, sys - def sigwinch_passthrough (sig, data): - s = struct.pack("HHHH", 0, 0, 0, 0) - a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s)) - global p - p.setwinsize(a[0],a[1]) - p = pexpect.spawn('/bin/bash') # Note this is global - signal.signal(signal.SIGWINCH, sigwinch_passthrough) - p.interact() - """ - # Flush the buffer. - self.stdout.write (self.buffer) - self.stdout.flush() - self.buffer = '' - mode = tty.tcgetattr(self.STDIN_FILENO) - tty.setraw(self.STDIN_FILENO) - try: - self.__interact_copy(escape_character) - finally: - tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode) - - def __interact_writen(self, fd, data): - """This is used by the interact() method. - """ - while data != '' and self.isalive(): - n = os.write(fd, data) - data = data[n:] - def __interact_read(self, fd): - """This is used by the interact() method. - """ - return os.read(fd, 1000) - def __interact_copy(self, escape_character = None): - """This is used by the interact() method. - """ - while self.isalive(): - try: - r, w, e = select.select([self.child_fd, self.STDIN_FILENO], [], []) - except select.errno, e: - if e[0] != errno.EINTR: - raise - if self.child_fd in r: - data = self.__interact_read(self.child_fd) - if self.logfile != None: - self.logfile.write (data) - self.logfile.flush() - os.write(self.STDOUT_FILENO, data) - if self.STDIN_FILENO in r: - data = self.__interact_read(self.STDIN_FILENO) - self.__interact_writen(self.child_fd, data) - if escape_character in data: - break -############################################################################## -# The following methods are no longer supported or allowed.. - def setmaxread (self, maxread): - """This method is no longer supported or allowed. - I don't like getters and setters without a good reason. - """ - raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.') - def expect_exact (self, pattern_list, timeout = -1): - """This method is no longer supported or allowed. - It was too hard to maintain and keep it up to date with expect_list. - Few people used this method. Most people favored reliability over speed. - The implementation is left in comments in case anyone needs to hack this - feature back into their copy. - If someone wants to diff this with expect_list and make them work - nearly the same then I will consider adding this make in. - """ - raise ExceptionPexpect ('This method is no longer supported or allowed.') - def setlog (self, fileobject): - """This method is no longer supported or allowed. - """ - raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.') - -############################################################################## -# End of spawn class -############################################################################## - -def which (filename): - """This takes a given filename; tries to find it in the environment path; - then checks if it is executable. - This returns the full path to the filename if found and executable. - Otherwise this returns None. - """ - # Special case where filename already contains a path. - if os.path.dirname(filename) != '': - if os.access (filename, os.X_OK): - return filename - - if not os.environ.has_key('PATH') or os.environ['PATH'] == '': - p = os.defpath - else: - p = os.environ['PATH'] - - # Oddly enough this was the one line that made Pexpect - # incompatible with Python 1.5.2. - #pathlist = p.split (os.pathsep) - pathlist = string.split (p, os.pathsep) - - for path in pathlist: - f = os.path.join(path, filename) - if os.access(f, os.X_OK): - return f - return None - -def split_command_line(command_line): - """This splits a command line into a list of arguments. - It splits arguments on spaces, but handles - embedded quotes, doublequotes, and escaped characters. - It's impossible to do this with a regular expression, so - I wrote a little state machine to parse the command line. - """ - arg_list = [] - arg = '' - - # Constants to name the states we can be in. - state_basic = 0 - state_esc = 1 - state_singlequote = 2 - state_doublequote = 3 - state_whitespace = 4 # The state of consuming whitespace between commands. - state = state_basic - - for c in command_line: - if state == state_basic or state == state_whitespace: - if c == '\\': # Escape the next character - state = state_esc - elif c == r"'": # Handle single quote - state = state_singlequote - elif c == r'"': # Handle double quote - state = state_doublequote - elif c.isspace(): - # Add arg to arg_list if we aren't in the middle of whitespace. - if state == state_whitespace: - None # Do nothing. - else: - arg_list.append(arg) - arg = '' - state = state_whitespace - else: - arg = arg + c - state = state_basic - elif state == state_esc: - arg = arg + c - state = state_basic - elif state == state_singlequote: - if c == r"'": - state = state_basic - else: - arg = arg + c - elif state == state_doublequote: - if c == r'"': - state = state_basic - else: - arg = arg + c - - if arg != '': - arg_list.append(arg) - return arg_list - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/AcceptHandler.java --- a/src/fdl/AcceptHandler.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/AcceptHandler.java Wed Feb 08 18:02:40 2012 +0900 @@ -31,8 +31,11 @@ // アクセプト処理 SocketChannel channel = serverChannel.accept(); channel.configureBlocking(false); + //channel.socket().setTcpNoDelay(true); fds.log(Level.INFO,"Server: accepted "+channel.socket()); + // TCP N Delay を ServerSocketChannel のオプションに指定する + // setTcpNoDelay(boolean on) tupleSpace.newUser(); // 入出力用のハンドラを生成し,アタッチする diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/FDLindaServ.java --- a/src/fdl/FDLindaServ.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/FDLindaServ.java Wed Feb 08 18:02:40 2012 +0900 @@ -12,19 +12,19 @@ import java.util.Iterator; import java.util.logging.Level; - public class FDLindaServ { static final int MAX_REQ = 1; static final int FAIL = (-1); static final int DEF_PORT = 10000; + public static boolean debug = false; public int port = DEF_PORT; AbstractSelector selector; private ServerSocketChannel ssChannel; public TupleSpace tupleSpace; public MetaEngine me; - public static void main(final String[] args) { - final String usages = "usage: FDLindaServ [-p port]"; + public static void main(MetaEngine me, final String[] args) { + final String usages = "usage: FDLindaServ [-d] [-p port]"; int port = DEF_PORT; //引数判定 @@ -32,7 +32,9 @@ for (int i=0; i0) { +// this does not work because #it.remove() is not called. +// for(SelectionKey s:selector.selectedKeys()) { +// TupleHandler handler = (TupleHandler)s.attachment(); +// handler.handle(s); +// } + for (Iterator it = selector.selectedKeys().iterator();it.hasNext(); ) { + SelectionKey s = it.next(); + it.remove(); + TupleHandler handler = (TupleHandler)s.attachment(); + handler.handle(s); + } + } + } catch (ClosedChannelException e) { + // we have to do something... + } catch (IOException e) { + }} public void checkTuple(long timeout) { // セレクタによる監視 @@ -92,7 +115,7 @@ // TupleHandler handler = (TupleHandler)s.attachment(); // handler.handle(s); // } - for (Iterator it = selector.selectedKeys().iterator();it.hasNext(); ) { + for (Iterator it = selector.selectedKeys().iterator();it.hasNext(); ) { SelectionKey s = it.next(); it.remove(); TupleHandler handler = (TupleHandler)s.attachment(); @@ -107,8 +130,13 @@ public void log(Level level,String msg) { + if (level!=Level.SEVERE && !debug) return; System.err.println(msg); if (level==Level.SEVERE) new IOException().setStackTrace(null); } + + public static void main(final String[] args) { + main(new NullMetaEngine(),args); + } } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/FederatedLinda.java --- a/src/fdl/FederatedLinda.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/FederatedLinda.java Wed Feb 08 18:02:40 2012 +0900 @@ -38,7 +38,7 @@ FederatedLinda fdl; static int MAX_SEQUENCE = 2048; - static boolean debug = false; + public static boolean debug = false; public int tid; public int seq; @@ -49,20 +49,28 @@ public PSXQueue q_top,q_end; public PSXReply r_top,r_end; - public Hashtable seqHash; + public Hashtable seqHash = new Hashtable(); public static FederatedLinda init() { FederatedLinda fdl = new FederatedLinda(); return fdl; } + public static FederatedLinda init(Selector selector) { + FederatedLinda fdl = new FederatedLinda(selector); + return fdl; + } + private FederatedLinda() { try { selector = Selector.open(); } catch (IOException e) { e.printStackTrace(); } - seqHash = new Hashtable(); + } + + public FederatedLinda(Selector selector) { + this.selector = selector; } public PSXLinda open(String _host,int _port) @@ -116,9 +124,7 @@ } s = new Integer(seq); } while (seqHash.containsKey(s)); - if (debug) { - log(Level.INFO,"hash value = "+s.hashCode()); - } + // log(Level.INFO,"hash value = "+s.hashCode()); seqHash.put(s,reply); seq++; return seq-1; @@ -128,12 +134,47 @@ return selector; } - public int sync() throws IOException { - return sync(0); + /** + * sync with no wait + * @return 0 + * @throws IOException + */ + public int sync() { + int key_num = 0; + queueExec(); + + try { + if (selector.selectNow()>0) { + for (Iterator it = selector.selectedKeys().iterator();it.hasNext(); ) { + SelectionKey s = it.next(); + it.remove(); + try { + if (!s.isReadable()) + throw new IOException(); + TupleHandler handle = (TupleHandler)s.attachment(); + handle.handle(s); + } catch (IOException e) { + s.cancel(); + log(Level.INFO,""+s.channel()+" is closed."); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } catch (ClosedSelectorException e) { + // client should be know + } + + return key_num; } - public int sync(long mtimeout) - throws IOException { + /** + * sync with mtimeout msec wait + * @param mtimeout 0 means indifinite wait + * @return 0 + * @throws IOException + */ + public int sync(long mtimeout) { int key_num = 0; queueExec(); @@ -162,7 +203,7 @@ return key_num; } - private void queueExec() { + public void queueExec() { while (q_top != null){ PSXQueue c = q_top; c.send(); @@ -180,10 +221,16 @@ } public void log(Level level,String msg) { + if (level!=Level.SEVERE && !debug) return; System.err.println(msg); if (level==Level.SEVERE) new IOException().setStackTrace(null); } + + public void wakeup() { + selector.wakeup(); + } + } /* end */ diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/IOHandler.java --- a/src/fdl/IOHandler.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/IOHandler.java Wed Feb 08 18:02:40 2012 +0900 @@ -9,7 +9,7 @@ import java.util.logging.Level; public class IOHandler implements TupleHandler { - static final boolean debug = true; + static final boolean debug = false; public TupleSpace tupleSpace; public SocketChannel ch; public FDLindaServ fds; @@ -67,7 +67,9 @@ } else if (mode == PSX.PSX_WAIT_RD) { tupleSpace.Wait_Rd(key, command, mode); } else if(mode == PSX.PSX_OUT) { - tupleSpace.Out(key, command, data); + tupleSpace.Out(key, command, data); + } else if (mode == PSX.PSX_UPDATE){ + tupleSpace.Update(key, command, data); } else { tupleSpace.hook.closeHook(key); fds.log(Level.SEVERE,"Incorrect tuple operation"); diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/MetaEngine.java --- a/src/fdl/MetaEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/MetaEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -1,5 +1,6 @@ package fdl; public interface MetaEngine { - void mainLoop(); + void mainLoop(MetaLinda ml); + } \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/MetaLinda.java --- a/src/fdl/MetaLinda.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/MetaLinda.java Wed Feb 08 18:02:40 2012 +0900 @@ -30,13 +30,15 @@ public TupleSpace ts; public FDLindaServ fds; - public FederatedLinda fdl=FederatedLinda.init(); + public FederatedLinda fdl; public PSXLinda next=null; - private LinkedList replies=new LinkedList(); + public LinkedList replies=new LinkedList(); public MetaLinda(TupleSpace ts,FDLindaServ fds) { this.ts = ts; this.fds = fds; + // Selector have to be shared with FDLindaServ and FederatedLInda + fdl=FederatedLinda.init(fds.selector); } public PSXLinda open(String _host,int _port) @@ -95,37 +97,70 @@ addReply(r); } + + public void waitRd(int id, PSXCallback callback) { + MetaReply r = new MetaReply(PSX.PSX_WAIT_RD,id,ts,callback); + addReply(r); + } + + public PSXReply waitRd(int id) { + MetaReply r = new MetaReply(PSX.PSX_WAIT_RD,id,ts); + return r; + } + public PSXLinda add(PSXLinda linda) { next = linda; return this; } - + /** + * Meta Sync with no wait + */ public int sync() { - return sync(0); + fdl.queueExec(); + fds.checkTuple(); // fdl sync is also handled here + return metaSync(); } + /** + * Meta Sync with wait + * @param timeout wait timeout msec, if 0 wait indefinitely + */ public int sync(long timeout) { + fdl.queueExec(); fds.checkTuple(timeout); // fdl sync is also handled here + return metaSync(); + } + + public int metaSync() { /* * r.callback() may call meta.sync() and modifies the * replies queue. Do current size of queue only. The * rest is checked on the next sync call including * the recursive case. */ - int count = replies.size(); - while(count-->0) { - MetaReply r = replies.poll(); - // previous call back may call this sync and make - // replies shorter. - if (r==null) break; - if (r.ready()) { - } else { - addReply(r); + boolean hasNewReply; + do { + hasNewReply = false; + int count = replies.size(); + while (count-->0) { + MetaReply r = replies.poll(); + // previous call back may call this sync and make + // replies shorter. + if (r==null) break; + if (r.ready()) { + hasNewReply = true; + } else { + addReply(r); + } } - } + } while (hasNewReply); return 0; } + + public void wakeup() { + fdl.wakeup(); + } public void send(ByteBuffer command, ByteBuffer data) { } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/MetaLogEngine.java --- a/src/fdl/MetaLogEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/MetaLogEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -28,7 +28,7 @@ do { data = commDebug.getLog(); if (data!=null) break; - meta.sync(); + meta.sync(0); } while (running); if (running) { meta.out(PSX.META_MONITOR_DATA, data); @@ -36,11 +36,11 @@ } }}; - public void mainLoop() { + public void mainLoop(MetaLinda meta) { meta.in(PSX.META_MONITOR,monitor_callback_start); meta.in(PSX.META_STOP, new PSXCallback() {public void callback(ByteBuffer reply) { running = false;}}); while(running) - meta.sync(); + meta.sync(0); } } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/MetaReply.java --- a/src/fdl/MetaReply.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/MetaReply.java Wed Feb 08 18:02:40 2012 +0900 @@ -52,10 +52,14 @@ ts.Out(null, command, data); return true; case PSX.PSX_UPDATE: - // not implemented + // TODO: not implemented break; } return mode==PSX.PSX_ANSWER; } + + public String toString() { + return "MetaReply(mode="+mode+"+id="+id+")"; + } } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/NullMetaEngine.java --- a/src/fdl/NullMetaEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/NullMetaEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -3,17 +3,17 @@ import java.nio.ByteBuffer; public class NullMetaEngine implements MetaEngine { - public MetaLinda meta; public boolean running=true; - public NullMetaEngine(MetaLinda meta) { - this.meta = meta; + public NullMetaEngine() { } - public void mainLoop() { + public void mainLoop(MetaLinda meta) { meta.in(PSX.META_STOP, new PSXCallback() {public void callback(ByteBuffer reply) { running = false;}}); while(running) - meta.sync(); + meta.sync(0); } + + } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/PSX.java --- a/src/fdl/PSX.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/PSX.java Wed Feb 08 18:02:40 2012 +0900 @@ -218,7 +218,7 @@ System.err.println("read size mismatch"+readsize+" and "+command.capacity()); } while(readsize>0) { - if(false && IOHandler.debug){ + if(IOHandler.debug){ System.out.println("reading packet..."+readsize); } count = channel.read(command); diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/PSXLinda.java --- a/src/fdl/PSXLinda.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/PSXLinda.java Wed Feb 08 18:02:40 2012 +0900 @@ -3,32 +3,129 @@ import java.io.IOException; import java.nio.ByteBuffer; +/** + * @author kono + * + */ public interface PSXLinda { - + + /** + * send IN request to the linda + * Tuple with id will be removed, and the value is returned in the reply + * @param id + * @return wait for this request + */ public PSXReply in(int id) ; + /** + * send IN request and set call back for reply + * @param id + * @param callback + */ public void in(int id, PSXCallback callback); + /** + * send Check request to the Linda + * Tuple with id will not be removed, it return even if there is no tuple for + * this id + * @param id + * @return wait for this request + */ public PSXReply ck(int id) ; + /** + * send Check request and set call back for reply + * @param id + * @param callback + */ public void ck(int id, PSXCallback callback) ; + /** + * send a Tuple to the linda + * Tuple may stack in the linda, multiple out is possible. + * No blocking. + * this id + * @param id + * @return wait for this request + */ public PSXReply out(int id, ByteBuffer data) ; + /** + * replace a Tuple in the linda + * the old value is returned in the reply + * @param id + * @return wait for this request + */ public PSXReply update(int id, ByteBuffer data) ; + /** + * send Update request and set call back for reply + * @param id + * @param callback + */ public void update(int id, ByteBuffer data,PSXCallback callback) ; + /** + * Read the tuple in the linda, do not remove the tuple. + * it return it's value + * @param id + * @return wait for this request + */ public PSXReply rd(int id) ; + /** + * send Read request and set call back for reply + * @param id + * @param callback + */ public void rd(int id, PSXCallback callback) ; - public PSXLinda add(PSXLinda linda) ; + + /** + * Wait for newly incoming tuple for this id. + * Existing tuple is ignored. + * Read the tuple in the linda, do not remove the tuple. + * it return it's value + * @param id + * @return wait for this request + */ + public PSXReply waitRd(int i); + /** + * send Read request and set call back for reply + * @param id + * @param callback + */ + public void waitRd(int i, PSXCallback callback); + + /** + * Send all registered requests, and read incoming replies. + * No wait + * @return + * @throws IOException + */ public int sync() throws IOException ; + /** + * Send all registered requests, and read incoming replies. + * @param mtime wait for mtime msec, if 0 this waits indefinitely. + * @return + * @throws IOException + */ public int sync(long mtime) throws IOException ; + /** + * Internal use + * @param linda + * @return + */ + public PSXLinda add(PSXLinda linda) ; + + /* + * Intenal use + * sned data for this port + */ public void send(ByteBuffer command, ByteBuffer data); + } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/PSXLindaImpl.java --- a/src/fdl/PSXLindaImpl.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/PSXLindaImpl.java Wed Feb 08 18:02:40 2012 +0900 @@ -65,12 +65,6 @@ if (debug) { fdl.log(Level.INFO,"waiting for connect"); } - if (false) { - try { - wait(2000); - } catch (InterruptedException e) { - } - } } fdl.log(Level.INFO,"Linda client connect to "+socketChannel); socketChannel.register(selector,SelectionKey.OP_READ,this); @@ -117,6 +111,15 @@ } } + public void close() { + if (socketChannel != null) { + try { + socketChannel.close(); + } catch (IOException e) { + } + } + } + private void checkConnect(String s) { fdl.log(Level.INFO, "Connected:"+ s +": " +socketChannel.isConnected()); @@ -183,6 +186,15 @@ } + public PSXReply waitRd(int id) { + PSXReply r = fdl.psx_queue(this, id, null, PSX.PSX_WAIT_RD, null); + return r; + } + + public void waitRd(int id, PSXCallback callback) { + fdl.psx_queue(this, id, null, PSX.PSX_WAIT_RD, callback); + } + } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/TupleSpace.java --- a/src/fdl/TupleSpace.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/TupleSpace.java Wed Feb 08 18:02:40 2012 +0900 @@ -39,6 +39,8 @@ ByteBuffer data = ByteBuffer.allocate(10); ByteBuffer r = ByteBuffer.allocate(10); int value = user; + + r.put((byte) '\0'); while(value>0) { r.put((byte) (value%10 + '0')); value /= 10; @@ -55,13 +57,18 @@ } protected void Out(SelectionKey key,ByteBuffer command, ByteBuffer data) { - Tuple tuple; - int id; - int datasize; char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); command.rewind(); - id = (int)idc; + int id = (int)idc; + + Out1(key, command, data, id); + } + + private void Out1(SelectionKey key, ByteBuffer command, ByteBuffer data, + int id) { + Tuple tuple; + int datasize; datasize = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); command.rewind(); @@ -75,7 +82,7 @@ PSX.setAnserCommand(command, tuple_space[id].getSeq()); PSX.send(tuple_space[id].ch, command, data); - removeTuple(id); + tuple_space[id] = tuple_space[id].next; tuple = null; } if(tuple_space[id] != null && tuple_space[id].mode == PSX.PSX_IN) { @@ -85,7 +92,7 @@ if (debug) fds.log(Level.INFO,"send size "+datasize+" : mode = "+(char)'a'); } PSX.send(tuple_space[id].ch, command, data); - removeTuple(id); + tuple_space[id] = tuple_space[id].next; tuple = null; // Incoming Out tuple is consumed here, and wating IN tuple is also removed. } else if ((tuple_space[id] == null)|| (tuple_space[id].getMode() == PSX.PSX_OUT)) { @@ -111,13 +118,26 @@ } } - private void removeTuple(int id) { - Tuple tuple; - //後処理 - tuple = tuple_space[id]; - tuple_space[id] = tuple.next; + protected void Update(SelectionKey key, ByteBuffer command, ByteBuffer data) { + char idc = (char)command.getShort(PSX.LINDA_ID_OFFSET); + command.rewind(); + int id = (int)idc; + + Tuple tuple = tuple_space[id]; + if (tuple != null) { + while (tuple.next != null) + tuple = tuple.next; + tuple.id = id; + tuple.setCommand(PSX.PSX_ANSWER, command.getInt(PSX.LINDA_SEQ_OFFSET)); + } else { + tuple = new Tuple(); + tuple.setCommand(PSX.PSX_ANSWER, id, command.getInt(PSX.LINDA_SEQ_OFFSET), ByteBuffer.allocate(0)); + } + PSX.send(key, tuple.getCommand(), tuple.getData()); + tuple_space[id] = null; + Out1(key, command, data, id); } - + protected void Wait_Rd(SelectionKey key, ByteBuffer command, int mode) { Tuple tuple; int id; @@ -291,5 +311,4 @@ return data; } - } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/multiengine/MultiEngineServer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/multiengine/MultiEngineServer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,19 @@ +package fdl.multiengine; + +import fdl.FDLindaServ; +import fdl.multiengine.meta.MetaEngineExecutor; +import fdl.multiengine.meta.StopServerEngine; + +public class MultiEngineServer { + static MetaEngineExecutor mee; + + /** + * @param args + */ + public static void main(String[] args) { + mee = new MetaEngineExecutor(); + mee.addMetaEngine(new StopServerEngine(mee)); + FDLindaServ.main(mee, args); // start server + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/multiengine/TupleClassLoader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/multiengine/TupleClassLoader.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,45 @@ +package fdl.multiengine; + +import java.nio.ByteBuffer; + +import com.google.protobuf.InvalidProtocolBufferException; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.protobuf.TupleClass.ClassFile; + +public class TupleClassLoader extends ClassLoader implements MetaEngine { + public String name; + private byte[] file; + private Class clazz; + + @SuppressWarnings("unchecked") + public TupleClassLoader(ByteBuffer protoc) { + try { + ClassFile classFile = ClassFile.parseFrom(protoc.array()); + name = classFile.getName(); + file = classFile.getFile().toByteArray(); + clazz = (Class) findClass(name); + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + } + } + + @Override + public Class findClass(String name) { + return defineClass(name, file, 0, file.length); + } + + public void mainLoop(MetaLinda ml) { + @SuppressWarnings("rawtypes") + Class[] parameter = { MetaLinda.class }; + try { + clazz.getMethod("mainLoop", parameter); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/multiengine/TupleId.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/multiengine/TupleId.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,39 @@ +package fdl.multiengine; + +import java.util.HashMap; + +public enum TupleId { + MANAGE (60000), + STOP; + + public int id; + public static HashMap hash = new HashMap(); + private static int lastId = 0; + + private TupleId(int id) { + this.id = id; + setLastId(id); + } + + private TupleId() { + this.id = incrementLastId(); + } + + private void setLastId(int id) { + lastId = id; + } + + private int incrementLastId() { + return ++lastId; + } + + public static TupleId getTupleIdFromId(int id) { + return hash.get(id); + } + + static { + for (TupleId tid : TupleId.values()) { + hash.put(tid.id, tid); + } + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/multiengine/meta/MetaEngineExecutor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/multiengine/meta/MetaEngineExecutor.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,33 @@ +package fdl.multiengine.meta; + +import java.util.LinkedList; + +import fdl.MetaEngine; +import fdl.MetaLinda; + +public class MetaEngineExecutor implements MetaEngine { + private LinkedList engines; + public boolean running = true; + + public MetaEngineExecutor() { + engines = new LinkedList(); + } + + public void mainLoop(MetaLinda ml) { + while (running) { + for (MetaEngine engine : engines) { + engine.mainLoop(ml); + } + ml.sync(0); + } + } + + public void addMetaEngine(MetaEngine engine) { + engines.add(engine); + } + + public void removeMetaEngine(MetaEngine engine) { + engines.remove(engine); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/multiengine/meta/StopServerEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/multiengine/meta/StopServerEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,24 @@ +package fdl.multiengine.meta; + +import java.nio.ByteBuffer; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXCallback; +import fdl.multiengine.TupleId; + +public class StopServerEngine implements MetaEngine { + MetaEngineExecutor mee; + + public StopServerEngine(MetaEngineExecutor mee) { + this.mee = mee; + } + + public void mainLoop(MetaLinda ml) { + ml.in(TupleId.STOP.id, + new PSXCallback() { public void callback(ByteBuffer reply) { + mee.running = false; }}); + mee.removeMetaEngine(this); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/FDLServWithSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/FDLServWithSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,23 @@ +package fdl.old.test.metaTransfer; + +import java.io.IOException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; + + +public class FDLServWithSend extends FDLindaServ { + int port1; + public FDLServWithSend(int port,int port1) throws IOException { + super(port); + this.port1 = port1; + } + + @Override public void mainLoop() { + System.out.println("change mainloop"); + MetaLinda ml = new MetaLinda(this.tupleSpace,this); + MetaProtocolEngine mpe = new MetaProtocolEngine(ml,"MetaEngine", port1); + mpe.mainLoop(ml); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/MetaProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/MetaProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,95 @@ +package fdl.old.test.metaTransfer; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class MetaProtocolEngine implements MetaEngine { + private int id = 10; + private boolean running = true; + + private PSXLinda psx; + private String name; + MetaLinda fdl1; + String host = "127.0.0.1"; + int port1; + private PSXLinda psx1; + private ByteBuffer data2 = ByteBuffer.allocate(10); + private int count = 1; + + + public MetaProtocolEngine(MetaLinda ml, String string, int port1) { + this.name = string; + this.port1 = port1; + this.fdl1 = ml; + } + + public void mainLoop(MetaLinda ml){ + boolean connect = true; + System.out.println("MetaEngine Start"); + while(connect){ + try { + initConnect(); + transfer(); + connect = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + private void initConnect(){ + System.out.println(name); + boolean connectpsx = true; + while(connectpsx){ + try { + psx = fdl1.open(host,port1); + connectpsx = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } + } + System.out.println("Connect "+port1); + psx1 = fdl1; + } + + private void transfer() throws IOException { + PSXReply in = psx.in(id); + System.out.println("PSXReply "+port1); + while (running) { + if(in.ready()){ + data2 = in.getData(); + int i = data2.getInt(); + data2.rewind(); + //outしたbytebufferの変更をこれ以降やっちゃいけない + psx1.out(id,data2); + + System.out.println("IntData0 "+port1 +i); + fdl1.sync(0); + if (count++>3) { + running = false; + break; + } + System.out.println("IntData1 "+port1 +i); + } + fdl1.sync(0); + } + assertEquals(4,count); + } + + +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/MetaProtocolEngine_not.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/MetaProtocolEngine_not.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,69 @@ +package fdl.old.test.metaTransfer; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class MetaProtocolEngine_not implements MetaEngine { + private FederatedLinda fdlmeta; + private ByteBuffer data = ByteBuffer.allocate(1024); + private int id = 10; + private boolean running = true; + + //ここからsend用宣言 + private PSXLinda sendpsx,getpsx; + private String host = "127.0.0.1"; + private int port; + + public MetaProtocolEngine_not(MetaLinda fdlmeta, int port) { + this.port = port; + } + + public void metaOpen(int sendport){ + try{ + sendpsx = fdlmeta.open(host, sendport); + }catch (IOException e) { + e.printStackTrace(); + } + } + + //meta部分のデータ取得 + public void metaTrans() { + try { + getpsx = fdlmeta.open(host, 10001); + } catch (IOException e) { + e.printStackTrace(); + } + PSXReply in = getpsx.in(id); + while (running) { + System.out.println(in); + System.out.println(in.ready()); + if(in.ready()) { + data = in.getData(); + sendpsx.out(id, data); + running = false; + break; + } + } + } + + + public void mainLoop(MetaLinda ml) { + System.out.println("MetaProtocolEngine Start"); + fdlmeta = FederatedLinda.init(); + if( port == 10002 ) { + metaOpen(10003); + metaTrans(); + fdlmeta.sync(); + System.out.println("MetaProtocolEngine Connect"); + } + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/FDLServWithSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/FDLServWithSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,22 @@ +package fdl.old.test.metaTransfer.Ring; + +import java.io.IOException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; + +public class FDLServWithSend extends FDLindaServ { + int port1; + public FDLServWithSend(int port,int port1) throws IOException { + super(port); + this.port1 = port1; + } + + @Override public void mainLoop() { + System.out.println("change mainloop"); + MetaLinda ml = new MetaLinda(this.tupleSpace,this); + MetaProtocolEngine mpe = new MetaProtocolEngine(ml,"MetaEngine", port1, port1); + mpe.mainLoop(ml); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/MetaProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/MetaProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,100 @@ +package fdl.old.test.metaTransfer.Ring; + +//import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class MetaProtocolEngine implements MetaEngine { + int id = 10; + private boolean running = true; + + private PSXLinda psx; + private String name; + MetaLinda fdl1; + String host = "127.0.0.1"; + int port1; + private PSXLinda psx1; + private ByteBuffer data = ByteBuffer.allocate(10); +// private int count = 1; + int port; + + + public MetaProtocolEngine(MetaLinda ml, String string, int port1, int port) { + this.name = string; + this.port1 = port1; + this.fdl1 = ml; + this.port = port; + } + + public void mainLoop(MetaLinda ml){ + boolean connect = true; + System.out.println("MetaEngine Start"); + while(connect){ + try { + initConnect(); + transfer(psx, psx1); + connect = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + private void initConnect() throws IOException{ + System.out.println(name); + //自分 + psx = fdl1; + if (port == 10001){ + sendData(); + } + //相手 + psx1 = fdl1.open(host,port1); + System.out.println("Connect "+port1); + } + + private void sendData() throws IOException{ + boolean connectSend = true; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + while(connectSend){ + psx.out(id, send); + fdl1.sync(1); + System.out.println("Send Data 10001"); + connectSend = false; + } + } + + private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { + PSXReply in = getpsx.in(id); + System.out.println("PSXReply "+port1); + while (running) { + if(in.ready()){ + data = in.getData(); + int i = data.getInt(); + data.rewind(); + //outしたbytebufferの変更をこれ以降やっちゃいけない + sendpsx.out(id,data); + + System.out.println("IntData0 "+port1 +i); + fdl1.sync(1); + running = false; + System.out.println("IntData1 "+port1 +i); + break; + } + fdl1.sync(0); + } +// assertEquals(4,count); + } + +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,39 @@ +package fdl.old.test.metaTransfer.Ring; + +import java.io.IOException; + +import fdl.FDLindaServ; + +public class Server extends Thread { + int port1; + int port2; + int set; + private String name; + + public Server(String string, int i, int t, int s) { + port1 = i; + port2 = t; + name = string; + set = s; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port1)}; + System.out.println(name); + switch (set){ + case 0: + try { + FDLServWithSend send = new FDLServWithSend(port1,port2); + send.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + break; + + case 1: + FDLindaServ.main(args); + break; + } + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,47 @@ +package fdl.old.test.metaTransfer.Ring; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + + public void run(){ + int id = 10; + int port = 10001; + boolean connect = true; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + while(connect){ + try { + sendData(id, port, host, send); + connect = false; + } catch(IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } + + } + } + + private void sendData(int id, int port, String host, ByteBuffer send) throws IOException { + fdlsend = FederatedLinda.init(); + + psxsend = fdlsend.open(host,port); + System.out.println("Connect Host1"); + psxsend.out(id, send); + fdlsend.sync(1); + System.out.println("Send Data"); + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/TestTransfer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/TestTransfer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,29 @@ +package fdl.old.test.metaTransfer.Ring; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +public class TestTransfer { + @Test + public void testTransfer() throws IOException { + int port1 = 10001; + int port2 = 10002; + + Server server1 = new Server("Server1",port1, port2,0); + server1.start(); + + Server server2 = new Server("Server2",port2, port1,1); + server2.start(); + + try { + server2.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + assertEquals(1,1); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/TestTransferRing.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/TestTransferRing.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,40 @@ +package fdl.old.test.metaTransfer.Ring; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestTransferRing { + Server[] servers; + final int serverCount = 3; + final int startPort = 10001; + @Test + public void testTransfer() { + int port = startPort; + + servers = new Server[serverCount]; + + for (int i = 0; i < serverCount; i++) { + servers[i] = new Server("Server"+(i+1),port+i,netporot(port+i),0); + servers[i].start(); + } + +// try { +// servers[3].join(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + + + assertEquals(1,1); + } + private int netporot(int i) { + i++; + if(i >= startPort+serverCount) { + return startPort; + } + + return i; + } +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Ring/TestTransferThree.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Ring/TestTransferThree.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,22 @@ +package fdl.old.test.metaTransfer.Ring; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestTransferThree { + @Test + public void testTransfer() { + + Server server1 = new Server("Server1", 10001, 10002, 0); + server1.start(); + Server server2 = new Server("Server2", 10002, 10001, 0); + server2.start(); +// Server server3 = new Server("Server3", 10003, 10001, 0); +// server3.start(); + + assertEquals(1,1); + } + +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,20 @@ +package fdl.old.test.metaTransfer; + +import fdl.FDLindaServ; + +public class Server extends Thread { + int port; + private String name; + + public Server(String string, int i) { + port = i; + name = string; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + System.out.println(name); + FDLindaServ.main(args); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,47 @@ +package fdl.old.test.metaTransfer; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + + public void run(){ + int id = 10; + int port = 10001; + boolean connect = true; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + while(connect){ + try { + sendData(id, port, host, send); + connect = false; + } catch(IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } + + } + } + + private void sendData(int id, int port, String host, ByteBuffer send) throws IOException { + fdlsend = FederatedLinda.init(); + + psxsend = fdlsend.open(host,port); + System.out.println("Connect Host1"); + psxsend.out(id, send); + fdlsend.sync(1); + System.out.println("Send Data"); + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/metaTransfer/TestTransfer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/metaTransfer/TestTransfer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,27 @@ +package fdl.old.test.metaTransfer; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +public class TestTransfer { + @Test + public void testTransfer() throws IOException { + int port1 = 10001; + int port2 = 10002; + +// Server server1 = new Server("Server1",port1); +// server1.start(); +// + Server server2 = new Server("Server2",port2); + server2.start(); + + + FDLServWithSend send = new FDLServWithSend(port1, port2); + send.mainLoop(); + + assertEquals(1,1); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/ProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/ProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,74 @@ +package fdl.old.test.transfer; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class ProtocolEngine extends Thread{ + private PSXLinda psx; + private String name; + FederatedLinda fdl1; + String host = "127.0.0.1"; + int port1 = 10001; + int port2 = 10002; + private int id = 10; + private PSXLinda psx1; + private boolean running = true; + private ByteBuffer data2 = ByteBuffer.allocate(10); + + public ProtocolEngine(String string, int port1, int port2) { + this.name = string; + this.port1 = port1; + this.port2 = port2; + } + + public void run(){ + fdl1 = FederatedLinda.init(); + try { + System.out.println(name); + psx = fdl1.open(host,port1); + System.out.println("Connect Host1"); + psx1 = fdl1.open(host,port2); + System.out.println("Connect Host2"); + // Host1にデータを送信する。 + testSend(psx); + // psxにデータを用意 + transfer(psx,psx1); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void testSend(PSXLinda psx2) throws IOException { + ByteBuffer send = ByteBuffer.allocate(10); + send.putInt(12); + send.flip(); + psx2.out(id, send); + psx2.sync(1); + } + + + private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { + PSXReply in = getpsx.in(id); + while (running) { + System.out.println(in.ready()); + if(in.ready()){ + //psx1にデータを書き出し + data2 = in.getData(); + sendpsx.out(id,data2); + //runningフラグをfalseする + running = false; + fdl1.sync(0); + break; + } + fdl1.sync(); + } + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,20 @@ +package fdl.old.test.transfer; + +import fdl.FDLindaServ; + +public class Server extends Thread{ + int port; + private String name; + + public Server(String string, int i) { + port = i; + name = string; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + System.out.println(name); + FDLindaServ.main(args); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,33 @@ +package fdl.old.test.transfer; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + + public void run(){ + int id = 10; + int port = 10001; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + fdlsend = FederatedLinda.init(); + try{ + psxsend = fdlsend.open(host,port); + System.out.println("Connect Host1"); + psxsend.out(id, send); + fdlsend.sync(); + System.out.println("Send Data"); + }catch (IOException e) { + System.out.println("例外:" + e); + } + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/TestTransfer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/TestTransfer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,25 @@ +package fdl.old.test.transfer; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestTransfer { + @Test + public void testTransfer() { + int port1 = 10001; + int port2 = 10002; + Server server1 = new Server("Server1",port1); + server1.start(); + Server server2 = new Server("Server2",port2); + server2.start(); + ProtocolEngine pe = new ProtocolEngine("ProtocolEngine",port1,port2); + pe.start(); + try { + pe.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + assertEquals(1,1); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/FDLServWithSendMeta.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/FDLServWithSendMeta.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,91 @@ +package fdl.old.test.transfer.cluster; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; + + +public class FDLServWithSendMeta extends FDLindaServ { + + public FDLServWithSendMeta(int port) throws IOException { + super(port); + } + + static String nextHost = null; + private static int localport = 10000; + static int chknum; + private static String bufstring; + private static int bufsize; + private static String txt; + private static int ring; + + @Override public void mainLoop() { + MetaLinda ml = new MetaLinda(tupleSpace, this); + MetaProtocolEngine mpe = new MetaProtocolEngine(ml, nextHost, chknum, bufsize, txt); + mpe.mainLoop(ml); + } + + public static void main(String[] args){ + for (int k=0; k "+start); + } catch (IOException e) { + e.printStackTrace(); + } + } + boolean connect = true; + while(connect){ + try { + initConnect(); + transfer = DF.format(new Date()); + transferlong = new Date().getTime(); + System.out.println("transfer Time => "+transfer); + for(int l = 0; l < 1000; l++) { + transfer(); + } + dataChk(); + write(); + connect = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + private void initConnect(){ + boolean connectpsx = true; + while(connectpsx){ + try { + psxsend = fdlmeta.open(host, port); + connectpsx = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } + } + } + + private void sendData() throws IOException{ + boolean connectSend = true; + ByteBuffer send = ByteBuffer.allocate(bufsize); + byte[] fdata = new byte[bufsize]; + // fdata = InetAddress.getLocalHost().getAddress(); + for(int n=0; n < fdata.length; n++ ){ + fdata[n] = 1; + } + send.put(fdata); + send.flip(); + while(connectSend){ + psxget.out(id, send); + psxget.sync(1); + System.out.println("Send Data"); + connectSend = false; + } + } + + public void transfer() throws IOException { + ByteBuffer data = ByteBuffer.allocate(bufsize); + boolean running = true; + PSXReply in = psxget.in(id); + while (running) { + if(in.ready()){ + data = in.getData(); + psxsend.out(id,data); + System.out.println("connect to => "+host); + psxsend.sync(1); + running = false; + break; + } + fdlmeta.sync(0); + } + } + + public void dataChk() throws IOException{ + fdlmeta.sync(1); + boolean running2 = true; + // ByteBuffer data2 = ByteBuffer.allocate(bufsize); + PSXReply in2 = psxget.in(id); + // byte[] fdata2 = new byte[bufsize]; + // byte[] fdata3 = new byte[bufsize]; + // fdata = InetAddress.getLocalHost().getAddress(); + // for(int n=0; n < fdata2.length; n++ ){ + // fdata2[n] = 1; + // } + while (running2) { + psxget.sync(1); + if(in2.ready()) { + // data2 = in2.getData(); + // data2.get(fdata3); + // if (fdata2 == fdata3){ + time = DF.format(new Date()); + timelong = new Date().getTime(); + System.out.println("Around Time => "+time); + // } + running2 = false; + break; + } + } + } + + + public void write() throws IOException{ + long result = (timelong - transferlong); + String diff = String.valueOf(result); + File read = new File("resultTime"+bufsize+"_"+txt+".txt"); + read.createNewFile(); + FileWriter filewrite = new FileWriter(read); + if (start != "null"){ + filewrite.write("ServerStart => "+start+"\r\n"); + } + if (transfer != "null") { + filewrite.write("TransferTime => "+transfer+"\r\n"); + } + if (time != "null"){ + filewrite.write("AroundTime => "+time+"\r\n"); + } + if (diff != "null"){ + filewrite.write("DiffTime => "+diff+"\r\n"); + } + filewrite.close(); + } +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/ProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/ProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,147 @@ +package fdl.old.test.transfer.cluster; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.text.SimpleDateFormat; +import java.util.Date; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class ProtocolEngine extends Thread{ + static int id = 10; + static FederatedLinda fdl; + static PSXLinda getpsx; + static PSXLinda sendpsx; + static int port = 10000; + String getHost = null; + String sendHost = null; + private int chknum; + private int bufsize; + private String txt; + SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); + private String time; + private long timelong; + private String start; + private String transfer; + private long transferlong; + + public ProtocolEngine(String gethost, String sendhost, int chknum, int bufsize, String txt) { + this.getHost = gethost; + this.sendHost = sendhost; + this.chknum = chknum; + this.bufsize = bufsize; + this.txt = txt; + } + + public void run(){ + fdl = FederatedLinda.init(); + try { + getpsx = fdl.open(getHost,port); + System.out.println("Connect Host1"); + sendpsx = fdl.open(sendHost,port); + System.out.println("Connect Host2"); + // Host1にデータを送信する。 + if(chknum == 1){ + testSend(getpsx); + start = DF.format(new Date()); + System.out.println("Start Time => "+start); + } + // psxにデータを用意 + transfer(getpsx,sendpsx); + dataChk(); + write(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void write() throws IOException { + long result = (timelong - transferlong); + String diff = String.valueOf(result); + File read = new File("resultTime"+bufsize+"_"+txt+".txt"); + read.createNewFile(); + FileWriter filewrite = new FileWriter(read); + if (start != "null"){ + filewrite.write("ServerStart => "+start+"\r\n"); + } + if (transfer != "null") { + filewrite.write("TransferTime => "+transfer+"\r\n"); + } + if (time != "null"){ + filewrite.write("AroundTime => "+time+"\r\n"); + } + if (diff != "null"){ + filewrite.write("DiffTime => "+diff+"\r\n"); + } + filewrite.close(); + } + + private void dataChk() throws IOException { + fdl.sync(1); + boolean running2 = true; + ByteBuffer data3 = ByteBuffer.allocate(bufsize); + PSXReply in2 = getpsx.in(id); + int localhost2 = InetAddress.getLocalHost().hashCode(); + while (running2) { + getpsx.sync(1); + if(in2.ready()) { + data3 = in2.getData(); + int i = data3.getInt(); + if (i == localhost2){ + time = DF.format(new Date()); + timelong = new Date().getTime(); + System.out.println("Around Time => "+time); + } + running2 = false; + break; + } + } + + } + + private void testSend(PSXLinda psx2) throws IOException { + boolean connectSend = true; + ByteBuffer send = ByteBuffer.allocate(bufsize); + int localhost = InetAddress.getLocalHost().hashCode(); + send.putInt(localhost); + send.flip(); + while(connectSend){ + psx2.out(id, send); + psx2.sync(1); + System.out.println("Send Data"); + connectSend = false; + } + } + + + private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { + ByteBuffer data2 = ByteBuffer.allocate(bufsize); + PSXReply in = getpsx.in(id); + boolean running = true; + while (running) { + if(in.ready()){ + //psx1にデータを書き出し + data2 = in.getData(); + sendpsx.out(id,data2); + //runningフラグをfalseする + running = false; + transfer = DF.format(new Date()); + transferlong = new Date().getTime(); + System.out.println("transfer Time => "+transfer); + System.out.println("connect to => "+sendHost); + sendpsx.sync(1); + break; + } + fdl.sync(); + } + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/ProtocolEngineMain.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/ProtocolEngineMain.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,72 @@ +package fdl.old.test.transfer.cluster; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.text.SimpleDateFormat; +import java.util.Date; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class ProtocolEngineMain { + static int id = 10; + static FederatedLinda fdl; + static PSXLinda getpsx; + static PSXLinda sendpsx; + static int port = 10000; + private static String getHost = null; + private static String sendHost = null; + + public static void main(String[] args) { + for (int i=0; i "+i); + //runningフラグをfalseする + running = false; + sendpsx.sync(1); + System.out.println("Transfer Data"); + break; + } + SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); + System.out.println(DF.format(new Date())); + fdl.sync(); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,16 @@ +package fdl.old.test.transfer.cluster; + +import fdl.FDLindaServ; + +public class Server extends Thread{ + int port; + + public Server() { + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + FDLindaServ.main(args); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/ServerMain.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/ServerMain.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,12 @@ +package fdl.old.test.transfer.cluster; + +import fdl.FDLindaServ; + +public class ServerMain{ + public static void main(String[] args){ + int port = 10000; + String[] args1 = {"-p",Integer.toString(port)}; + FDLindaServ.main(args1); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/TestGet.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/TestGet.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,48 @@ +package fdl.old.test.transfer.cluster; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestGet extends Thread { + private PSXLinda psxget; + private FederatedLinda fdlget; + private int port; + String host = "127.0.0.1"; + private ByteBuffer data = ByteBuffer.allocate(1024); + + public TestGet(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + fdlget = FederatedLinda.init(); + try { + System.out.println("TestGet Start"); + psxget = fdlget.open(host, port); + System.out.println("Host1 connected"); + //getにidのタプルを取り出す + PSXReply dataGet = psxget.in(id); + System.out.println("dataget"); + System.out.println(dataGet.ready()); + if(dataGet.ready()){ + System.out.println(dataGet); + data = dataGet.getData(); + fdlget.sync(1); + i = data.getInt(); + System.out.println(i); + } + } catch (IOException e) { + try { + Thread.sleep(10); + } catch (InterruptedException e1) { + } + } + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/cluster/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/cluster/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,46 @@ +package fdl.old.test.transfer.cluster; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend { + static PSXLinda psxsend; + static FederatedLinda fdlsend; + private static int port = 10000; + + public static void main(String[] args){ + int id = 10; + int i; + String host = "cls001.cs.ie.u-ryukyu.ac.jp"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + i = send.getInt(); + send.rewind(); + fdlsend = FederatedLinda.init(); + boolean connect = true; + while(connect) { + try{ + System.out.println("TestSend Start"); + psxsend = fdlsend.open(host,port); + System.out.println("Set Data = " +i); + System.out.println("Connect Host1"); + psxsend.out(id, send); + System.out.println("regist Que"); + fdlsend.sync(); + System.out.println("Send Data"); + connect = false; + }catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/three/ProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/three/ProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,82 @@ +package fdl.old.test.transfer.three; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class ProtocolEngine extends Thread{ + private PSXLinda psx; + private String name; + FederatedLinda fdl1; + String host = "127.0.0.1"; + int port1; + int port2; + private int id = 10; + private PSXLinda psx1; + private boolean running = true; + private ByteBuffer data2 = ByteBuffer.allocate(10); + private int count = 1; + + public ProtocolEngine(String string, int port1, int port2) { + this.name = string; + this.port1 = port1; + this.port2 = port2; + } + + public void run(){ + fdl1 = FederatedLinda.init(); + boolean connect = true; + while(connect){ + try { + initConnect(); + transfer(); + connect = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + private void initConnect() throws IOException { + System.out.println(name); + psx = fdl1.open(host,port1); + System.out.println("Connect "+port1); + psx1 = fdl1.open(host,port2); + System.out.println("Connect "+port2); + } + + private void transfer() throws IOException { + PSXReply in = psx.in(id); + System.out.println("PSXReply "+port1 +": "+port2 +": "); + while (running) { + if(in.ready()){ + data2 = in.getData(); + int i = data2.getInt(); + data2.rewind(); + //outしたbytebufferの変更をこれ以降やっちゃいけない + psx1.out(id,data2); + + System.out.println("IntData0 "+port1 +": "+port2 +": " +i); + fdl1.sync(1); + if (count++>3) { + running = false; + break; + } + System.out.println("IntData1 "+port1 +": "+port2 +": " +i); + } + fdl1.sync(); + } + assertEquals(4,count); + } + + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/three/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/three/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,20 @@ +package fdl.old.test.transfer.three; + +import fdl.FDLindaServ; + +public class Server extends Thread { + int port; + private String name; + + public Server(String string, int i) { + port = i; + name = string; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + System.out.println(name+"start"); + FDLindaServ.main(args); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/three/TestGet.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/three/TestGet.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,55 @@ +package fdl.old.test.transfer.three; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestGet extends Thread { + PSXLinda psxget; + FederatedLinda fdlget; + private int port; + String host = "127.0.0.1"; + private ByteBuffer data = ByteBuffer.allocate(10); + + public TestGet(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + fdlget = FederatedLinda.init(); + boolean connecttest = true; + while(connecttest){ + try { + getData(id); + i = data.getInt(); + System.out.println(i); + connecttest = false; + } catch (IOException e) { + e.printStackTrace(); + connecttest = true; + } + } + } + + private void getData(int id) throws IOException { + psxget = fdlget.open(host, port); + PSXReply get = psxget.in(id); + boolean running = false; + while(running){ + if(get.ready()){ + data = get.getData(); + running = false; + fdlget.sync(0); + break; + } + fdlget.sync(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/three/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/three/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,48 @@ +package fdl.old.test.transfer.three; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + private int port; + + public TestSend(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + i = send.getInt(); + send.rewind(); + fdlsend = FederatedLinda.init(); + boolean connect = true; + while(connect) { + try{ + psxsend = fdlsend.open(host,port); + System.out.println("Set Data = " +i); + System.out.println("Connect Host1"); + psxsend.out(id, send); + System.out.println("regist Que"); + fdlsend.sync(1); + System.out.println("Send Data"); + connect = false; + }catch (IOException e) { + try { + Thread.sleep(20); + } catch (InterruptedException e1) { + } + } + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/three/TestTransfer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/three/TestTransfer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,52 @@ +package fdl.old.test.transfer.three; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + + +public class TestTransfer { + Server[] servers; + ProtocolEngine[] engines; + final int serverCount = 3; + final int startPort = 10011; + @Test + public void testTransfer() { + int port = startPort; + servers = new Server[serverCount]; + engines = new ProtocolEngine[serverCount]; + + for (int i = 0; i < serverCount; i++) { + servers[i] = new Server("Server"+(i+1),port+i); + servers[i].start(); + } + + TestSend send = new TestSend(startPort); + send.start(); + try{ + send.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + for (int i = 0; i < serverCount; i++) { + engines[i] = new ProtocolEngine("ProtocolEngine "+(i+1) +":port " +(port+i) +":port "+(netporot(port+i)),port+i,netporot(port+i)); + engines[i].start(); + } + try { + for (ProtocolEngine e: engines){ + e.join(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + assertEquals(1,1); + } + private int netporot(int i) { + i++; + if(i >= startPort+serverCount) { + return startPort; + } + return i; + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/two/ProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/two/ProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,67 @@ +package fdl.old.test.transfer.two; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class ProtocolEngine extends Thread{ + private static PSXLinda psx; + private static String name; + static FederatedLinda fdl1; + static String host = "127.0.0.1"; + static int port1 = 10001; + static int port2 = 10002; + private static int id = 10; + private static PSXLinda psx1; + private static boolean running; + private static ByteBuffer data2 = ByteBuffer.allocate(10); + + public static void main(String[] args){ + fdl1 = FederatedLinda.init(); + try { + System.out.println(name); + psx = fdl1.open(host,port1); + System.out.println("Connect Host1"); + psx1 = fdl1.open(host,port2); + System.out.println("Connect Host2"); + // Host1にデータを送信する。 + testSend(psx); + // psxにデータを用意 + transfer(psx,psx1); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void testSend(PSXLinda psx2) throws IOException { + ByteBuffer send = ByteBuffer.allocate(10); + send.putInt(12); + send.flip(); + psx2.out(id, send); + psx2.sync(0); + } + + + private static void transfer(PSXLinda psx2, PSXLinda psx12) throws IOException { + PSXReply in = psx2.in(id); + while (running) { + if(in.ready()){ + //psx1にデータを書き出し + data2 = in.getData(); + psx12.out(id,data2); + //runningフラグをfalseする + running = false; + fdl1.sync(0); + break; + } + fdl1.sync(); + } + + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/two/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/two/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,20 @@ +package fdl.old.test.transfer.two; + +import fdl.FDLindaServ; + +public class Server extends Thread { + int port; + private String name; + + public Server(String string, int i) { + port = i; + name = string; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + System.out.println(name); + FDLindaServ.main(args); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/two/TestGet.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/two/TestGet.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,44 @@ +package fdl.old.test.transfer.two; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestGet extends Thread { + private static PSXLinda psxget; + private static FederatedLinda fdlget; + private static int port = 10001; + static String host = "127.0.0.1"; + private static ByteBuffer data = ByteBuffer.allocate(1024); + + public static void main(String[] args){ + int id = 10; + int i; + fdlget = FederatedLinda.init(); + try { + System.out.println("TestGet Start"); + psxget = fdlget.open(host, port); + System.out.println("Host1 connected"); + //getにidのタプルを取り出す + PSXReply dataGet = psxget.in(id); + System.out.println("dataget"); + System.out.println(dataGet.ready()); + if(dataGet.ready()){ + System.out.println(dataGet); + data = dataGet.getData(); + fdlget.sync(1); + i = data.getInt(); + System.out.println(i); + } + } catch (IOException e) { + try { + Thread.sleep(10); + } catch (InterruptedException e1) { + } + } + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/two/TestSend.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/two/TestSend.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,50 @@ +package fdl.old.test.transfer.two; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + private int port; + + public TestSend(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + i = send.getInt(); + send.rewind(); + fdlsend = FederatedLinda.init(); + boolean connect = true; + while(connect) { + try{ + System.out.println("TestSend Start"); + psxsend = fdlsend.open(host,port); + System.out.println("Set Data = " +i); + System.out.println("Connect Host1"); + psxsend.out(id, send); + System.out.println("regist Que"); + fdlsend.sync(1); + System.out.println("Send Data"); + connect = false; + }catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test/transfer/two/TestTransfer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test/transfer/two/TestTransfer.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,21 @@ +package fdl.old.test.transfer.two; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class TestTransfer { + @Test + public void testTransfer() throws InterruptedException { + int port = 10001; + Server server = new Server("Server1",port); + server.start(); + server.join(); + TestSend send = new TestSend(port); + send.start(); + + send.join(); + + assertEquals(1,1); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/CountTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/CountTest.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,42 @@ +package fdl.old.test2; + +class CountA implements Runnable { + + public void run() { + for (int i=0; i <= 5; i++) { + System.out.println("A: " + i); + } + } +} + +class CountB implements Runnable { + + public void run() { + for (int i=5; i >= 0; i--) { + System.out.println(" B: " + i); + } + } +} + +class CountTest { + public static void main(String[] args) { + // ランナブルクラスのインスタンス化 + CountA runA = new CountA(); + CountB runB = new CountB(); + System.out.println("Runnable Class のインスタンス化終了"); + + // スレッドのインスタンス化 + Thread threadA = new Thread(runA); + Thread threadB = new Thread(runB); + System.out.println("Thread へ受け渡し終了"); + + // スレッドの開始 + threadA.start(); + threadB.start(); + System.out.println("Thread の start() 終了"); + System.out.println("適時自動的に run() が実行される"); + + + } +} + diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/Server.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/Server.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,35 @@ +package fdl.old.test2; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; + +public class Server extends Thread{ + static int Port = 10007; + + public void run(){ + try { + ServerSocket server = new ServerSocket(Port); + Socket sock =null; + System.out.println("Server Start"); + sock = server.accept(); + System.out.println("Connect"); + PrintWriter ps = new PrintWriter(sock.getOutputStream()); + Date d = new Date(); + ps.print(d + "\r\n"); + ps.flush(); + sock.close(); // クライアントからの接続を切断 + System.out.println("Connection Closed"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void main(String[] args) { + Server sv = new Server(); + sv.start(); + } +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/TestRead.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/TestRead.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,26 @@ +package fdl.old.test2; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class TestRead { + + public static void main(String[] args) { + try { + File inputFile = new File(args[0]); + FileReader in = new FileReader(inputFile); + BufferedReader br = new BufferedReader(in); + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + } + br.close(); + in.close(); + } catch (IOException e) { + System.out.println(e); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/TestRing.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/TestRing.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,51 @@ +package fdl.old.test2; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class TestRing { + static int id; + public static void main(String args[]){ + + try{ + FederatedLinda fdl; + PSXLinda psx; + String host = "127.0.0.1"; + PSXReply r; + + FileReader in = new FileReader("nodelist"); + BufferedReader br = new BufferedReader(in); + String line; + + while ((line = br.readLine()) != null) { + int i1 = Integer.parseInt(line); + fdl = FederatedLinda.init(); + psx = fdl.open(host,i1); + r = psx.in(65535); + fdl.sync(1); + + ByteBuffer data = ByteBuffer.allocate(10); + data.putInt(10); + data.flip(); + + psx.out(1,data); + while(!r.ready()){ + psx.sync(0); + } + } + br.close(); + in.close(); + + + }catch (IOException e) { + System.err.println("Communication failure."); + } + + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/client.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/client.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,26 @@ +package fdl.old.test2; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Socket; + +public class client { + public static void main(String arg[]) throws IOException{ + String www = arg[0]; + int port = 10007; + Socket sock; + BufferedReader dis; +// PrintWriter ps; + sock = new Socket(www,port); + dis = new BufferedReader(new InputStreamReader(sock.getInputStream())); +// ps = new PrintWriter(sock.getOutputStream(),true); +// ps.println("Get"+arg[1]); +// String s = null; +// while((s = dis.readLine()) != null){ + System.out.println(dis); +// } + sock.close(); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/old/test2/nodelist --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/old/test2/nodelist Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,2 @@ +10000 +10001 diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/protobuf/Aqua.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/protobuf/Aqua.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,758 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: aquarium.proto + +package fdl.protobuf; + +public final class Aqua { + private Aqua() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + } + public static final class Width extends + com.google.protobuf.GeneratedMessage { + // Use Width.newBuilder() to construct. + private Width() { + initFields(); + } + private Width(boolean noInit) {} + + private static final Width defaultInstance; + public static Width getDefaultInstance() { + return defaultInstance; + } + + public Width getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return fdl.protobuf.Aqua.internal_static_aqua_Width_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return fdl.protobuf.Aqua.internal_static_aqua_Width_fieldAccessorTable; + } + + // required int32 width = 1; + public static final int WIDTH_FIELD_NUMBER = 1; + private boolean hasWidth; + private int width_ = 0; + public boolean hasWidth() { return hasWidth; } + public int getWidth() { return width_; } + + private void initFields() { + } + public final boolean isInitialized() { + if (!hasWidth) return false; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (hasWidth()) { + output.writeInt32(1, getWidth()); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (hasWidth()) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, getWidth()); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + public static fdl.protobuf.Aqua.Width parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.Aqua.Width parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input, extensionRegistry)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.Aqua.Width parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.Aqua.Width parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(fdl.protobuf.Aqua.Width prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder { + private fdl.protobuf.Aqua.Width result; + + // Construct using fdl.protobuf.Aqua.Width.newBuilder() + private Builder() {} + + private static Builder create() { + Builder builder = new Builder(); + builder.result = new fdl.protobuf.Aqua.Width(); + return builder; + } + + protected fdl.protobuf.Aqua.Width internalGetResult() { + return result; + } + + public Builder clear() { + if (result == null) { + throw new IllegalStateException( + "Cannot call clear() after build()."); + } + result = new fdl.protobuf.Aqua.Width(); + return this; + } + + public Builder clone() { + return create().mergeFrom(result); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return fdl.protobuf.Aqua.Width.getDescriptor(); + } + + public fdl.protobuf.Aqua.Width getDefaultInstanceForType() { + return fdl.protobuf.Aqua.Width.getDefaultInstance(); + } + + public boolean isInitialized() { + return result.isInitialized(); + } + public fdl.protobuf.Aqua.Width build() { + if (result != null && !isInitialized()) { + throw newUninitializedMessageException(result); + } + return buildPartial(); + } + + private fdl.protobuf.Aqua.Width buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + if (!isInitialized()) { + throw newUninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return buildPartial(); + } + + public fdl.protobuf.Aqua.Width buildPartial() { + if (result == null) { + throw new IllegalStateException( + "build() has already been called on this Builder."); + } + fdl.protobuf.Aqua.Width returnMe = result; + result = null; + return returnMe; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof fdl.protobuf.Aqua.Width) { + return mergeFrom((fdl.protobuf.Aqua.Width)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(fdl.protobuf.Aqua.Width other) { + if (other == fdl.protobuf.Aqua.Width.getDefaultInstance()) return this; + if (other.hasWidth()) { + setWidth(other.getWidth()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder( + this.getUnknownFields()); + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + this.setUnknownFields(unknownFields.build()); + return this; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + this.setUnknownFields(unknownFields.build()); + return this; + } + break; + } + case 8: { + setWidth(input.readInt32()); + break; + } + } + } + } + + + // required int32 width = 1; + public boolean hasWidth() { + return result.hasWidth(); + } + public int getWidth() { + return result.getWidth(); + } + public Builder setWidth(int value) { + result.hasWidth = true; + result.width_ = value; + return this; + } + public Builder clearWidth() { + result.hasWidth = false; + result.width_ = 0; + return this; + } + + // @@protoc_insertion_point(builder_scope:aqua.Width) + } + + static { + defaultInstance = new Width(true); + fdl.protobuf.Aqua.internalForceInit(); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:aqua.Width) + } + + public static final class Position extends + com.google.protobuf.GeneratedMessage { + // Use Position.newBuilder() to construct. + private Position() { + initFields(); + } + private Position(boolean noInit) {} + + private static final Position defaultInstance; + public static Position getDefaultInstance() { + return defaultInstance; + } + + public Position getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return fdl.protobuf.Aqua.internal_static_aqua_Position_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return fdl.protobuf.Aqua.internal_static_aqua_Position_fieldAccessorTable; + } + + // required float x = 1; + public static final int X_FIELD_NUMBER = 1; + private boolean hasX; + private float x_ = 0F; + public boolean hasX() { return hasX; } + public float getX() { return x_; } + + // required float y = 2; + public static final int Y_FIELD_NUMBER = 2; + private boolean hasY; + private float y_ = 0F; + public boolean hasY() { return hasY; } + public float getY() { return y_; } + + // required float angle_x = 3; + public static final int ANGLE_X_FIELD_NUMBER = 3; + private boolean hasAngleX; + private float angleX_ = 0F; + public boolean hasAngleX() { return hasAngleX; } + public float getAngleX() { return angleX_; } + + // required float angle_y = 4; + public static final int ANGLE_Y_FIELD_NUMBER = 4; + private boolean hasAngleY; + private float angleY_ = 0F; + public boolean hasAngleY() { return hasAngleY; } + public float getAngleY() { return angleY_; } + + private void initFields() { + } + public final boolean isInitialized() { + if (!hasX) return false; + if (!hasY) return false; + if (!hasAngleX) return false; + if (!hasAngleY) return false; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (hasX()) { + output.writeFloat(1, getX()); + } + if (hasY()) { + output.writeFloat(2, getY()); + } + if (hasAngleX()) { + output.writeFloat(3, getAngleX()); + } + if (hasAngleY()) { + output.writeFloat(4, getAngleY()); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (hasX()) { + size += com.google.protobuf.CodedOutputStream + .computeFloatSize(1, getX()); + } + if (hasY()) { + size += com.google.protobuf.CodedOutputStream + .computeFloatSize(2, getY()); + } + if (hasAngleX()) { + size += com.google.protobuf.CodedOutputStream + .computeFloatSize(3, getAngleX()); + } + if (hasAngleY()) { + size += com.google.protobuf.CodedOutputStream + .computeFloatSize(4, getAngleY()); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + public static fdl.protobuf.Aqua.Position parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.Aqua.Position parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input, extensionRegistry)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.Aqua.Position parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.Aqua.Position parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(fdl.protobuf.Aqua.Position prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder { + private fdl.protobuf.Aqua.Position result; + + // Construct using fdl.protobuf.Aqua.Position.newBuilder() + private Builder() {} + + private static Builder create() { + Builder builder = new Builder(); + builder.result = new fdl.protobuf.Aqua.Position(); + return builder; + } + + protected fdl.protobuf.Aqua.Position internalGetResult() { + return result; + } + + public Builder clear() { + if (result == null) { + throw new IllegalStateException( + "Cannot call clear() after build()."); + } + result = new fdl.protobuf.Aqua.Position(); + return this; + } + + public Builder clone() { + return create().mergeFrom(result); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return fdl.protobuf.Aqua.Position.getDescriptor(); + } + + public fdl.protobuf.Aqua.Position getDefaultInstanceForType() { + return fdl.protobuf.Aqua.Position.getDefaultInstance(); + } + + public boolean isInitialized() { + return result.isInitialized(); + } + public fdl.protobuf.Aqua.Position build() { + if (result != null && !isInitialized()) { + throw newUninitializedMessageException(result); + } + return buildPartial(); + } + + private fdl.protobuf.Aqua.Position buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + if (!isInitialized()) { + throw newUninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return buildPartial(); + } + + public fdl.protobuf.Aqua.Position buildPartial() { + if (result == null) { + throw new IllegalStateException( + "build() has already been called on this Builder."); + } + fdl.protobuf.Aqua.Position returnMe = result; + result = null; + return returnMe; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof fdl.protobuf.Aqua.Position) { + return mergeFrom((fdl.protobuf.Aqua.Position)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(fdl.protobuf.Aqua.Position other) { + if (other == fdl.protobuf.Aqua.Position.getDefaultInstance()) return this; + if (other.hasX()) { + setX(other.getX()); + } + if (other.hasY()) { + setY(other.getY()); + } + if (other.hasAngleX()) { + setAngleX(other.getAngleX()); + } + if (other.hasAngleY()) { + setAngleY(other.getAngleY()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder( + this.getUnknownFields()); + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + this.setUnknownFields(unknownFields.build()); + return this; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + this.setUnknownFields(unknownFields.build()); + return this; + } + break; + } + case 13: { + setX(input.readFloat()); + break; + } + case 21: { + setY(input.readFloat()); + break; + } + case 29: { + setAngleX(input.readFloat()); + break; + } + case 37: { + setAngleY(input.readFloat()); + break; + } + } + } + } + + + // required float x = 1; + public boolean hasX() { + return result.hasX(); + } + public float getX() { + return result.getX(); + } + public Builder setX(float value) { + result.hasX = true; + result.x_ = value; + return this; + } + public Builder clearX() { + result.hasX = false; + result.x_ = 0F; + return this; + } + + // required float y = 2; + public boolean hasY() { + return result.hasY(); + } + public float getY() { + return result.getY(); + } + public Builder setY(float value) { + result.hasY = true; + result.y_ = value; + return this; + } + public Builder clearY() { + result.hasY = false; + result.y_ = 0F; + return this; + } + + // required float angle_x = 3; + public boolean hasAngleX() { + return result.hasAngleX(); + } + public float getAngleX() { + return result.getAngleX(); + } + public Builder setAngleX(float value) { + result.hasAngleX = true; + result.angleX_ = value; + return this; + } + public Builder clearAngleX() { + result.hasAngleX = false; + result.angleX_ = 0F; + return this; + } + + // required float angle_y = 4; + public boolean hasAngleY() { + return result.hasAngleY(); + } + public float getAngleY() { + return result.getAngleY(); + } + public Builder setAngleY(float value) { + result.hasAngleY = true; + result.angleY_ = value; + return this; + } + public Builder clearAngleY() { + result.hasAngleY = false; + result.angleY_ = 0F; + return this; + } + + // @@protoc_insertion_point(builder_scope:aqua.Position) + } + + static { + defaultInstance = new Position(true); + fdl.protobuf.Aqua.internalForceInit(); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:aqua.Position) + } + + private static com.google.protobuf.Descriptors.Descriptor + internal_static_aqua_Width_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_aqua_Width_fieldAccessorTable; + private static com.google.protobuf.Descriptors.Descriptor + internal_static_aqua_Position_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_aqua_Position_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\016aquarium.proto\022\004aqua\"\026\n\005Width\022\r\n\005width" + + "\030\001 \002(\005\"B\n\010Position\022\t\n\001x\030\001 \002(\002\022\t\n\001y\030\002 \002(\002" + + "\022\017\n\007angle_x\030\003 \002(\002\022\017\n\007angle_y\030\004 \002(\002B\024\n\014fd" + + "l.protobufB\004Aqua" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + internal_static_aqua_Width_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_aqua_Width_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_aqua_Width_descriptor, + new java.lang.String[] { "Width", }, + fdl.protobuf.Aqua.Width.class, + fdl.protobuf.Aqua.Width.Builder.class); + internal_static_aqua_Position_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_aqua_Position_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_aqua_Position_descriptor, + new java.lang.String[] { "X", "Y", "AngleX", "AngleY", }, + fdl.protobuf.Aqua.Position.class, + fdl.protobuf.Aqua.Position.Builder.class); + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + } + + public static void internalForceInit() {} + + // @@protoc_insertion_point(outer_class_scope) +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/protobuf/TupleClass.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/protobuf/TupleClass.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,385 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: classloader.proto + +package fdl.protobuf; + +public final class TupleClass { + private TupleClass() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + } + public static final class ClassFile extends + com.google.protobuf.GeneratedMessage { + // Use ClassFile.newBuilder() to construct. + private ClassFile() { + initFields(); + } + private ClassFile(boolean noInit) {} + + private static final ClassFile defaultInstance; + public static ClassFile getDefaultInstance() { + return defaultInstance; + } + + public ClassFile getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return fdl.protobuf.TupleClass.internal_static_tupleclassloader_ClassFile_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return fdl.protobuf.TupleClass.internal_static_tupleclassloader_ClassFile_fieldAccessorTable; + } + + // required string name = 1; + public static final int NAME_FIELD_NUMBER = 1; + private boolean hasName; + private java.lang.String name_ = ""; + public boolean hasName() { return hasName; } + public java.lang.String getName() { return name_; } + + // required bytes file = 2; + public static final int FILE_FIELD_NUMBER = 2; + private boolean hasFile; + private com.google.protobuf.ByteString file_ = com.google.protobuf.ByteString.EMPTY; + public boolean hasFile() { return hasFile; } + public com.google.protobuf.ByteString getFile() { return file_; } + + private void initFields() { + } + public final boolean isInitialized() { + if (!hasName) return false; + if (!hasFile) return false; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (hasName()) { + output.writeString(1, getName()); + } + if (hasFile()) { + output.writeBytes(2, getFile()); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (hasName()) { + size += com.google.protobuf.CodedOutputStream + .computeStringSize(1, getName()); + } + if (hasFile()) { + size += com.google.protobuf.CodedOutputStream + .computeBytesSize(2, getFile()); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.TupleClass.ClassFile parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + Builder builder = newBuilder(); + if (builder.mergeDelimitedFrom(input, extensionRegistry)) { + return builder.buildParsed(); + } else { + return null; + } + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static fdl.protobuf.TupleClass.ClassFile parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(fdl.protobuf.TupleClass.ClassFile prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder { + private fdl.protobuf.TupleClass.ClassFile result; + + // Construct using fdl.protobuf.TupleClass.ClassFile.newBuilder() + private Builder() {} + + private static Builder create() { + Builder builder = new Builder(); + builder.result = new fdl.protobuf.TupleClass.ClassFile(); + return builder; + } + + protected fdl.protobuf.TupleClass.ClassFile internalGetResult() { + return result; + } + + public Builder clear() { + if (result == null) { + throw new IllegalStateException( + "Cannot call clear() after build()."); + } + result = new fdl.protobuf.TupleClass.ClassFile(); + return this; + } + + public Builder clone() { + return create().mergeFrom(result); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return fdl.protobuf.TupleClass.ClassFile.getDescriptor(); + } + + public fdl.protobuf.TupleClass.ClassFile getDefaultInstanceForType() { + return fdl.protobuf.TupleClass.ClassFile.getDefaultInstance(); + } + + public boolean isInitialized() { + return result.isInitialized(); + } + public fdl.protobuf.TupleClass.ClassFile build() { + if (result != null && !isInitialized()) { + throw newUninitializedMessageException(result); + } + return buildPartial(); + } + + private fdl.protobuf.TupleClass.ClassFile buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + if (!isInitialized()) { + throw newUninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return buildPartial(); + } + + public fdl.protobuf.TupleClass.ClassFile buildPartial() { + if (result == null) { + throw new IllegalStateException( + "build() has already been called on this Builder."); + } + fdl.protobuf.TupleClass.ClassFile returnMe = result; + result = null; + return returnMe; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof fdl.protobuf.TupleClass.ClassFile) { + return mergeFrom((fdl.protobuf.TupleClass.ClassFile)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(fdl.protobuf.TupleClass.ClassFile other) { + if (other == fdl.protobuf.TupleClass.ClassFile.getDefaultInstance()) return this; + if (other.hasName()) { + setName(other.getName()); + } + if (other.hasFile()) { + setFile(other.getFile()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder( + this.getUnknownFields()); + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + this.setUnknownFields(unknownFields.build()); + return this; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + this.setUnknownFields(unknownFields.build()); + return this; + } + break; + } + case 10: { + setName(input.readString()); + break; + } + case 18: { + setFile(input.readBytes()); + break; + } + } + } + } + + + // required string name = 1; + public boolean hasName() { + return result.hasName(); + } + public java.lang.String getName() { + return result.getName(); + } + public Builder setName(java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + result.hasName = true; + result.name_ = value; + return this; + } + public Builder clearName() { + result.hasName = false; + result.name_ = getDefaultInstance().getName(); + return this; + } + + // required bytes file = 2; + public boolean hasFile() { + return result.hasFile(); + } + public com.google.protobuf.ByteString getFile() { + return result.getFile(); + } + public Builder setFile(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + result.hasFile = true; + result.file_ = value; + return this; + } + public Builder clearFile() { + result.hasFile = false; + result.file_ = getDefaultInstance().getFile(); + return this; + } + + // @@protoc_insertion_point(builder_scope:tupleclassloader.ClassFile) + } + + static { + defaultInstance = new ClassFile(true); + fdl.protobuf.TupleClass.internalForceInit(); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:tupleclassloader.ClassFile) + } + + private static com.google.protobuf.Descriptors.Descriptor + internal_static_tupleclassloader_ClassFile_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_tupleclassloader_ClassFile_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\021classloader.proto\022\020tupleclassloader\"\'\n" + + "\tClassFile\022\014\n\004name\030\001 \002(\t\022\014\n\004file\030\002 \002(\014B\032" + + "\n\014fdl.protobufB\nTupleClass" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + internal_static_tupleclassloader_ClassFile_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_tupleclassloader_ClassFile_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_tupleclassloader_ClassFile_descriptor, + new java.lang.String[] { "Name", "File", }, + fdl.protobuf.TupleClass.ClassFile.class, + fdl.protobuf.TupleClass.ClassFile.Builder.class); + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + } + + public static void internalForceInit() {} + + // @@protoc_insertion_point(outer_class_scope) +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/protobuf/classloader.proto --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/protobuf/classloader.proto Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,8 @@ +package tupleclassloader; +option java_package = "fdl.protobuf"; +option java_outer_classname = "TupleClass"; + +message ClassFile { + required string name = 1; + required bytes file = 2; +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/protobuf/gen.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/protobuf/gen.sh Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,2 @@ +#!/bin/sh +protoc --java_out=../../ classloader.proto diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/TestPSXLinda.java --- a/src/fdl/test/TestPSXLinda.java Wed Feb 08 18:02:00 2012 +0900 +++ b/src/fdl/test/TestPSXLinda.java Wed Feb 08 18:02:40 2012 +0900 @@ -69,7 +69,7 @@ int cnt=0; while(!r.ready()) { // psx.sync(1000); - psx.sync(); + psx.sync(0); System.out.println("Waiting...."+(cnt++)); } @@ -77,18 +77,17 @@ data.clear(); psx.out(PSX.META_STOP, data); - psx.sync(); + psx.sync(0); } catch (IOException e) { System.err.println("Communication failure."); } } - public static void print_id (PSXReply ans) { + public static void print_id (PSXReply ans) throws IOException { ByteBuffer r = ans.getData(); - id = r.getShort(); System.out.print("ID = "); - System.out.println(id); + System.out.write(r.array()); } } diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/TestTree.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/TestTree.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,493 @@ +package fdl.test; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.LinkedList; + +import fdl.FDLindaServ; +import fdl.FederatedLinda; +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSX; +import fdl.PSXCallback; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestTree { + /** + * Tree Walk Example + * configure tree structured federated Linda + * run message from tree root + * sum up data from the leaves + */ + public static final int PORT = 10000; + /** + * Tuple id for the Protocol + */ + public static final int ConnectReuest = 100; + private static final int Down = 101; + public static final int Up = 102; + /** + * Configuration binary format separator + */ + private static final int NO_HOST = -1; + private static final int HOST_LIST_END_MARK = -2; + private static final int CONNECT_LIST_END_MARK = -3; + /** + * Global + */ + public LinkedList lindas = new LinkedList(); // not used. + public int id = 0; + public boolean debug = true; + + class TreeNode implements Runnable { + /** + * Linda Server Thread + */ + public int id, port; + public TreeNode(int id, int port) { this.id = id; this.port = port; } + public void run() { + String[] args = { + // "-d", // Debug Message Flag + "-p",Integer.toString(port)}; + FDLindaServ.main(new TreeMetaProtocolEngine(id,port), args); + } + } + + /** + * @author kono + * Tuple transfer engine for a federated linda node + * MetaLinda is passed to mainLoop in this class + */ + class TreeMetaProtocolEngine implements MetaEngine { + /** + * Meta Engine for Configuration and Tree Walk + */ + boolean running = true; + LinkedList leftWaiter = new LinkedList(); + LinkedList rightWaiter = new LinkedList(); + PSXLinda parent, left, right; + String parentHost, leftHost,rightHost; + int parentPort=0, leftPort=0, rightPort=0; + public int nodeId = 0; + public int nodePort = 0; + public MetaLinda ml; + + public TreeMetaProtocolEngine(int id, int port) { + nodeId = id; + nodePort = port; + } + + /** + * Meta Engine Main Loop + */ + public void mainLoop(MetaLinda meta) { + ml = meta; + /* handler of Server STOP command */ + ml.in(PSX.META_STOP, new PSXCallback() {public void callback(ByteBuffer reply) { + running = false;}}); + /* handler of Configuration */ + ml.in(ConnectReuest, new PSXCallback() {public void callback(ByteBuffer reply) { + configureConnection(reply); + engineStart(); + } + }); + while(running) { + ml.sync(0); // Wait forever + } + } + + /** + * Configuration of node according to configuration packet + * @param reply + */ + private void configureConnection(ByteBuffer reply) { + parentPort = reply.getInt(); + leftPort = reply.getInt(); + rightPort = reply.getInt(); + if (parentPort!=0) parentHost = getString(reply); + if (leftPort!=0) leftHost = getString(reply); + if (rightPort!=0) rightHost = getString(reply); + } + + /** + * + * Open Inter-Server Connections and start tuple communication Engine + * + * parent + * | + * self = ml (Meta Linda Server) + * / \ + * left right + * + * @param ml Meta Linda Server + * @param reply a contents of configuration packet + */ + private void engineStart() { + try { + /** + * connect to neighbors + */ + if (parentPort!=0) parent = ml.fdl.open(parentHost, parentPort); + if (leftPort!=0) left = ml.fdl.open(leftHost, leftPort); + if (rightPort!=0) right = ml.fdl.open(rightHost, rightPort); + + if (debug) System.out.println("Connect this port="+nodePort+" parent="+parentPort+" left="+leftPort+" right="+rightPort); + /* + * Handle Downward tree walk + */ + ml.in(Down, new PSXCallback() {public void callback(ByteBuffer reply) { + ml.in(Down,this); // start this call back again for next packet + if (left==null) { + if (debug) System.out.println("Reached in Leaf "+nodeId+" value = "+parentPort); + // Leaf case + ByteBuffer answer = ByteBuffer.allocate(10); + answer.putInt(parentPort); + answer.flip(); + ml.out(Up, answer); + return; + } + // intermediate node + if (debug) System.out.println("Pass it to the children from "+nodeId + " to "+ leftPort + " and " + rightPort + "."); + ByteBuffer copy = reply.duplicate(); + left.out(Down, reply); + right.out(Down, copy); + } + + }); + /** + * Handle upward tree walk + */ + if (leftPort!=0) + left.in(Up, new PSXCallback() {public void callback(ByteBuffer reply) { + if (debug) System.out.println("Up from left at"+nodeId); + left.in(Up,this); + leftWaiter.add(reply); + checkSend(ml); + } + + }); + if (rightPort!=0) + right.in(Up, new PSXCallback() {public void callback(ByteBuffer reply) { + if (debug) System.out.println("Up from right at"+nodeId); + right.in(Up,this); + rightWaiter.add(reply); + checkSend(ml); + } + }); + } catch (IOException e) { + } + } + + /** + * Wait for all message from the sub node and + * calc sum and pass it to the parent. + * @param ml + */ + private void checkSend(MetaLinda ml) { + if (leftWaiter.isEmpty()||rightWaiter.isEmpty()) return; + ByteBuffer out = ByteBuffer.allocate(10); + int value = leftWaiter.poll().getInt()+rightWaiter.poll().getInt(); + if (parent!=null) { + // intermediate node + if (debug) System.out.println("Up the vluae "+value+" from "+nodeId); + out.putInt(value); + out.flip(); + ml.out(Up, out); + } else { + // Top node case + System.out.println("Top level node gets "+value); + } + } + } + + + /** + * @author kono + * Configuration Manager + * Read Configuration + * start linda servers + * send connect message to the servers + * then send start message + * stop all the server with Meta stop message + */ + class Configure implements Runnable { + public String id; + public ByteBuffer config; + public FederatedLinda fdl; + public LinkedList psxs = new LinkedList(); + private LinkedList hosts = new LinkedList(); + private LinkedList ports = new LinkedList(); + public Configure(int id,ByteBuffer config) { this.id = "Configure"+id; + this.config = config; + } + public void run() { + String[] args = {id}; + main(args); + } + + /** + * Configuration main + * @param arg + */ + public void main(String[] arg) { + final String name = arg[0]; + try { + fdl = FederatedLinda.init(); + readConfigure(); + fdl.sync(1000); + startTest(); + stop(); + } catch (IOException e) { + System.err.println(name+": Communication failure."); + } + } + + + /** + * Send start messages to configured servers + */ + public void startTest() { + System.out.println("StartTest"); + try { + PSXLinda psx = psxs.get(0); + for(int i=3;i<10;i++) { + sendData(psx,Down,i); + psx.sync(1000); + } + sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Send Server Stop message + * @throws IOException + */ + public void stop() throws IOException { + ByteBuffer data = ByteBuffer.allocate(10); + for(PSXLinda psx:psxs) { + psx.out(PSX.META_STOP, data.duplicate()); + psx.sync(1); + } + } + + /** + * This is not used. Open with connection id + * @param host + * @param port + * @throws IOException + */ + public void openLinda(String host, int port) throws IOException { + FederatedLinda fdl; + PSXLinda psx; + PSXReply r; + fdl = FederatedLinda.init(); + psx = fdl.open(host,port); + r = psx.in(65535); + fdl.sync(1); + System.out.println(host+" port "+port +": Connected."); + int cnt=0; + while(!r.ready()) { + // psx.sync(1000); + psx.sync(10); + System.out.println(host+" port "+port +": Waiting...."+(cnt++)); + } + print_id(r); + + return ; + } + + /** + * Read connection p configuation in a ByteBuffer + * @throws IOException + * + * Easy binary format, we should use more general format such as + * XML or messagePack. + */ + public void readConfigure() throws IOException { + int mode = 0; + int host = 0; + while(config.hasRemaining()) { + int port = config.getInt(); + if (mode==0) { + // Former part + // List of host and server name + if (port==HOST_LIST_END_MARK) { + mode = 1; + continue; + } + String hostname = getString(config); + + psxs.add(fdl.open(hostname,port)); + hosts.add(hostname); + ports.add(port); + + } else { + // Later part + // create interconnection configuration packet + // send it to the server + if (port == CONNECT_LIST_END_MARK) return; + // connect this, it's parent, left and right + connect(host++, port,config.getInt(),config.getInt()); + } + } + } + + /** + * Make a configuration packet for a federated linda node + * @param host No. of host to configure + * @param parent + * @param left + * @param right + */ + private void connect(int host, int parent, int left, int right) { + PSXLinda p = psxs.get(host); + ByteBuffer out = ByteBuffer.allocate(4096); + + out.putInt(parent!=NO_HOST? ports.get(parent):0); + out.putInt(left!=NO_HOST? ports.get(left):0); + out.putInt(right!=NO_HOST? ports.get(right):0); + if (parent!=NO_HOST) putString(out,hosts.get(parent)); + if (left!=NO_HOST) putString(out,hosts.get(left)); + if (right!=NO_HOST) putString(out,hosts.get(right)); + out.flip(); + p.out(ConnectReuest, out); + } + + } + + public synchronized void sleep(int time) { + try { + wait(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void read_wait(PSXLinda psx, PSXReply reply, String mesg) throws IOException { + while(!reply.ready()) psx.sync(10); + System.out.println(mesg); + System.out.println(reply.getData().getInt()); + System.out.println(""); + } + + public ByteBuffer sendData(PSXLinda psx,int id, int n) { + ByteBuffer data = ByteBuffer.allocate(10); + data.putInt(n); + data.flip(); + psx.out(id,data); + return data; + } + + public void in_wait(PSXLinda psx, int i) throws IOException { + PSXReply r = psx.in(i); + while(! r.ready()) { + psx.sync(10); + } + return; + } + + public void print_id (PSXReply ans) throws IOException { + ByteBuffer r = ans.getData(); + System.out.print("ID = "); + System.out.write(r.array()); + System.out.println(""); + } + + + /** + * read null terminated String in ByteBuffer + * @param reply + * @return + */ + public String getString(ByteBuffer reply) { + byte c; + int position = reply.position(); + int len = 0; + while(reply.hasRemaining()) { + c = reply.get(); + len ++; + if (c== 0) break; + } + return new String(reply.array(),position,len-1); + + } + + /** + * put String in ByteBuffer with null + * @param reply + * @param s + */ + public void putString(ByteBuffer reply,String s) { + reply.put(s.getBytes()); + reply.put((byte)0); + } + + public static void main(String[] arg) throws InterruptedException { + TestTree me = new TestTree(); + me.test1(); + } + + public void test1() throws InterruptedException { + // Make Configuration First + ByteBuffer config = makeConfig(); + sleep(2000); // Wait for servers' start + System.out.println("Start Configure"); + // Start configuration server and start test + Thread r1 = new Thread(new Configure(1,config)); + r1.start(); + r1.join(); + } + + /** + * Make Configuration + * start servers now + * No connections yet + * @return + */ + private ByteBuffer makeConfig() { + ByteBuffer config = ByteBuffer.allocate(4096); + putHostRun(config,"127.0.0.1",PORT); + putHostRun(config,"127.0.0.1",PORT+1); + putHostRun(config,"127.0.0.1",PORT+2); + putHostRun(config,"127.0.0.1",PORT+3); + putHostRun(config,"127.0.0.1",PORT+4); + putHostRun(config,"127.0.0.1",PORT+5); + putHostRun(config,"127.0.0.1",PORT+6); + config.putInt(HOST_LIST_END_MARK); + putTree(config,NO_HOST,1,2); + putTree(config,0,3,4); + putTree(config,0,5,6); + putTree(config,1,NO_HOST,NO_HOST); + putTree(config,1,NO_HOST,NO_HOST); + putTree(config,2,NO_HOST,NO_HOST); + putTree(config,2,NO_HOST,NO_HOST); + config.putInt(CONNECT_LIST_END_MARK); + config.flip(); + return config; + } + + private void putHostRun(ByteBuffer config, String host, int port2) { + putHost(config,host,port2); + // should run on specified host using ssh + Thread s = new Thread(new TreeNode(id ++, port2)); + s.start(); + lindas.add(s); + } + + private void putHost(ByteBuffer config, String s, int port2) { + config.putInt(port2); + putString(config, s); + } + + private void putTree(ByteBuffer config, int p, int l, int r) { + config.putInt(p); + config.putInt(l); + config.putInt(r); + } +} + +/* end */ diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/TestWaitRd.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/TestWaitRd.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,187 @@ +package fdl.test; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; + +import fdl.FDLindaServ; +import fdl.FederatedLinda; +import fdl.PSX; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestWaitRd { + + public FederatedLinda fdl; + public FDLindaServ fds; + public static final int PORT = 10000; + + class LindaServer implements Runnable { + public int id; + public LindaServer(int id) { this.id = id; } + public void run() { + String[] args = {/* "-d",*/"-p",Integer.toString(PORT)}; + FDLindaServ.main(args); + } + } + + class Sender implements Runnable { + public String id; + public Sender(int id) { this.id = "Sender"+id; } + public void run() { + String[] args = {id}; + sleep(2000); + main(args); + } + + public void main(String[] arg) { + try { + PSXLinda psx = openLinda(id); + sleep(1000); + sendData(psx,1,0); + psx.sync(1000); + in_wait(psx,1); + sendData(psx,1,1); + psx.sync(1000); + in_wait(psx,1); + sendData(psx,1,2); + psx.sync(1000); + in_wait(psx,1); + + for(int i=3;i<10;i++) { + sendData(psx,1,i); + psx.sync(1000); + in_wait(psx,1); + } + sleep(1000); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + class Reader implements Runnable { + public String id; + public Reader(int id) { this.id = "Reader"+id; } + public void run() { + String[] args = {id}; + sleep(2000); + main(args); + } + + public void main(String[] arg) { + final String name = arg[0]; + try { + PSXLinda psx = openLinda(id); + + PSXReply reply = psx.waitRd(1); + read_wait(psx, reply,name+": First read"); + PSXReply reply1 = psx.waitRd(1); + read_wait(psx, reply1,name+": 2nd read"); + + for(int i=3;i<10;i++) { + PSXReply replyn = psx.waitRd(1); + read_wait(psx, replyn,name+": "+i+"th read"); + } + + ByteBuffer data = ByteBuffer.allocate(10); + psx.out(PSX.META_STOP, data); + psx.sync(1); + + } catch (IOException e) { + System.err.println(name+": Communication failure."); + } + } + } + + public synchronized void sleep(int time) { + try { + wait(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void read_wait(PSXLinda psx, PSXReply reply, String mesg) throws IOException { + while(!reply.ready()) psx.sync(10); + System.out.println(mesg); + System.out.println(reply.getData().getInt()); + System.out.println(""); + } + + public PSXLinda openLinda(String name) throws IOException { + FederatedLinda fdl; + PSXLinda psx; + PSXReply r; + String host; + InetSocketAddress localAddress; + int port = PORT; + try { + localAddress = new InetSocketAddress(InetAddress.getLocalHost(), port); + host = localAddress.getHostName(); + } catch (UnknownHostException e) { + host = "localhost"; + } + fdl = FederatedLinda.init(); + psx = fdl.open(host,port); + r = psx.in(65535); + fdl.sync(1); + System.out.println(name+": Connected."); + int cnt=0; + while(!r.ready()) { + // psx.sync(1000); + psx.sync(10); + System.out.println(name+": Waiting...."+(cnt++)); + } + print_id(r); + return psx; + } + + public ByteBuffer sendData(PSXLinda psx,int id, int n) { + ByteBuffer data = ByteBuffer.allocate(10); + data.putInt(n); + data.flip(); + psx.out(id,data); + return data; + } + + public void in_wait(PSXLinda psx, int i) throws IOException { + PSXReply r = psx.in(i); + while(! r.ready()) { + psx.sync(10); + } + return; + } + + public void print_id (PSXReply ans) throws IOException { + ByteBuffer r = ans.getData(); + System.out.print("ID = "); + System.out.write(r.array()); + System.out.println(""); + } + + + public static void main(String[] arg) throws InterruptedException { + TestWaitRd me = new TestWaitRd(); + me.test1(); + } + + public void test1() throws InterruptedException { + Thread l1 = new Thread(new LindaServer(1)); + Thread r1 = new Thread(new Reader(1)); + Thread r2 = new Thread(new Reader(2)); + Thread s1 = new Thread(new Sender(1)); + l1.start(); + s1.start(); + r1.start(); + r2.start(); + s1.join(); + r1.join(); + r2.join(); + l1.join(); + + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/api/inout/InOutClient.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/api/inout/InOutClient.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,40 @@ +package fdl.test.api.inout; + +import java.io.IOException; +import fdl.test.topology.FDLindaNode; + +public class InOutClient { + private static int localPort = 10001; + private static String host = "localhost"; + private static int port = 10000; + private static int count = 1; + private static String usageString = "Usage: InOutClient [-host hostname] [-port portnum] [-count countnum]"; + /** + * @param args + */ + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-host".equals(args[i])) { + host = args[++i]; + } else if ("-port".equals(args[i])) { + port = Integer.parseInt(args[++i]); + } else if ("-count".equals(args[i])) { + count = Integer.parseInt(args[++i]); + } else if ("-d".equals(args[i])) { + FDLindaNode.debug = true; + } else { + System.err.println(usageString); + } + } + + try { + FDLindaNode node = new FDLindaNode(localPort); + InOutMetaEngine me = InOutMetaEngine.create(node.getMetaLinda(), host, port, count); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/api/inout/InOutMetaEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/api/inout/InOutMetaEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,75 @@ +package fdl.test.api.inout; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Date; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSX; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class InOutMetaEngine implements MetaEngine { + private final static int targetId = 1000; + + private String host; + private int port; + private int maxCount, count = 0; + private MetaLinda ml; + private PSXLinda linda; + private PSXReply reply; + private Date startTime, endTime; + + private InOutMetaEngine(MetaLinda metaLinda, String host, int port, int count) { + this.ml = metaLinda; + this.host = host; + this.port = port; + this.maxCount = count; + } + + private void connect() { + try { + linda = ml.open(host, port); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void mainLoop(MetaLinda ml) { + if (count >= maxCount) { + endTime = new Date(); + Long resultTime = new Long(endTime.getTime() - startTime.getTime()); + linda.out(PSX.META_STOP, ByteBuffer.wrap("".getBytes())); + try { + linda.sync(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(maxCount + " " + resultTime); + System.exit(0); + } + if (reply == null) { + reply = linda.in(targetId); + linda.out(targetId, ByteBuffer.wrap("test".getBytes())); + return; + } + if (reply.ready()) { + reply = null; + count++; + } + } + + public static InOutMetaEngine create(MetaLinda metaLinda, String host, int port, int count) { + InOutMetaEngine iome = new InOutMetaEngine(metaLinda, host, port, count); + iome.connect(); + iome.linda.out(targetId, ByteBuffer.wrap("test".getBytes())); + try { + iome.linda.sync(); + } catch (IOException e) { + e.printStackTrace(); + } + iome.startTime = new Date(); + return iome; + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/api/update/UpdateClient.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/api/update/UpdateClient.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,40 @@ +package fdl.test.api.update; + +import java.io.IOException; +import fdl.test.topology.FDLindaNode; + +public class UpdateClient { + private static int localPort = 10001; + private static String host = "localhost"; + private static int port = 10000; + private static int count = 1; + private static String usageString = "Usage: UpdateClient [-host hostname] [-port portnum] [-count countnum]"; + /** + * @param args + */ + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-host".equals(args[i])) { + host = args[++i]; + } else if ("-port".equals(args[i])) { + port = Integer.parseInt(args[++i]); + } else if ("-count".equals(args[i])) { + count = Integer.parseInt(args[++i]); + } else if ("-d".equals(args[i])) { + FDLindaNode.debug = true; + } else { + System.err.println(usageString); + } + } + + try { + FDLindaNode node = new FDLindaNode(localPort); + UpdateMetaEngine me = UpdateMetaEngine.create(node.getMetaLinda(), host, port, count); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/api/update/UpdateMetaEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/api/update/UpdateMetaEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,74 @@ +package fdl.test.api.update; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Date; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSX; +import fdl.PSXLinda; +import fdl.PSXReply; + +public class UpdateMetaEngine implements MetaEngine { + private final static int targetId = 1001; + + private String host; + private int port; + private int maxCount, count = 0; + private MetaLinda ml; + private PSXLinda linda; + private PSXReply reply; + private Date startTime, endTime; + + private UpdateMetaEngine(MetaLinda metaLinda, String host, int port, int count) { + this.ml = metaLinda; + this.host = host; + this.port = port; + this.maxCount = count; + } + + private void connect() { + try { + linda = ml.open(host, port); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void mainLoop(MetaLinda ml) { + if (count >= maxCount) { + endTime = new Date(); + Long resultTime = new Long(endTime.getTime() - startTime.getTime()); + linda.out(PSX.META_STOP, ByteBuffer.wrap("".getBytes())); + try { + linda.sync(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(maxCount + " " + resultTime); + System.exit(0); + } + if (reply == null) { + reply = linda.update(targetId, ByteBuffer.wrap("test".getBytes())); + return; + } + if (reply.ready()) { + reply = null; + count++; + } + } + + public static UpdateMetaEngine create(MetaLinda metaLinda, String host, int port, int count) { + UpdateMetaEngine iome = new UpdateMetaEngine(metaLinda, host, port, count); + iome.connect(); + iome.linda.out(targetId, ByteBuffer.wrap("test".getBytes())); + try { + iome.linda.sync(); + } catch (IOException e) { + e.printStackTrace(); + } + iome.startTime = new Date(); + return iome; + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/AcceptXMLCallback.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/AcceptXMLCallback.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,117 @@ +package fdl.test.debug; + +import java.io.IOException; +import java.io.StringReader; +import java.nio.ByteBuffer; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import fdl.PSXCallback; +import fdl.PSXLindaImpl; + +class AcceptXMLCallback implements PSXCallback { + TupleId tid; + NodeProperty np; + TreeProperty tp; + DebugProperty dp; + + private DocumentBuilderFactory dbFactory = null; + private DocumentBuilder docBuilder = null; + + public AcceptXMLCallback(TupleId tid, NodeProperty np) { + this.tid = tid; + this.np = np; + dbFactory = DocumentBuilderFactory.newInstance(); + try { + docBuilder = dbFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + + } + public void callback(ByteBuffer reply) { + String xml = new String(reply.array()); + Debug.print(xml); + parseXML(xml); + + np.ml.in(tid.id, this); + } + protected void parseXML(String xml) { + Document doc = null; + try { + doc = docBuilder.parse(new InputSource(new StringReader(xml))); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + Element root = doc.getDocumentElement(); + if(root.getTagName().equals("connections")) { + np.nodeId = Integer.parseInt(root.getAttribute("id")); + if (np.nodeId == 0) { + np.ml.in(TupleId.START.id, new StartCallback(np, tp = new TreeProperty(np))); + np.ml.in(TupleId.DEBUGSTART.id, new DebugStartCallback(np, dp = new DebugProperty())); + } + NodeList connections = root.getElementsByTagName("connection"); + makeConnections(connections); + } else if (root.getTagName().equals("routing")) { + Debug.print("Routing xml received!"); + + NodeList routing = root.getElementsByTagName("source"); + for (int i = 0; i < routing.getLength(); i++) { + Element src = (Element) routing.item(i); + Integer srcId = Integer.parseInt(src.getAttribute("id")); + Routing r = np.nodes.get(TupleId.getTupleIdFromId(srcId)); + NodeList dest = src.getElementsByTagName("dest"); + for (int j = 0; j < dest.getLength(); j++) { + Element dst = (Element) dest.item(j); + r.route.add(Integer.parseInt(dst.getAttribute("id"))); + } + } + + } + switch (tid) { + case MANAGE: + np.connected = true; + break; + case DEBUG: + np.debugConnected = true; + break; + } + if (np.connected && np.debugConnected) { + np.sendLocalHostName(); + Debug.print("Send local host name"); + np.connected = np.debugConnected = false; + } + } + private void makeConnections(NodeList connections) { + for (int i = 0; i < connections.getLength(); i++) { + Element connection = (Element)connections.item(i); + Element host = (Element)connection.getElementsByTagName("host").item(0); + Element port = (Element)connection.getElementsByTagName("port").item(0); + Element t = (Element)connection.getElementsByTagName("tid").item(0); + int srcId = Integer.parseInt(connection.getAttribute("id")); + String dstHostName = host.getTextContent(); + int dstPort = Integer.parseInt(port.getAttribute("id")); + int dstId = Integer.parseInt(t.getAttribute("id")); + try { + PSXLindaImpl linda = (PSXLindaImpl) np.ml.open(dstHostName, dstPort); + Routing r = new Routing(linda, dstId); + np.nodes.put(TupleId.getTupleIdFromId(srcId), r); + np.ml.in(srcId, new RoutingCallback(TupleId.getTupleIdFromId(srcId), r, np, tp, dp)); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/ConfigurationManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/ConfigurationManager.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,45 @@ +package fdl.test.debug; + +import java.io.IOException; + +import fdl.MetaEngine; + +/** +* ConfigurationManager +* +* @author Kazuki Akamine +* +* FDLindaNode を管理する Server +* +*/ + +public class ConfigurationManager { + private static int nodeNum = 2; + private static int relayNum = 1; + private static int relaySize = 1024; + private static String usageString + = "ConfigurationManager -nodes NODENUM -relay RELAYNUM -size RELAYSIZE"; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-nodes".equals(args[i])) { + nodeNum = Integer.parseInt(args[++i]); + } else if ("-relay".equals(args[i])) { + relayNum = Integer.parseInt(args[++i]); + } else if ("-size".equals(args[i])) { + relaySize = Integer.parseInt(args[++i]); + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode manager = new FDLindaNode(FDLindaNode.DEFAULTPORT); + MetaEngine me = new ConfigurationManagerEngine(manager.getMetaLinda(), nodeNum, relayNum, relaySize); + manager.setMetaEngine(me); + manager.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/ConfigurationManagerEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/ConfigurationManagerEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,173 @@ +package fdl.test.debug; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.*; + +public class ConfigurationManagerEngine implements MetaEngine { + public static final int DEFAULTPORT = 10000; + + public int nodeNum = 0; + public int relayNum = 0; + public int relaySize = 1; + private MetaLinda ml; + private NodeInfo[] nodes; + private boolean running = true; + + private class AcceptNewNode implements PSXCallback { + int count = 0; + public void callback(ByteBuffer reply) { + String[] hostData = new String(reply.array()).split(":"); + String hostName = hostData[0]; + int port = DEFAULTPORT; + if (hostData.length > 1) + port = Integer.parseInt(hostData[1]); + nodes[count] = new NodeInfo(hostName, port); + try { + nodes[count].linda = ml.open(hostName, port); + } catch (IOException e) { + e.printStackTrace(); + } + if (++count < nodeNum) { + ml.in(TupleId.MANAGE.id, this); + } else { + linkNodes(); + ml.in(TupleId.MANAGE.id, new ConfirmConnectionNode()); + } + } + } + + private class ConfirmConnectionNode implements PSXCallback { + int count = 0; + public void callback(ByteBuffer reply) { + if (++count < nodeNum) { + ml.in(TupleId.MANAGE.id, this); + } else { + routingNodes(); + ml.in(TupleId.MANAGE.id, new ConfirmRoutingNode()); + } + } + + } + + private class ConfirmRoutingNode implements PSXCallback { + int count = 0; + public void callback(ByteBuffer reply) { + if (++count < nodeNum) { + ml.in(TupleId.MANAGE.id, this); + } else { + print("All link configured!"); + // TREE実験開始を通知 + nodes[0].linda.out(TupleId.START.id, ByteBuffer.wrap("0".getBytes())); + // DebugRing 開始を通知 + //nodes[0].linda.out(DEBUGSTART, ByteBuffer.wrap(("print," + BODY + ",").getBytes())); + ml.in(TupleId.START.id, new ConfirmFinish()); + } + } + + } + + private class ConfirmFinish implements PSXCallback { + public void callback(ByteBuffer reply) { + System.out.println(new String(reply.array())); + nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap("shutdown".getBytes())); + print("Finish token"); + try { + nodes[0].linda.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + running = false; + } + + } + + private class RingLoop implements PSXCallback { + int counter = 0; + public void callback(ByteBuffer reply) { + nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap(("print," + TupleId.BODY.id + ",").getBytes())); + print("ring=" + counter + ": \n" + new String(reply.array())); + counter++; + ml.in(TupleId.DEBUG.id, this); + } + + } + + public ConfigurationManagerEngine(MetaLinda metaLinda, int nodeNum, int relayNum, int relaySize) { + // initialize + this.ml = metaLinda; + this.nodeNum = nodeNum; + this.relayNum = relayNum; + this.relaySize = relaySize; + nodes = new NodeInfo[nodeNum]; + } + + public void mainLoop(MetaLinda ml1) { + // resist poll tuple id + ml.in(TupleId.MANAGE.id, new AcceptNewNode()); + ml.in(TupleId.DEBUG.id, new RingLoop()); + while (running) + ml.sync(0); + } + + private void linkNodes() { + for (int i = 0; i < nodes.length; i++) { + // XML for Tree Topology + ConnectionXMLBuilder tree = new ConnectionXMLBuilder(i); + int k; + if (i != 0) { // TOP + k = (i-1)/2; + tree.appendConnection(TupleId.TREETOP, nodes[k].host, nodes[k].port, (i%2 == 0) ? TupleId.TREERIGHT : TupleId.TREELEFT); + } + if ((k = 2*i+1) < nodes.length) // LEFT + tree.appendConnection(TupleId.TREELEFT, nodes[k].host, nodes[k].port, TupleId.TREETOP); + if ((k = 2*i+2) < nodes.length) // RIGHT + tree.appendConnection(TupleId.TREERIGHT, nodes[k].host, nodes[k].port, TupleId.TREETOP); + nodes[i].connectionXML = tree.createXML(); + nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].connectionXML.getBytes())); + print(nodes[i].connectionXML); + + // XML for Ring Debug Topology + ConnectionXMLBuilder debug = new ConnectionXMLBuilder(i); + int left = (nodes.length + i - 1) % nodes.length; + int right = (i + 1) % nodes.length; + debug.appendConnection(TupleId.RINGLEFT, nodes[left].host, nodes[left].port, TupleId.RINGRIGHT); + debug.appendConnection(TupleId.RINGRIGHT, nodes[right].host, nodes[right].port, TupleId.RINGLEFT); + nodes[i].debugConnectionXML = debug.createXML(); + nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugConnectionXML.getBytes())); + print(nodes[i].debugConnectionXML); + } + } + + private void routingNodes() { + for (int i = 0; i < nodes.length; i++) { + RoutingXMLBuilder tree = new RoutingXMLBuilder(); + if (i != 0) { // TOP + if (2*i+1 < nodes.length) { // LEFT + tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREELEFT); + tree.appendRoutingTable(TupleId.TREELEFT, TupleId.TREETOP); + } + if (2*i+2 < nodes.length) { // RIGHT + tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREERIGHT); + tree.appendRoutingTable(TupleId.TREERIGHT, TupleId.TREETOP); + } + } + nodes[i].routingXML = tree.createXML(); + nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].routingXML.getBytes())); + print(nodes[i].routingXML); + + RoutingXMLBuilder debug = new RoutingXMLBuilder(); + debug.appendRoutingTable(TupleId.RINGLEFT, TupleId.RINGRIGHT); + debug.appendRoutingTable(TupleId.RINGRIGHT, TupleId.RINGLEFT); + nodes[i].debugRoutingXML = debug.createXML(); + nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugRoutingXML.getBytes())); + print(nodes[i].debugRoutingXML); + } + } + + void print(String str) { + System.err.println("[DEBUG] ConfigurationManager: " + str); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/ConnectionXMLBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/ConnectionXMLBuilder.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,31 @@ +package fdl.test.debug; + +import org.w3c.dom.*; + +public class ConnectionXMLBuilder extends XMLBuilder { + private Element connections; + + public ConnectionXMLBuilder(int id) { + super(); + connections = document.createElement("connections"); + connections.setAttribute("id", new Integer(id).toString()); + document.appendChild(connections); + } + + public void appendConnection(TupleId mytid, String host, int port, TupleId tid){ + Element connection = document.createElement("connection"); + connection.setAttribute("id", new Integer(mytid.id).toString()); + Element h = document.createElement("host"); + h.setTextContent(host); + Element p = document.createElement("port"); + p.setAttribute("id", new Integer(port).toString()); + Element t = document.createElement("tid"); + t.setAttribute("id", new Integer(tid.id).toString()); + + connections.appendChild(connection); + connection.appendChild(h); + connection.appendChild(p); + connection.appendChild(t); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/Debug.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/Debug.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,21 @@ +package fdl.test.debug; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class Debug { + private static Debug d = new Debug(); // Singleton + private String localHostName; + private Debug() { + try { + localHostName = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + static void print(String str) { + System.err.println("[DEBUG] " + d.localHostName + ": " + str); + System.err.flush(); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/DebugProperty.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/DebugProperty.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,5 @@ +package fdl.test.debug; + +public class DebugProperty { + public int relayNum, relaySize, relayCounter; +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/DebugStartCallback.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/DebugStartCallback.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,39 @@ +package fdl.test.debug; + +import java.nio.ByteBuffer; +//import java.util.Date; + +import fdl.PSXCallback; + +class DebugStartCallback implements PSXCallback { + public NodeProperty np; + public DebugProperty dp; + public DebugStartCallback(NodeProperty np, DebugProperty dp) { + this.np = np; + this.dp = dp; + } + public void callback(ByteBuffer reply) { + String[] commands = new String(reply.array()).split(","); + String command = commands[0]; + if (command.equals("relay")) { + dp.relayNum = Integer.parseInt(commands[1]); + dp.relaySize = Integer.parseInt(commands[2]); + dp.relayCounter = 0; + Debug.print("relay num=" + dp.relayNum + " size=" + dp.relaySize); + Routing r = np.nodes.get(TupleId.RINGRIGHT); + // 実験開始 + // TODO: startTime? + //startTime = new Date(); + r.linda.out(r.dstId, ByteBuffer.wrap(new byte[dp.relaySize])); + np.ml.in(TupleId.DEBUGSTART.id, this); + } else if (command.equals("print")) { + Routing r = np.nodes.get(TupleId.RINGRIGHT); + r.linda.out(r.dstId, reply); + Debug.print("Send Debug Message: print"); + np.ml.in(TupleId.DEBUGSTART.id, this); + } else if (command.equals("shutdown")) { + Routing r = np.nodes.get(TupleId.RINGRIGHT); + r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes())); + } + } +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/FDLindaNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/FDLindaNode.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,75 @@ +package fdl.test.debug; + +import java.io.IOException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; +import fdl.MetaEngine; + +/** +* FDLindaNode +* +* @author Kazuki Akamine +* +* Federated Linda の Topology Node +* 接続する機能までを実装 MetaEngine は外部から提供される +* +*/ + +public class FDLindaNode extends FDLindaServ { + // Fields + private MetaLinda ml; + public static final int DEFAULTPORT = 10000; + private static String manager; + private final static String usageString + = "Usage: FDLindaNode -manager SERVERNAME:PORT"; + + // Constructor + public FDLindaNode(int port) throws IOException { + super(port); + this.ml = new MetaLinda(tupleSpace, this); + } + + @Override public void mainLoop() { + me.mainLoop(ml); + } + public void setMetaEngine(MetaEngine me) { + this.me = me; + } + public MetaLinda getMetaLinda() { + return ml; + } + + // main routine + public static void main(String[] args) { + int port = DEFAULTPORT; + for (int i = 0; i < args.length; i++) { + if ("-manager".equals(args[i])) { + manager = args[++i]; + } else if ("-p".equals(args[i])) { + port = new Integer(args[++i]).intValue(); + } else { + System.err.println(usageString); + } + } + + String[] managerData = manager.split(":"); + String managerHostName = managerData[0]; + int managerPort; + if (managerData.length > 1) { + managerPort = new Integer(managerData[1]).intValue(); + } else { + managerPort = DEFAULTPORT; + } + + try { + FDLindaNode node = new FDLindaNode(DEFAULTPORT); + MetaEngine me = new MetaProtocolEngine(port, node.getMetaLinda(), managerHostName, managerPort); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/MetaProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/MetaProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,42 @@ +package fdl.test.debug; + +import java.nio.ByteBuffer; + +import fdl.*; + +/** +* MetaProtocolEngine +* +* @author Kazuki Akamine +* +* 接続する機能までを実装した MetaEngine +* これを継承して、具体的な処理を書く +* +*/ + +public class MetaProtocolEngine implements MetaEngine { + // Fields + private NodeProperty np; + + // Constructor + public MetaProtocolEngine(int port, MetaLinda ml, String managerHostName, int managerPort) { + this.np = new NodeProperty(port, ml, managerHostName, managerPort); + ml.out(TupleId.BODY.id, ByteBuffer.wrap("dummy".getBytes())); + } + + public void mainLoop(MetaLinda ml) { + initPoller(); + while (np.running) { + np.ml.sync(0); + } + Debug.print("Terminated" + np.nodeId + + " replies=" + np.ml.replies.size() + + " qsize=" + np.ml.fdl.qsize); + } + + protected void initPoller() { + np.ml.in(TupleId.MANAGE.id, new AcceptXMLCallback(TupleId.MANAGE, np)); + np.ml.in(TupleId.DEBUG.id, new AcceptXMLCallback(TupleId.DEBUG, np)); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/NodeInfo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/NodeInfo.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,17 @@ +package fdl.test.debug; + +import fdl.PSXLinda; + +public class NodeInfo { + public String host; + public int port; + public PSXLinda linda; + public String connectionXML, debugConnectionXML; + public String routingXML, debugRoutingXML; + + public NodeInfo(String host, int port) { + this.host = host; + this.port = port; + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/NodeProperty.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/NodeProperty.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,66 @@ +package fdl.test.debug; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.HashMap; + +import fdl.MetaLinda; +import fdl.PSXLinda; + +public class NodeProperty { + public static int DEFAULTPORT = 10000; + + public String localHostName; + public int localPort; + public PSXLinda manager; + public String managerHostName; + public int managerPort = DEFAULTPORT; + + public int nodeId; + public boolean running = true; + public boolean connected = false; + public boolean debugConnected = false; + public HashMap nodes = new HashMap(); + public MetaLinda ml; + + public NodeProperty(int port, MetaLinda ml, String managerHostName, int managerPort) { + this.ml = ml; + this.localPort = port; + this.managerHostName = managerHostName; + this.managerPort = managerPort; + try { + this.localHostName = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + manager = connectServer(this.managerHostName, this.managerPort); + sendLocalHostName(); + } + + protected void sendLocalHostName() { + // TopologyManager に自分のホストネームを送信して、起動を伝える + ByteBuffer local = ByteBuffer.wrap((localHostName + ":" + localPort).getBytes()); + manager.out(TupleId.MANAGE.id, local); + } + + protected PSXLinda connectServer(String hostName, int port) { + PSXLinda linda = null; + boolean connectPSX = true; + while (connectPSX) { + try { + linda = ml.open(hostName, port); + connectPSX = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + Debug.print("Connect to " + hostName); + return linda; + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/Routing.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/Routing.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,17 @@ +package fdl.test.debug; + +import java.util.LinkedList; + +import fdl.*; + +public class Routing { + PSXLinda linda; + int dstId; + LinkedList route; + + public Routing(PSXLinda linda, int dstId) { + this.linda = linda; + this.dstId = dstId; + this.route = new LinkedList(); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/RoutingCallback.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/RoutingCallback.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,141 @@ +package fdl.test.debug; + +import java.nio.ByteBuffer; +import java.util.Date; + +import fdl.PSXCallback; +import fdl.PSXReply; + +class RoutingCallback implements PSXCallback { + TupleId tid; + Routing routing; + NodeProperty np; + TreeProperty tp; + DebugProperty dp; + + public RoutingCallback(TupleId tid, Routing routing, NodeProperty np, TreeProperty tp, DebugProperty dp) { + this.tid = tid; + this.routing = routing; + this.np = np; + this.tp = tp; + this.dp = dp; + } + + public void callback(ByteBuffer reply) { + Debug.print("get: " + new String(reply.array())); + String str = new String(reply.array()); + String[] commands= str.split(","); + switch (tid) { + case TREETOP: + case TREELEFT: + case TREERIGHT: + np.ml.in(TupleId.BODY.id); + np.ml.out(TupleId.BODY.id, reply); + Debug.print("Update body: " + new String(reply.array())); + switch (tid) { + case TREETOP: + Routing r = np.nodes.get(tid); + if (r.route.isEmpty()) { + // TREE末端の処理 + r.linda.out(r.dstId, reply); // 送ってきた方に送り返す + np.ml.in(tid.id, this); + Debug.print("UP! " + np.nodeId); + return; + } + break; + default: + switch (tid) { + case TREELEFT: + tp.leftFlag = true; + break; + case TREERIGHT: + tp.rightFlag = true; + break; + } + if (tp.leftFlag && tp.rightFlag) { + Debug.print("UP"); + if (np.nodeId == 0) { + if (tp.treeCounter++ < tp.treeLoopNum) { + // int num = Integer.parseInt(new String(reply.array())); + tp.startTree(ByteBuffer.wrap((""+tp.treeCounter).getBytes())); + } else { + tp.endTime = new Date(); + Double resultTime = new Double(((tp.endTime.getTime() - tp.startTime.getTime()) / (double)tp.treeLoopNum)); + ByteBuffer data = ByteBuffer.wrap(resultTime.toString().getBytes()); + np.manager.out(TupleId.START.id, data); + Debug.print("Finish Tree"); + } + } else { + Routing r1 = np.nodes.get(TupleId.TREETOP); + r1.linda.out(r1.dstId, reply); + } + } + np.ml.in(tid.id, this); + return; + } + break; + case RINGLEFT: + case RINGRIGHT: + if (str.equals("shutdown")) { + Debug.print("get shutdown command id: " + np.nodeId); + if (np.nodeId != 0) { + Routing r = null; + r = np.nodes.get(tid.getMirrorId()); + r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes())); + np.ml.fdl.queueExec(); + } else { + Debug.print("shutdown reaches last node!"); + } + np.running = false; + return; + } else if (commands[0].equals("print")) { + Debug.print("Get Debug Message: print"); + int printId = Integer.parseInt(commands[1]); + PSXReply rep = np.ml.rd(printId); + np.ml.sync(1); + str += "" + + np.localHostName + ":" + np.localPort + + ""; + if (rep.ready()) + str += new String(rep.data.array()); + str += "\n"; + np.ml.in(tid.id, this); + if (np.nodeId == 0) { + // 実験終了 + np.manager.out(TupleId.DEBUG.id, ByteBuffer.wrap(str.getBytes())); + } else { + Routing r = np.nodes.get(tid.getMirrorId()); + r.linda.out(r.dstId, ByteBuffer.wrap(str.getBytes())); + } + return; + } + break; + default: + if (np.nodeId == 0 && tid == TupleId.RINGLEFT) { + dp.relayCounter++; + Debug.print("" + dp.relayCounter + " relay"); + if (dp.relayCounter >= dp.relayNum) { + // 実験終了 + tp.endTime = new Date(); + Double resultTime = new Double(((tp.endTime.getTime() - tp.startTime.getTime()) / (double)dp.relayNum)); + ByteBuffer data = ByteBuffer.wrap(resultTime.toString().getBytes()); + np.manager.out(TupleId.DEBUG.id, data); + np.ml.in(tid.id, this); + return; + } + } + } + for (Integer dstId : routing.route) { + if (dstId == TupleId.TREELEFT.id) + tp.leftFlag = false; + else if (dstId == TupleId.TREERIGHT.id) + tp.rightFlag = false; + Routing r = np.nodes.get(TupleId.getTupleIdFromId(dstId)); + r.linda.out(r.dstId, reply); + } + np.ml.in(tid.id, this); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/RoutingXMLBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/RoutingXMLBuilder.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,36 @@ +package fdl.test.debug; + +import java.util.*; + +import org.w3c.dom.Element; + +public class RoutingXMLBuilder extends XMLBuilder { + private Element routing; + private HashMap routingTable; + + public RoutingXMLBuilder() { + super(); + routing = document.createElement("routing"); + document.appendChild(routing); + routingTable = new HashMap(); + } + + private Element createRoutingTable(TupleId src) { + Integer tupleId = new Integer(src.id); + Element source = document.createElement("source"); + source.setAttribute("id", tupleId.toString()); + routing.appendChild(source); + routingTable.put(tupleId, source); + return source; + } + + public void appendRoutingTable(TupleId src, TupleId dst) { + Element source = routingTable.get(new Integer(src.id)); + if (source == null) + source = createRoutingTable(src); + Element dest = document.createElement("dest"); + dest.setAttribute("id", new Integer(dst.id).toString()); + source.appendChild(dest); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/StartCallback.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/StartCallback.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,27 @@ +package fdl.test.debug; + +import java.nio.ByteBuffer; +import java.util.Date; + +import fdl.PSXCallback; + +class StartCallback implements PSXCallback { + NodeProperty np; + TreeProperty tp; + + public StartCallback(NodeProperty np, TreeProperty tp) { + this.np = np; + this.tp = tp; + } + + public void callback(ByteBuffer reply) { + Debug.print("Start Tree"); + tp.startTime = new Date(); + tp = new TreeProperty(np); + tp.startTree(reply); + np.ml.in(TupleId.BODY.id); + np.ml.out(TupleId.BODY.id, reply); + np.ml.in(TupleId.START.id, this); + } + +} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/TreeProperty.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/TreeProperty.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,30 @@ +package fdl.test.debug; + +import java.nio.ByteBuffer; +import java.util.Date; + +public class TreeProperty { + public boolean rightFlag = true, leftFlag = true; + public int treeCounter = 0, treeLoopNum = 100; + public Date startTime, endTime; + public NodeProperty np; + + public TreeProperty(NodeProperty np) { + this.np = np; + } + + public void startTree(ByteBuffer reply) { + Routing r; + // 子があるならば、子にタプルを伝搬 + if (np.nodes.containsKey(TupleId.TREERIGHT)) { + r = np.nodes.get(TupleId.TREERIGHT); + r.linda.out(r.dstId, reply); + rightFlag = false; + } + if (np.nodes.containsKey(TupleId.TREELEFT)) { + r = np.nodes.get(TupleId.TREELEFT); + r.linda.out(r.dstId, reply); + leftFlag = false; + } + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/TupleId.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/TupleId.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,42 @@ +package fdl.test.debug; + +import java.util.HashMap; + +public enum TupleId { + BODY (100), + START (BODY.id+1), + FINISH (BODY.id+2), + MANAGE (60000), + TREETOP (MANAGE.id+1), + TREELEFT (MANAGE.id+2) { public TupleId getMirrorId() { return TREERIGHT; } }, + TREERIGHT (MANAGE.id+3) { public TupleId getMirrorId() { return TREELEFT; } }, + DEBUG (61000), + RINGLEFT (DEBUG.id+1) { public TupleId getMirrorId() { return RINGRIGHT; } }, + RINGRIGHT (DEBUG.id+2) { public TupleId getMirrorId() { return RINGLEFT; } }, + DEBUGSTART (DEBUG.id+3); + + public int id; + public static HashMap hash = new HashMap(); + + private TupleId(int id) { + this.id = id; + } + public TupleId getMirrorId() { + return this; + } + public static TupleId getTupleIdFromId(int id) { +// for (TupleId tid : TupleId.values()) { +// if (tid.id == id) { +// return tid; +// } +// } + return hash.get(id); + } + + static { + for (TupleId tid : TupleId.values()) { + hash.put(tid.id, tid); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/debug/XMLBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/debug/XMLBuilder.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,40 @@ +package fdl.test.debug; + +import java.io.*; +import javax.xml.parsers.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.w3c.dom.*; + +public class XMLBuilder { + protected Document document; + protected Transformer transformer; + public XMLBuilder() { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = null; + try { + docBuilder = dbFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + document = docBuilder.newDocument(); + TransformerFactory tFactory = TransformerFactory.newInstance(); + try { + transformer = tFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + } catch (TransformerConfigurationException e) { + e.printStackTrace(); + } + } + + public String createXML() { + StringWriter sw = new StringWriter(); + try { + transformer.transform(new DOMSource(document), new StreamResult(sw)); + } catch (TransformerException e) { + e.printStackTrace(); + } + return sw.toString(); + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/FDLServWithSend.java --- a/src/fdl/test/metaTransfer/FDLServWithSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -package fdl.test.metaTransfer; - -import java.io.IOException; - -import fdl.FDLindaServ; -import fdl.MetaLinda; - - -public class FDLServWithSend extends FDLindaServ { - int port1; - public FDLServWithSend(int port,int port1) throws IOException { - super(port); - this.port1 = port1; - } - - @Override public void mainLoop() { - System.out.println("change mainloop"); - MetaLinda ml = new MetaLinda(tupleSpace, this); - MetaProtocolEngine mpe = new MetaProtocolEngine(ml,"MetaEngine", port1); - mpe.mainLoop(); - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/MetaProtocolEngine.java --- a/src/fdl/test/metaTransfer/MetaProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -package fdl.test.metaTransfer; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.MetaEngine; -import fdl.MetaLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - -public class MetaProtocolEngine implements MetaEngine { - private int id = 10; - private boolean running = true; - - private PSXLinda psx; - private String name; - MetaLinda fdl1; - String host = "127.0.0.1"; - int port1; - private PSXLinda psx1; - private ByteBuffer data2 = ByteBuffer.allocate(10); - private int count = 1; - - - public MetaProtocolEngine(MetaLinda ml, String string, int port1) { - this.name = string; - this.port1 = port1; - this.fdl1 = ml; - } - - public void mainLoop(){ - boolean connect = true; - System.out.println("MetaEngine Start"); - while(connect){ - try { - initConnect(); - transfer(); - connect = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - private void initConnect(){ - System.out.println(name); - boolean connectpsx = true; - while(connectpsx){ - try { - psx = fdl1.open(host,port1); - connectpsx = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } - } - } - System.out.println("Connect "+port1); - psx1 = fdl1; - } - - private void transfer() throws IOException { - PSXReply in = psx.in(id); - System.out.println("PSXReply "+port1); - while (running) { - if(in.ready()){ - data2 = in.getData(); - int i = data2.getInt(); - data2.rewind(); - //outしたbytebufferの変更をこれ以降やっちゃいけない - psx1.out(id,data2); - - System.out.println("IntData0 "+port1 +i); - fdl1.sync(); - if (count++>3) { - running = false; - break; - } - System.out.println("IntData1 "+port1 +i); - } - fdl1.sync(); - } - assertEquals(4,count); - } - - -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/MetaProtocolEngine_not.java --- a/src/fdl/test/metaTransfer/MetaProtocolEngine_not.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -package fdl.test.metaTransfer; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.MetaEngine; -import fdl.MetaLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class MetaProtocolEngine_not implements MetaEngine { - private FederatedLinda fdlmeta; - private ByteBuffer data = ByteBuffer.allocate(1024); - private int id = 10; - private boolean running = true; - - //ここからsend用宣言 - private PSXLinda sendpsx,getpsx; - private String host = "127.0.0.1"; - private int port; - - public MetaProtocolEngine_not(MetaLinda fdlmeta, int port) { - this.port = port; - } - - public void metaOpen(int sendport){ - try{ - sendpsx = fdlmeta.open(host, sendport); - }catch (IOException e) { - e.printStackTrace(); - } - } - - //meta部分のデータ取得 - public void metaTrans() { - try { - getpsx = fdlmeta.open(host, 10001); - } catch (IOException e) { - e.printStackTrace(); - } - PSXReply in = getpsx.in(id); - while (running) { - System.out.println(in); - System.out.println(in.ready()); - if(in.ready()) { - data = in.getData(); - sendpsx.out(id, data); - running = false; - break; - } - } - } - - - public void mainLoop() { - System.out.println("MetaProtocolEngine Start"); - fdlmeta = FederatedLinda.init(); - if( port == 10002 ) { - metaOpen(10003); - metaTrans(); - try { - fdlmeta.sync(); - } catch (IOException e) { - e.printStackTrace(); - } - System.out.println("MetaProtocolEngine Connect"); - } - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/FDLServWithSend.java --- a/src/fdl/test/metaTransfer/Ring/FDLServWithSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import java.io.IOException; - -import fdl.FDLindaServ; -import fdl.MetaLinda; - -public class FDLServWithSend extends FDLindaServ { - int port1; - public FDLServWithSend(int port,int port1) throws IOException { - super(port); - this.port1 = port1; - } - - @Override public void mainLoop() { - System.out.println("change mainloop"); - MetaLinda ml = new MetaLinda(tupleSpace, this); - MetaProtocolEngine mpe = new MetaProtocolEngine(ml,"MetaEngine", port1, port1); - mpe.mainLoop(); - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/MetaProtocolEngine.java --- a/src/fdl/test/metaTransfer/Ring/MetaProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -//import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.MetaEngine; -import fdl.MetaLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - -public class MetaProtocolEngine implements MetaEngine { - int id = 10; - private boolean running = true; - - private PSXLinda psx; - private String name; - MetaLinda fdl1; - String host = "127.0.0.1"; - int port1; - private PSXLinda psx1; - private ByteBuffer data = ByteBuffer.allocate(10); -// private int count = 1; - int port; - - - public MetaProtocolEngine(MetaLinda ml, String string, int port1, int port) { - this.name = string; - this.port1 = port1; - this.fdl1 = ml; - this.port = port; - } - - public void mainLoop(){ - boolean connect = true; - System.out.println("MetaEngine Start"); - while(connect){ - try { - initConnect(); - transfer(psx, psx1); - connect = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - private void initConnect() throws IOException{ - System.out.println(name); - //自分 - psx = fdl1; - if (port == 10001){ - sendData(); - } - //相手 - psx1 = fdl1.open(host,port1); - System.out.println("Connect "+port1); - } - - private void sendData() throws IOException{ - boolean connectSend = true; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - while(connectSend){ - psx.out(id, send); - fdl1.sync(1); - System.out.println("Send Data 10001"); - connectSend = false; - } - } - - private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { - PSXReply in = getpsx.in(id); - System.out.println("PSXReply "+port1); - while (running) { - if(in.ready()){ - data = in.getData(); - int i = data.getInt(); - data.rewind(); - //outしたbytebufferの変更をこれ以降やっちゃいけない - sendpsx.out(id,data); - - System.out.println("IntData0 "+port1 +i); - fdl1.sync(1); - running = false; - System.out.println("IntData1 "+port1 +i); - break; - } - fdl1.sync(); - } -// assertEquals(4,count); - } - -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/Server.java --- a/src/fdl/test/metaTransfer/Ring/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import java.io.IOException; - -import fdl.FDLindaServ; - -public class Server extends Thread { - int port1; - int port2; - int set; - private String name; - - public Server(String string, int i, int t, int s) { - port1 = i; - port2 = t; - name = string; - set = s; - } - - public void run(){ - String[] args = {"-p",Integer.toString(port1)}; - System.out.println(name); - switch (set){ - case 0: - try { - FDLServWithSend send = new FDLServWithSend(port1,port2); - send.mainLoop(); - } catch (IOException e) { - e.printStackTrace(); - } - break; - - case 1: - FDLindaServ.main(args); - break; - } - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/TestSend.java --- a/src/fdl/test/metaTransfer/Ring/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend extends Thread { - PSXLinda psxsend; - FederatedLinda fdlsend; - - public void run(){ - int id = 10; - int port = 10001; - boolean connect = true; - String host = "127.0.0.1"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - while(connect){ - try { - sendData(id, port, host, send); - connect = false; - } catch(IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } - } - - } - } - - private void sendData(int id, int port, String host, ByteBuffer send) throws IOException { - fdlsend = FederatedLinda.init(); - - psxsend = fdlsend.open(host,port); - System.out.println("Connect Host1"); - psxsend.out(id, send); - fdlsend.sync(1); - System.out.println("Send Data"); - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/TestTransfer.java --- a/src/fdl/test/metaTransfer/Ring/TestTransfer.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.junit.Test; - -public class TestTransfer { - @Test - public void testTransfer() throws IOException { - int port1 = 10001; - int port2 = 10002; - - Server server1 = new Server("Server1",port1, port2,0); - server1.start(); - - Server server2 = new Server("Server2",port2, port1,1); - server2.start(); - - try { - server2.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - assertEquals(1,1); - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/TestTransferRing.java --- a/src/fdl/test/metaTransfer/Ring/TestTransferRing.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class TestTransferRing { - Server[] servers; - final int serverCount = 3; - final int startPort = 10001; - @Test - public void testTransfer() { - int port = startPort; - - servers = new Server[serverCount]; - - for (int i = 0; i < serverCount; i++) { - servers[i] = new Server("Server"+(i+1),port+i,netporot(port+i),0); - servers[i].start(); - } - -// try { -// servers[3].join(); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } - - - assertEquals(1,1); - } - private int netporot(int i) { - i++; - if(i >= startPort+serverCount) { - return startPort; - } - - return i; - } -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Ring/TestTransferThree.java --- a/src/fdl/test/metaTransfer/Ring/TestTransferThree.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -package fdl.test.metaTransfer.Ring; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class TestTransferThree { - @Test - public void testTransfer() { - - Server server1 = new Server("Server1", 10001, 10002, 0); - server1.start(); - Server server2 = new Server("Server2", 10002, 10001, 0); - server2.start(); -// Server server3 = new Server("Server3", 10003, 10001, 0); -// server3.start(); - - assertEquals(1,1); - } - -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/Server.java --- a/src/fdl/test/metaTransfer/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package fdl.test.metaTransfer; - -import fdl.FDLindaServ; - -public class Server extends Thread { - int port; - private String name; - - public Server(String string, int i) { - port = i; - name = string; - } - - public void run(){ - String[] args = {"-p",Integer.toString(port)}; - System.out.println(name); - FDLindaServ.main(args); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/TestSend.java --- a/src/fdl/test/metaTransfer/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -package fdl.test.metaTransfer; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend extends Thread { - PSXLinda psxsend; - FederatedLinda fdlsend; - - public void run(){ - int id = 10; - int port = 10001; - boolean connect = true; - String host = "127.0.0.1"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - while(connect){ - try { - sendData(id, port, host, send); - connect = false; - } catch(IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } - } - - } - } - - private void sendData(int id, int port, String host, ByteBuffer send) throws IOException { - fdlsend = FederatedLinda.init(); - - psxsend = fdlsend.open(host,port); - System.out.println("Connect Host1"); - psxsend.out(id, send); - fdlsend.sync(1); - System.out.println("Send Data"); - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/metaTransfer/TestTransfer.java --- a/src/fdl/test/metaTransfer/TestTransfer.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -package fdl.test.metaTransfer; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.junit.Test; - -public class TestTransfer { - @Test - public void testTransfer() throws IOException { - int port1 = 10001; - int port2 = 10002; - -// Server server1 = new Server("Server1",port1); -// server1.start(); -// - Server server2 = new Server("Server2",port2); - server2.start(); - - - FDLServWithSend send = new FDLServWithSend(port1, port2); - send.mainLoop(); - - assertEquals(1,1); - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/FDLindaNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/FDLindaNode.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,64 @@ +package fdl.test.topology; + +import java.io.IOException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; +import fdl.MetaEngine; + +/** +* FDLindaNode +* +* @author Kazuki Akamine +* +* Federated Linda の Topology Node +* 接続する機能までを実装 MetaEngine は外部から提供される +* +*/ + +public class FDLindaNode extends FDLindaServ { + // Fields + private MetaLinda ml; + private static int localPort = 10000; + private static String managerHostName; + private final static String usageString + = "Usage: FDLindaNode -manager SERVERNAME"; + + // Constructor + public FDLindaNode(int port) throws IOException { + super(port); + this.ml = new MetaLinda(tupleSpace, this); + } + + @Override public void mainLoop() { + while (true) { // loop + me.mainLoop(ml); + ml.sync(); + } + } + public void setMetaEngine(MetaEngine me) { + this.me = me; + } + public MetaLinda getMetaLinda() { + return ml; + } + // main routine + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-manager".equals(args[i])) { + managerHostName = args[++i]; + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode node = new FDLindaNode(localPort); + MetaEngine me = new MetaProtocolEngine(node.getMetaLinda(), managerHostName); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/MetaProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/MetaProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,117 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +/** +* MetaProtocolEngine +* +* @author Kazuki Akamine +* +* 接続する機能までを実装した MetaEngine +* これを継承して、具体的な処理を書く +* +*/ + +public class MetaProtocolEngine implements MetaEngine { + // Fields + protected static String lastToken = "null"; + protected static int port = 10000; + protected static int manageId = 60000; + protected PSXLinda manager, psxLocal; + protected ArrayList psxSendServers; + protected MetaLinda fdlMeta; + protected String managerHostName; + protected String localHostName; + + // Constructor + public MetaProtocolEngine(MetaLinda ml, String managerHostName) { + this.fdlMeta = ml; + this.managerHostName = managerHostName; + try { + this.localHostName = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + this.psxSendServers = new ArrayList(); + } + public void mainLoop(MetaLinda ml) {} + + public void mainLoop() { + psxLocal = fdlMeta; + initTopologyManager(); + initSendServer(); + } + + protected void initSendServer() { + sendLocalHostName(); + connectSendServers(); + sendLocalHostName(); + } + + protected void initTopologyManager() { + // Connect to TopologyManager Server + try { + manager = fdlMeta.open(managerHostName, port); + } catch (IOException e) { + // manager は先に立ち上げてから実行する。 + // while で回して再接続 loop にすべき? + e.printStackTrace(); + } + } + + protected void sendLocalHostName() { + // TopologyManager に自分のホストネームを送信して、起動を伝える + ByteBuffer local; + local = ByteBuffer.wrap(localHostName.getBytes()); + manager.out(manageId, local); + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + + protected void connectSendServers() { + // TopologyManager から、送られてくる ConnectServer の hostName を取得して接続 + System.out.println("[DEBUG] MethodCall connectSendServers()"); + while (true) { + PSXReply reply; + reply = psxLocal.in(manageId); + do { + try { + psxLocal.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String hostName = new String(data.array()); + System.out.println("[DEBUG] GetReply " + hostName); + if (hostName.equals(lastToken)) + break; + connectSendServer(hostName); + } + } + + protected void connectSendServer(String hostName) { + PSXLinda linda; + try { + linda = fdlMeta.open(hostName, port); + psxSendServers.add(linda); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("Connect to " + hostName); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/NodeManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/NodeManager.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,93 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import fdl.PSXLinda; +import fdl.MetaLinda; + +public class NodeManager { + public String hostName; + public PSXLinda linda; + private MetaLinda mLinda; + private int port; + private int manageId; + private ArrayList waitingNodes; + private ArrayList waitedNodes; +// private ArrayList sendNodes; + + public NodeManager(MetaLinda ml, int port, int manageId) { + this.port = port; + this.manageId = manageId; + this.mLinda = ml; + hostName = null; + waitingNodes = new ArrayList(); + waitedNodes = new ArrayList(); +// sendNodes = new ArrayList(); + } + + public void addConnection(NodeManager node) { + waitingNodes.add(node); + node.waitedNodes.add(this); + } + + public void finishConnection(NodeManager node) { + waitingNodes.remove(node); + node.waitedNodes.remove(this); +// sendNodes.add(node); + } + + public void startUp(String hostName) { + // 起動 + this.hostName = hostName; + try { + linda = mLinda.open(hostName, port); + } catch (IOException e) { + e.printStackTrace(); + } + // 起動待ちのノードに通知 + connectWaitedNodes(); + } + + public void connectWaitedNodes() { + for (int i = 0; i < waitedNodes.size(); i++) { + NodeManager node = waitedNodes.get(i); + if (node.isRunning()) { + node.connect(this); + node.finishConnection(this); + if (waitingNodes.contains(node)) { + connect(node); + finishConnection(node); + } + } + } + for (int i = 0; i < waitingNodes.size(); i++) { + NodeManager node = waitingNodes.get(i); + if (node.isRunning()) { + connect(node); + finishConnection(node); + if (node.waitingNodes.contains(this)) { + node.connect(this); + node.finishConnection(this); + } + } + } + } + + public void connect(NodeManager node) { + ByteBuffer data = ByteBuffer.wrap(node.hostName.getBytes()); + linda.out(manageId, data); + try { + linda.sync(1); + System.out.println("[DEBUG] SendConnectTo: " + hostName + " to " + node.hostName); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean isRunning() { + return hostName != null; + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/TopologyManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/TopologyManager.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,40 @@ +package fdl.test.topology; + +import java.io.IOException; + +import fdl.MetaEngine; + +/** +* TopologyManager +* +* @author Kazuki Akamine +* +* FDLindaNode の Topology を管理する Server +* +*/ + +public class TopologyManager { + private static int localPort = 10000; + private static int nodeNum = 2; + private static String usageString + = "TopologyManager -nodes NODENUM"; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-nodes".equals(args[i])) { + nodeNum = Integer.parseInt(args[++i]); + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode manager = new FDLindaNode(localPort); + MetaEngine me = new TopologyManagerEngine(manager.getMetaLinda(), nodeNum); + manager.setMetaEngine(me); + manager.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/TopologyManagerEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/TopologyManagerEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,115 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +/** +* TopologyManagerEngine +* +* @author Kazuki Akamine +* +* FDLindaNode の Topology を管理する Server の MetaEngine +* TopologyManager の具体的な実装 +* +*/ + +public class TopologyManagerEngine implements MetaEngine { + protected static String lastToken = "null"; + protected static int port = 10000; + protected static int manageId = 60000; + protected PSXLinda manager; + protected MetaLinda fdlMeta; + protected int nodeNum; // 後々は、nodelist から自動で取得したい + protected NodeManager[] nodes; + + // Constructor + public TopologyManagerEngine(MetaLinda ml, int nodeNum) { + this.fdlMeta = ml; + this.nodeNum = nodeNum; + this.nodes = new NodeManager[this.nodeNum]; + for (int i = 0; i < nodes.length; i++) { + this.nodes[i] = new NodeManager(fdlMeta, port, manageId); + } + } + + public void mainLoop(MetaLinda ml) {} + + public void mainLoop() { + manager = fdlMeta; + makeTopology(); + } + + protected void makeTopology() { + makeConnection(); + acceptNewNode(); + sendLastToken(); + checkConnection(); + } + + // これを継承して、 Topology を形成 + // 最終的には外部XMLを読み込んで接続するようにする + protected void makeConnection() { + // 接続を定義 + nodes[0].addConnection(nodes[1]); + nodes[1].addConnection(nodes[0]); + } + + protected void acceptNewNode() { + // 起動状況を確認しつつ、接続命令を送信 + // nodes の数だけ新規参入 node を待つ。 + for (int i = 0; i < nodes.length; i++) { + PSXReply reply; + reply = manager.in(manageId); + do { + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String hostName = new String(data.array()); + System.out.println("[DEBUG] GetNodeName: " + hostName); + nodes[i].startUp(hostName); + } + } + + protected void sendLastToken() { + ByteBuffer lastTokenBB = ByteBuffer.wrap(lastToken.getBytes()); + for (int i = 0; i < nodes.length; i++) { + // 参加ノードに実験開始を告知する ("null"を送る) + nodes[i].linda.out(manageId, lastTokenBB); + System.out.println("[DEBUG] SendMsg: " + lastToken + " to " + nodes[i].hostName); + + try { + nodes[i].linda.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + protected void checkConnection() { + for (int i = 0; i < nodes.length; i++) { + PSXReply reply; + reply = manager.in(manageId); + do { + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String ack = new String(data.array()); + System.out.println("[DEBUG] ConnectedHost: " + ack); + } + System.out.println("[DEBUG] AllConnected"); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/ring/FDLindaRingNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/FDLindaRingNode.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,49 @@ +package fdl.test.topology.ring; + +import java.io.IOException; +import fdl.MetaEngine; +import fdl.test.topology.*; + +/** +* FDLindaRingNode +* +* @author Kazuki Akamine +* +* Federated Linda の Ring 用 Node +* Ring 用の MetaEngine を持たせる。 +* +*/ + +public class FDLindaRingNode { + // Fields + private static int localPort = 10000; + private static String managerHostName; + private static String relayNumString; + private static int relayNum; + private final static String usageString + = "Usage: FDLindaRingNode -manager SERVERNAME"; + + // main routine + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-manager".equals(args[i])) { + managerHostName = args[++i]; + } else if ("-relay".equals(args[i])) { + relayNumString = args[++i]; + } else { + System.err.println(usageString); + } + } + relayNum = Integer.parseInt(relayNumString); + + try { + FDLindaNode node = new FDLindaNode(localPort); + MetaEngine me = new RingMetaProtocolEngine(node.getMetaLinda(), managerHostName, relayNum); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/ring/RingMetaProtocolEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/RingMetaProtocolEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,102 @@ +package fdl.test.topology.ring; + +import java.io.IOException; +import java.nio.ByteBuffer; +//import java.text.SimpleDateFormat; +import java.util.Date; + +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; +import fdl.test.topology.MetaProtocolEngine; + +/** +* RingMetaProtocolEngine +* +* @author Kazuki Akamine +* +* Ring 接続実験用の MetaProtocolEngine +* Ring 接続実験の具体的な処理を記述する。 +* +*/ + +public class RingMetaProtocolEngine extends MetaProtocolEngine { + private int relayNum; + private static int relayId = 10; + private Date startTime, endTime; + //private SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); + public RingMetaProtocolEngine(MetaLinda ml, String managerHostName, int relayNum) { + super(ml, managerHostName); + this.relayNum = relayNum; + startTime = null; + endTime = null; + } + + @Override public void mainLoop() { + // 接続処理 + super.mainLoop(); + // Ring 実験開始 + relayTuple(relayId); + } + + private void relayTuple(int tupleId) { + while (true) { + ByteBuffer data = receiveToken(tupleId); + if (startTime == null) { + startTime = new Date(); + } + if (relayNum == 0) { + String token = new String(data.array()); + if (!token.equals(lastToken)) { + // TODO: 実験結果をManagerに伝えるなどの処理 + sendResult(); + System.out.println("[Ring] relay finished: " + token); + // 実験終了を各ノードにリレーで伝える + sendToken(tupleId, ByteBuffer.wrap(lastToken.getBytes())); + } + return; + } + sendToken(tupleId, data); + System.out.println("[DEBUG] Relay..."); + relayNum--; + } + } + + private void sendResult() { + this.endTime = new Date(); + Long resultTime = new Long(endTime.getTime() - startTime.getTime()); + ByteBuffer data = ByteBuffer.wrap(resultTime.toString().getBytes()); + manager.out(relayId, data); + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private ByteBuffer receiveToken(int tupleId) { + PSXReply reply = psxLocal.in(tupleId); + do { + try { + psxLocal.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + return data; + } + + private void sendToken(int tupleId, ByteBuffer data) { + for (int i = 0; i < psxSendServers.size(); i++) { + PSXLinda linda = psxSendServers.get(i); + linda.out(tupleId, data); + try { + linda.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/ring/RingTopologyManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/RingTopologyManager.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,41 @@ +package fdl.test.topology.ring; + +import java.io.IOException; + +import fdl.MetaEngine; +import fdl.test.topology.FDLindaNode; + +/** +* RingTopologyManager +* +* @author Kazuki Akamine +* +* FDLindaNode の Topology を管理する Server +* Ring 実験用の Main Class +* +*/ + +public class RingTopologyManager { + private static int localPort = 10000; + private static int nodeNum; + private static String usageString + = "RingTopologyManager -nodes NODENUM"; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-nodes".equals(args[i])) { + nodeNum = Integer.parseInt(args[++i]); + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode manager = new FDLindaNode(localPort); + MetaEngine me = new RingTopologyManagerEngine(manager.getMetaLinda(), nodeNum); + manager.setMetaEngine(me); + manager.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/topology/ring/RingTopologyManagerEngine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/RingTopologyManagerEngine.java Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,61 @@ +package fdl.test.topology.ring; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.MetaLinda; +import fdl.PSXReply; +import fdl.test.topology.TopologyManagerEngine; + +public class RingTopologyManagerEngine extends TopologyManagerEngine { + private int relayId = 10; + + // Constructor + public RingTopologyManagerEngine(MetaLinda ml, int nodeNum) { + super(ml, nodeNum); + } + + public void mainLoop() { + super.mainLoop(); + startRelay(); + endRelay(); + } + + protected void makeTopology() { + super.makeTopology(); + } + + @Override protected void makeConnection() { + // Ring 状の接続を定義 + for (int i = 0; i < nodes.length; i++) { + nodes[i].addConnection(nodes[(i+1)%nodes.length]); + } + } + + private void startRelay() { + String relayString = "test"; + ByteBuffer data = ByteBuffer.wrap(relayString.getBytes()); + nodes[0].linda.out(relayId, data); + try { + nodes[0].linda.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void endRelay() { + PSXReply reply; + reply = manager.in(relayId); + do { + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String resultData = new String(data.array()); + System.out.println("[DEBUG] RelayTime: " + resultData); + } + +} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/ProtocolEngine.java --- a/src/fdl/test/transfer/ProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -package fdl.test.transfer; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class ProtocolEngine extends Thread{ - private PSXLinda psx; - private String name; - FederatedLinda fdl1; - String host = "127.0.0.1"; - int port1 = 10001; - int port2 = 10002; - private int id = 10; - private PSXLinda psx1; - private boolean running = true; - private ByteBuffer data2 = ByteBuffer.allocate(10); - - public ProtocolEngine(String string, int port1, int port2) { - this.name = string; - this.port1 = port1; - this.port2 = port2; - } - - public void run(){ - fdl1 = FederatedLinda.init(); - try { - System.out.println(name); - psx = fdl1.open(host,port1); - System.out.println("Connect Host1"); - psx1 = fdl1.open(host,port2); - System.out.println("Connect Host2"); - // Host1にデータを送信する。 - testSend(psx); - // psxにデータを用意 - transfer(psx,psx1); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void testSend(PSXLinda psx2) throws IOException { - ByteBuffer send = ByteBuffer.allocate(10); - send.putInt(12); - send.flip(); - psx2.out(id, send); - psx2.sync(1); - } - - - private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { - PSXReply in = getpsx.in(id); - while (running) { - System.out.println(in.ready()); - if(in.ready()){ - //psx1にデータを書き出し - data2 = in.getData(); - sendpsx.out(id,data2); - //runningフラグをfalseする - running = false; - fdl1.sync(0); - break; - } - fdl1.sync(); - } - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/Server.java --- a/src/fdl/test/transfer/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package fdl.test.transfer; - -import fdl.FDLindaServ; - -public class Server extends Thread{ - int port; - private String name; - - public Server(String string, int i) { - port = i; - name = string; - } - - public void run(){ - String[] args = {"-p",Integer.toString(port)}; - System.out.println(name); - FDLindaServ.main(args); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/TestSend.java --- a/src/fdl/test/transfer/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -package fdl.test.transfer; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend extends Thread { - PSXLinda psxsend; - FederatedLinda fdlsend; - - public void run(){ - int id = 10; - int port = 10001; - String host = "127.0.0.1"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - fdlsend = FederatedLinda.init(); - try{ - psxsend = fdlsend.open(host,port); - System.out.println("Connect Host1"); - psxsend.out(id, send); - fdlsend.sync(); - System.out.println("Send Data"); - }catch (IOException e) { - System.out.println("例外:" + e); - } - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/TestTransfer.java --- a/src/fdl/test/transfer/TestTransfer.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package fdl.test.transfer; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class TestTransfer { - @Test - public void testTransfer() { - int port1 = 10001; - int port2 = 10002; - Server server1 = new Server("Server1",port1); - server1.start(); - Server server2 = new Server("Server2",port2); - server2.start(); - ProtocolEngine pe = new ProtocolEngine("ProtocolEngine",port1,port2); - pe.start(); - try { - pe.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - assertEquals(1,1); - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/FDLServWithSendMeta.java --- a/src/fdl/test/transfer/cluster/FDLServWithSendMeta.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package fdl.test.transfer.cluster; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import fdl.FDLindaServ; -import fdl.MetaLinda; - - -public class FDLServWithSendMeta extends FDLindaServ { - - public FDLServWithSendMeta(int port) throws IOException { - super(port); - } - - static String nextHost = null; - private static int localport = 10000; - static int chknum; - private static String bufstring; - private static int bufsize; - private static String txt; - private static int ring; - - @Override public void mainLoop() { - MetaLinda ml = new MetaLinda(tupleSpace, this); - MetaProtocolEngine mpe = new MetaProtocolEngine(ml, nextHost, chknum, bufsize, txt); - mpe.mainLoop(); - } - - public static void main(String[] args){ - for (int k=0; k "+start); - } catch (IOException e) { - e.printStackTrace(); - } - } - boolean connect = true; - while(connect){ - try { - initConnect(); - transfer = DF.format(new Date()); - transferlong = new Date().getTime(); - System.out.println("transfer Time => "+transfer); - for(int l=0; l<1000; l++){ - transfer(); - } - dataChk(); - write(); - connect = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - private void initConnect(){ - boolean connectpsx = true; - while(connectpsx){ - try { - psxsend = fdlmeta.open(host,port); - connectpsx = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } - } - } - } - - private void sendData() throws IOException{ - boolean connectSend = true; - ByteBuffer send = ByteBuffer.allocate(bufsize); - byte[] fdata = new byte[bufsize]; -// fdata = InetAddress.getLocalHost().getAddress(); - for(int n=0; n < fdata.length; n++ ){ - fdata[n] = 1; - } - send.put(fdata); - send.flip(); - while(connectSend){ - psxget.out(id, send); - psxget.sync(1); - System.out.println("Send Data"); - connectSend = false; - } - } - - public void transfer() throws IOException { - ByteBuffer data = ByteBuffer.allocate(bufsize); - boolean running = true; - PSXReply in = psxget.in(id); - while (running) { - if(in.ready()){ - data = in.getData(); - psxsend.out(id,data); - System.out.println("connect to => "+host); - psxsend.sync(1); - running = false; - break; - } - fdlmeta.sync(); - } - } - - public void dataChk() throws IOException{ - fdlmeta.sync(1); - boolean running2 = true; -// ByteBuffer data2 = ByteBuffer.allocate(bufsize); - PSXReply in2 = psxget.in(id); -// byte[] fdata2 = new byte[bufsize]; -// byte[] fdata3 = new byte[bufsize]; -// fdata = InetAddress.getLocalHost().getAddress(); -// for(int n=0; n < fdata2.length; n++ ){ -// fdata2[n] = 1; -// } - while (running2) { - psxget.sync(1); - if(in2.ready()) { -// data2 = in2.getData(); -// data2.get(fdata3); -// if (fdata2 == fdata3){ - time = DF.format(new Date()); - timelong = new Date().getTime(); - System.out.println("Around Time => "+time); -// } - running2 = false; - break; - } - } - } - - - public void write() throws IOException{ - long result = (timelong - transferlong); - String diff = String.valueOf(result); - File read = new File("resultTime"+bufsize+"_"+txt+".txt"); - read.createNewFile(); - FileWriter filewrite = new FileWriter(read); - if (start != "null"){ - filewrite.write("ServerStart => "+start+"\r\n"); - } - if (transfer != "null") { - filewrite.write("TransferTime => "+transfer+"\r\n"); - } - if (time != "null"){ - filewrite.write("AroundTime => "+time+"\r\n"); - } - if (diff != "null"){ - filewrite.write("DiffTime => "+diff+"\r\n"); - } - filewrite.close(); - } -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/ProtocolEngine.java --- a/src/fdl/test/transfer/cluster/ProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,147 +0,0 @@ -package fdl.test.transfer.cluster; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.net.InetAddress; -import java.nio.ByteBuffer; -import java.text.SimpleDateFormat; -import java.util.Date; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class ProtocolEngine extends Thread{ - static int id = 10; - static FederatedLinda fdl; - static PSXLinda getpsx; - static PSXLinda sendpsx; - static int port = 10000; - String getHost = null; - String sendHost = null; - private int chknum; - private int bufsize; - private String txt; - SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); - private String time; - private long timelong; - private String start; - private String transfer; - private long transferlong; - - public ProtocolEngine(String gethost, String sendhost, int chknum, int bufsize, String txt) { - this.getHost = gethost; - this.sendHost = sendhost; - this.chknum = chknum; - this.bufsize = bufsize; - this.txt = txt; - } - - public void run(){ - fdl = FederatedLinda.init(); - try { - getpsx = fdl.open(getHost,port); - System.out.println("Connect Host1"); - sendpsx = fdl.open(sendHost,port); - System.out.println("Connect Host2"); - // Host1にデータを送信する。 - if(chknum == 1){ - testSend(getpsx); - start = DF.format(new Date()); - System.out.println("Start Time => "+start); - } - // psxにデータを用意 - transfer(getpsx,sendpsx); - dataChk(); - write(); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void write() throws IOException { - long result = (timelong - transferlong); - String diff = String.valueOf(result); - File read = new File("resultTime"+bufsize+"_"+txt+".txt"); - read.createNewFile(); - FileWriter filewrite = new FileWriter(read); - if (start != "null"){ - filewrite.write("ServerStart => "+start+"\r\n"); - } - if (transfer != "null") { - filewrite.write("TransferTime => "+transfer+"\r\n"); - } - if (time != "null"){ - filewrite.write("AroundTime => "+time+"\r\n"); - } - if (diff != "null"){ - filewrite.write("DiffTime => "+diff+"\r\n"); - } - filewrite.close(); - } - - private void dataChk() throws IOException { - fdl.sync(1); - boolean running2 = true; - ByteBuffer data3 = ByteBuffer.allocate(bufsize); - PSXReply in2 = getpsx.in(id); - int localhost2 = InetAddress.getLocalHost().hashCode(); - while (running2) { - getpsx.sync(1); - if(in2.ready()) { - data3 = in2.getData(); - int i = data3.getInt(); - if (i == localhost2){ - time = DF.format(new Date()); - timelong = new Date().getTime(); - System.out.println("Around Time => "+time); - } - running2 = false; - break; - } - } - - } - - private void testSend(PSXLinda psx2) throws IOException { - boolean connectSend = true; - ByteBuffer send = ByteBuffer.allocate(bufsize); - int localhost = InetAddress.getLocalHost().hashCode(); - send.putInt(localhost); - send.flip(); - while(connectSend){ - psx2.out(id, send); - psx2.sync(1); - System.out.println("Send Data"); - connectSend = false; - } - } - - - private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException { - ByteBuffer data2 = ByteBuffer.allocate(bufsize); - PSXReply in = getpsx.in(id); - boolean running = true; - while (running) { - if(in.ready()){ - //psx1にデータを書き出し - data2 = in.getData(); - sendpsx.out(id,data2); - //runningフラグをfalseする - running = false; - transfer = DF.format(new Date()); - transferlong = new Date().getTime(); - System.out.println("transfer Time => "+transfer); - System.out.println("connect to => "+sendHost); - sendpsx.sync(1); - break; - } - fdl.sync(); - } - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/ProtocolEngineMain.java --- a/src/fdl/test/transfer/cluster/ProtocolEngineMain.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -package fdl.test.transfer.cluster; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.text.SimpleDateFormat; -import java.util.Date; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - -public class ProtocolEngineMain { - static int id = 10; - static FederatedLinda fdl; - static PSXLinda getpsx; - static PSXLinda sendpsx; - static int port = 10000; - private static String getHost = null; - private static String sendHost = null; - - public static void main(String[] args) { - for (int i=0; i "+i); - //runningフラグをfalseする - running = false; - sendpsx.sync(1); - System.out.println("Transfer Data"); - break; - } - SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); - System.out.println(DF.format(new Date())); - fdl.sync(); - } - - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/Server.java --- a/src/fdl/test/transfer/cluster/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -package fdl.test.transfer.cluster; - -import fdl.FDLindaServ; - -public class Server extends Thread{ - int port; - - public Server() { - } - - public void run(){ - String[] args = {"-p",Integer.toString(port)}; - FDLindaServ.main(args); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/ServerMain.java --- a/src/fdl/test/transfer/cluster/ServerMain.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -package fdl.test.transfer.cluster; - -import fdl.FDLindaServ; - -public class ServerMain{ - public static void main(String[] args){ - int port = 10000; - String[] args1 = {"-p",Integer.toString(port)}; - FDLindaServ.main(args1); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/TestGet.java --- a/src/fdl/test/transfer/cluster/TestGet.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -package fdl.test.transfer.cluster; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class TestGet extends Thread { - private PSXLinda psxget; - private FederatedLinda fdlget; - private int port; - String host = "127.0.0.1"; - private ByteBuffer data = ByteBuffer.allocate(1024); - - public TestGet(int port1) { - this.port = port1; - } - - public void run(){ - int id = 10; - int i; - fdlget = FederatedLinda.init(); - try { - System.out.println("TestGet Start"); - psxget = fdlget.open(host, port); - System.out.println("Host1 connected"); - //getにidのタプルを取り出す - PSXReply dataGet = psxget.in(id); - System.out.println("dataget"); - System.out.println(dataGet.ready()); - if(dataGet.ready()){ - System.out.println(dataGet); - data = dataGet.getData(); - fdlget.sync(1); - i = data.getInt(); - System.out.println(i); - } - } catch (IOException e) { - try { - Thread.sleep(10); - } catch (InterruptedException e1) { - } - } - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/cluster/TestSend.java --- a/src/fdl/test/transfer/cluster/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -package fdl.test.transfer.cluster; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend { - static PSXLinda psxsend; - static FederatedLinda fdlsend; - private static int port = 10000; - - public static void main(String[] args){ - int id = 10; - int i; - String host = "cls001.cs.ie.u-ryukyu.ac.jp"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - i = send.getInt(); - send.rewind(); - fdlsend = FederatedLinda.init(); - boolean connect = true; - while(connect) { - try{ - System.out.println("TestSend Start"); - psxsend = fdlsend.open(host,port); - System.out.println("Set Data = " +i); - System.out.println("Connect Host1"); - psxsend.out(id, send); - System.out.println("regist Que"); - fdlsend.sync(); - System.out.println("Send Data"); - connect = false; - }catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/three/ProtocolEngine.java --- a/src/fdl/test/transfer/three/ProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -package fdl.test.transfer.three; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class ProtocolEngine extends Thread{ - private PSXLinda psx; - private String name; - FederatedLinda fdl1; - String host = "127.0.0.1"; - int port1; - int port2; - private int id = 10; - private PSXLinda psx1; - private boolean running = true; - private ByteBuffer data2 = ByteBuffer.allocate(10); - private int count = 1; - - public ProtocolEngine(String string, int port1, int port2) { - this.name = string; - this.port1 = port1; - this.port2 = port2; - } - - public void run(){ - fdl1 = FederatedLinda.init(); - boolean connect = true; - while(connect){ - try { - initConnect(); - transfer(); - connect = false; - } catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - private void initConnect() throws IOException { - System.out.println(name); - psx = fdl1.open(host,port1); - System.out.println("Connect "+port1); - psx1 = fdl1.open(host,port2); - System.out.println("Connect "+port2); - } - - private void transfer() throws IOException { - PSXReply in = psx.in(id); - System.out.println("PSXReply "+port1 +": "+port2 +": "); - while (running) { - if(in.ready()){ - data2 = in.getData(); - int i = data2.getInt(); - data2.rewind(); - //outしたbytebufferの変更をこれ以降やっちゃいけない - psx1.out(id,data2); - - System.out.println("IntData0 "+port1 +": "+port2 +": " +i); - fdl1.sync(1); - if (count++>3) { - running = false; - break; - } - System.out.println("IntData1 "+port1 +": "+port2 +": " +i); - } - fdl1.sync(); - } - assertEquals(4,count); - } - - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/three/Server.java --- a/src/fdl/test/transfer/three/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package fdl.test.transfer.three; - -import fdl.FDLindaServ; - -public class Server extends Thread { - int port; - private String name; - - public Server(String string, int i) { - port = i; - name = string; - } - - public void run(){ - String[] args = {"-p",Integer.toString(port)}; - System.out.println(name+"start"); - FDLindaServ.main(args); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/three/TestGet.java --- a/src/fdl/test/transfer/three/TestGet.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package fdl.test.transfer.three; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class TestGet extends Thread { - PSXLinda psxget; - FederatedLinda fdlget; - private int port; - String host = "127.0.0.1"; - private ByteBuffer data = ByteBuffer.allocate(10); - - public TestGet(int port1) { - this.port = port1; - } - - public void run(){ - int id = 10; - int i; - fdlget = FederatedLinda.init(); - boolean connecttest = true; - while(connecttest){ - try { - getData(id); - i = data.getInt(); - System.out.println(i); - connecttest = false; - } catch (IOException e) { - e.printStackTrace(); - connecttest = true; - } - } - } - - private void getData(int id) throws IOException { - psxget = fdlget.open(host, port); - PSXReply get = psxget.in(id); - boolean running = false; - while(running){ - if(get.ready()){ - data = get.getData(); - running = false; - fdlget.sync(0); - break; - } - fdlget.sync(); - } - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/three/TestSend.java --- a/src/fdl/test/transfer/three/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -package fdl.test.transfer.three; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend extends Thread { - PSXLinda psxsend; - FederatedLinda fdlsend; - private int port; - - public TestSend(int port1) { - this.port = port1; - } - - public void run(){ - int id = 10; - int i; - String host = "127.0.0.1"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - i = send.getInt(); - send.rewind(); - fdlsend = FederatedLinda.init(); - boolean connect = true; - while(connect) { - try{ - psxsend = fdlsend.open(host,port); - System.out.println("Set Data = " +i); - System.out.println("Connect Host1"); - psxsend.out(id, send); - System.out.println("regist Que"); - fdlsend.sync(1); - System.out.println("Send Data"); - connect = false; - }catch (IOException e) { - try { - Thread.sleep(20); - } catch (InterruptedException e1) { - } - } - } - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/three/TestTransfer.java --- a/src/fdl/test/transfer/three/TestTransfer.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package fdl.test.transfer.three; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - - -public class TestTransfer { - Server[] servers; - ProtocolEngine[] engines; - final int serverCount = 3; - final int startPort = 10011; - @Test - public void testTransfer() { - int port = startPort; - servers = new Server[serverCount]; - engines = new ProtocolEngine[serverCount]; - - for (int i = 0; i < serverCount; i++) { - servers[i] = new Server("Server"+(i+1),port+i); - servers[i].start(); - } - - TestSend send = new TestSend(startPort); - send.start(); - try{ - send.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - for (int i = 0; i < serverCount; i++) { - engines[i] = new ProtocolEngine("ProtocolEngine "+(i+1) +":port " +(port+i) +":port "+(netporot(port+i)),port+i,netporot(port+i)); - engines[i].start(); - } - try { - for (ProtocolEngine e: engines){ - e.join(); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - assertEquals(1,1); - } - private int netporot(int i) { - i++; - if(i >= startPort+serverCount) { - return startPort; - } - return i; - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/two/ProtocolEngine.java --- a/src/fdl/test/transfer/two/ProtocolEngine.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -package fdl.test.transfer.two; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class ProtocolEngine extends Thread{ - private static PSXLinda psx; - private static String name; - static FederatedLinda fdl1; - static String host = "127.0.0.1"; - static int port1 = 10001; - static int port2 = 10002; - private static int id = 10; - private static PSXLinda psx1; - private static boolean running; - private static ByteBuffer data2 = ByteBuffer.allocate(10); - - public static void main(String[] args){ - fdl1 = FederatedLinda.init(); - try { - System.out.println(name); - psx = fdl1.open(host,port1); - System.out.println("Connect Host1"); - psx1 = fdl1.open(host,port2); - System.out.println("Connect Host2"); - // Host1にデータを送信する。 - testSend(psx); - // psxにデータを用意 - transfer(psx,psx1); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static void testSend(PSXLinda psx2) throws IOException { - ByteBuffer send = ByteBuffer.allocate(10); - send.putInt(12); - send.flip(); - psx2.out(id, send); - psx2.sync(); - } - - - private static void transfer(PSXLinda psx2, PSXLinda psx12) throws IOException { - PSXReply in = psx2.in(id); - while (running) { - if(in.ready()){ - //psx1にデータを書き出し - data2 = in.getData(); - psx12.out(id,data2); - //runningフラグをfalseする - running = false; - fdl1.sync(0); - break; - } - fdl1.sync(); - } - - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/two/Server.java --- a/src/fdl/test/transfer/two/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package fdl.test.transfer.two; - -import fdl.FDLindaServ; - -public class Server extends Thread { - int port; - private String name; - - public Server(String string, int i) { - port = i; - name = string; - } - - public void run(){ - String[] args = {"-p",Integer.toString(port)}; - System.out.println(name); - FDLindaServ.main(args); - } - -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/two/TestGet.java --- a/src/fdl/test/transfer/two/TestGet.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -package fdl.test.transfer.two; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - - -public class TestGet extends Thread { - private static PSXLinda psxget; - private static FederatedLinda fdlget; - private static int port = 10001; - static String host = "127.0.0.1"; - private static ByteBuffer data = ByteBuffer.allocate(1024); - - public static void main(String[] args){ - int id = 10; - int i; - fdlget = FederatedLinda.init(); - try { - System.out.println("TestGet Start"); - psxget = fdlget.open(host, port); - System.out.println("Host1 connected"); - //getにidのタプルを取り出す - PSXReply dataGet = psxget.in(id); - System.out.println("dataget"); - System.out.println(dataGet.ready()); - if(dataGet.ready()){ - System.out.println(dataGet); - data = dataGet.getData(); - fdlget.sync(1); - i = data.getInt(); - System.out.println(i); - } - } catch (IOException e) { - try { - Thread.sleep(10); - } catch (InterruptedException e1) { - } - } - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/two/TestSend.java --- a/src/fdl/test/transfer/two/TestSend.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -package fdl.test.transfer.two; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; - -public class TestSend extends Thread { - PSXLinda psxsend; - FederatedLinda fdlsend; - private int port; - - public TestSend(int port1) { - this.port = port1; - } - - public void run(){ - int id = 10; - int i; - String host = "127.0.0.1"; - ByteBuffer send = ByteBuffer.allocate(1024); - send.putInt(12); - send.flip(); - i = send.getInt(); - send.rewind(); - fdlsend = FederatedLinda.init(); - boolean connect = true; - while(connect) { - try{ - System.out.println("TestSend Start"); - psxsend = fdlsend.open(host,port); - System.out.println("Set Data = " +i); - System.out.println("Connect Host1"); - psxsend.out(id, send); - System.out.println("regist Que"); - fdlsend.sync(1); - System.out.println("Send Data"); - connect = false; - }catch (IOException e) { - try { - Thread.sleep(40); - } catch (InterruptedException e1) { - } - } - } - } - - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test/transfer/two/TestTransfer.java --- a/src/fdl/test/transfer/two/TestTransfer.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -package fdl.test.transfer.two; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class TestTransfer { - @Test - public void testTransfer() throws InterruptedException { - int port = 10001; - Server server = new Server("Server1",port); - server.start(); - server.join(); - TestSend send = new TestSend(port); - send.start(); - - send.join(); - - assertEquals(1,1); - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/CountTest.java --- a/src/fdl/test2/CountTest.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -package fdl.test2; - -class CountA implements Runnable { - - public void run() { - for (int i=0; i <= 5; i++) { - System.out.println("A: " + i); - } - } -} - -class CountB implements Runnable { - - public void run() { - for (int i=5; i >= 0; i--) { - System.out.println(" B: " + i); - } - } -} - -class CountTest { - public static void main(String[] args) { - // ランナブルクラスのインスタンス化 - CountA runA = new CountA(); - CountB runB = new CountB(); - System.out.println("Runnable Class のインスタンス化終了"); - - // スレッドのインスタンス化 - Thread threadA = new Thread(runA); - Thread threadB = new Thread(runB); - System.out.println("Thread へ受け渡し終了"); - - // スレッドの開始 - threadA.start(); - threadB.start(); - System.out.println("Thread の start() 終了"); - System.out.println("適時自動的に run() が実行される"); - - - } -} - diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/Server.java --- a/src/fdl/test2/Server.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -package fdl.test2; - -import java.io.IOException; -import java.io.PrintWriter; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.Date; - -public class Server extends Thread{ - static int Port = 10007; - - public void run(){ - try { - ServerSocket server = new ServerSocket(Port); - Socket sock =null; - System.out.println("Server Start"); - sock = server.accept(); - System.out.println("Connect"); - PrintWriter ps = new PrintWriter(sock.getOutputStream()); - Date d = new Date(); - ps.print(d + "\r\n"); - ps.flush(); - sock.close(); // クライアントからの接続を切断 - System.out.println("Connection Closed"); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static void main(String[] args) { - Server sv = new Server(); - sv.start(); - } -} \ No newline at end of file diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/TestRead.java --- a/src/fdl/test2/TestRead.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -package fdl.test2; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; - -public class TestRead { - - public static void main(String[] args) { - try { - File inputFile = new File(args[0]); - FileReader in = new FileReader(inputFile); - BufferedReader br = new BufferedReader(in); - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); - } - br.close(); - in.close(); - } catch (IOException e) { - System.out.println(e); - } - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/TestRing.java --- a/src/fdl/test2/TestRing.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -package fdl.test2; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.nio.ByteBuffer; - -import fdl.FederatedLinda; -import fdl.PSXLinda; -import fdl.PSXReply; - -public class TestRing { - static int id; - public static void main(String args[]){ - - try{ - FederatedLinda fdl; - PSXLinda psx; - String host = "127.0.0.1"; - PSXReply r; - - FileReader in = new FileReader("nodelist"); - BufferedReader br = new BufferedReader(in); - String line; - - while ((line = br.readLine()) != null) { - int i1 = Integer.parseInt(line); - fdl = FederatedLinda.init(); - psx = fdl.open(host,i1); - r = psx.in(65535); - fdl.sync(1); - - ByteBuffer data = ByteBuffer.allocate(10); - data.putInt(10); - data.flip(); - - psx.out(1,data); - while(!r.ready()){ - psx.sync(); - } - } - br.close(); - in.close(); - - - }catch (IOException e) { - System.err.println("Communication failure."); - } - - } -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/client.java --- a/src/fdl/test2/client.java Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -package fdl.test2; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.Socket; - -public class client { - public static void main(String arg[]) throws IOException{ - String www = arg[0]; - int port = 10007; - Socket sock; - BufferedReader dis; -// PrintWriter ps; - sock = new Socket(www,port); - dis = new BufferedReader(new InputStreamReader(sock.getInputStream())); -// ps = new PrintWriter(sock.getOutputStream(),true); -// ps.println("Get"+arg[1]); -// String s = null; -// while((s = dis.readLine()) != null){ - System.out.println(dis); -// } - sock.close(); - } - -} diff -r fce61ee25d20 -r 2a64c13e743a src/fdl/test2/nodelist --- a/src/fdl/test2/nodelist Wed Feb 08 18:02:00 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -10000 -10001 diff -r fce61ee25d20 -r 2a64c13e743a tools/StartFDLindaServ2.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/StartFDLindaServ2.py Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,29 @@ +#! /usr/bin/env python +"""start process +""" +import pexpect +import re +import time +import sys +import os + +def main(base_port, ldserv_num): + + if not os.environ.has_key('LDSERV'): + ldserv = "java -classpath FedLinda.jar fdl.FDLindaServ" + else: + ldserv = os.environ['LDSERV'] + + for num in range(0,ldserv_num): + cmd = "".join([ldserv + ' -p ' + str(base_port+num) + ' &']) + os.system(cmd) + print 'start :', cmd + + +if __name__ == "__main__": + + if (len(sys.argv) != 3): + print 'Usage : %s ' % sys.argv[0] + sys.exit(1) + + main(int(sys.argv[1]), int(sys.argv[2])) diff -r fce61ee25d20 -r 2a64c13e743a tools/StopProcess.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/StopProcess.py Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,46 @@ +#! /usr/bin/env python +"""find and kill processes +""" +import pexpect +import re +import sys +import os + +def main(proc): + child = pexpect.spawn('ps aucxwww') + child.setwinsize(100,300) + child.expect(pexpect.EOF) + + pslist = child.before.splitlines() + # separate string by space + fmt = re.split(' +', pslist[0]) + + if fmt[-1] == '': + fmt.pop() + if fmt[0] == '': + fmt.pop(0) + lenfmt = len(fmt) + + # indexing, key is format of output of ps command, value is number of list + index = dict([(k,v) for (k,v) in zip(fmt, range(0,lenfmt))]) + + # find ".*.*" in 'COMMAND' part + pars = re.compile(".*"+proc+".*") + + for l in pslist[1:]: + elm = re.split(' +',l ,lenfmt) + cmd = elm[index['COMMAND']] + result = pars.match(cmd) + + if (result is not None): + os.system('kill -KILL ' + elm[index['PID']]) + print 'stop', elm[index['PID']], ':', cmd + + +if __name__== "__main__": + if (len(sys.argv) != 2): + print "Usage : %s " % sys.argv[0] + sys.exit(1) + + main(sys.argv[1]) + diff -r fce61ee25d20 -r 2a64c13e743a tools/pexpect.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/pexpect.py Wed Feb 08 18:02:40 2012 +0900 @@ -0,0 +1,1203 @@ +"""Pexpect is a Python module for spawning child applications; +controlling them; and responding to expected patterns in their output. +Pexpect can be used for automating interactive applications such as +ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts +for duplicating software package installations on different servers. +It can be used for automated software testing. Pexpect is in the spirit of +Don Libes' Expect, but Pexpect is pure Python. Other Expect-like +modules for Python require TCL and Expect or require C extensions to +be compiled. Pexpect does not use C, Expect, or TCL extensions. It +should work on any platform that supports the standard Python pty +module. The Pexpect interface focuses on ease of use so that simple +tasks are easy. + +There are two main interfaces to Pexpect. You can call the function: + pexpect.run() +to execute a command and return the output. +Do no use this on interactive commands that expect input. +This is a handy replacment for os.system(). +The more useful interface is the class: + pexpect.spawn() +This creates a spawn instance. This will start the command that you specify. +You can then interact with the child command through the spawn instance. +Most commands, including ssh, cannot tell that they are being run inside +of a script. This works even for commands that ask for passwords or +other input outside of the normal stdio streams. + +Pexpect is Open Source, Free, and all that good stuff. +License: Python Software Foundation License + http://www.opensource.org/licenses/PythonSoftFoundation.html + +Noah Spurrier +Richard Holden +Marco Molteni +Kimberley Burchett +Robert Stone +Mike Snitzer +Marti Raudsepp +Matt +Hartmut Goebel +Chad Schroeder +Erick Tryzelaar +Dave Kirby +Ids vander Molen +George Todd +Noel Taylor +Nicolas D. Cesar +(Let me know if I forgot anyone.) + +$Revision: 1.1.1.1 $ +$Date: 2006/04/01 06:03:29 $ +""" + +try: + import os, sys, time + import select + import string + import re + import struct + import resource + import types + import pty + import tty + import termios + import fcntl + import errno + import traceback + import signal +except ImportError, e: + raise ImportError (str(e) + """ +A critical module was not found. Probably this operating system does not support it. +Pexpect is intended for UNIX-like operating systems.""") + +__version__ = '2.0' +__revision__ = '$Revision: 1.1.1.1 $' +__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which', 'split_command_line', + '__version__', '__revision__'] + +# Exception classes used by this module. +class ExceptionPexpect(Exception): + """Base class for all exceptions raised by this module. + """ + def __init__(self, value): + self.value = value + def __str__(self): + return str(self.value) + def get_trace(self): + """This returns an abbreviated stack trace with lines that only concern the caller. + In other words, the stack trace inside the Pexpect module is not included. + """ + tblist = traceback.extract_tb(sys.exc_info()[2]) + tblist = filter(self.__filter_not_pexpect, tblist) + tblist = traceback.format_list(tblist) + return ''.join(tblist) + def __filter_not_pexpect(self, trace_list_item): + if trace_list_item[0].find('pexpect.py') == -1: + return 1 + else: + return 0 +class EOF(ExceptionPexpect): + """Raised when EOF is read from a child. + """ +class TIMEOUT(ExceptionPexpect): + """Raised when a read time exceeds the timeout. + """ +##class TIMEOUT_PATTERN(TIMEOUT): +## """Raised when the pattern match time exceeds the timeout. +## This is different than a read TIMEOUT because the child process may +## give output, thus never give a TIMEOUT, but the output +## may never match a pattern. +## """ +##class MAXBUFFER(ExceptionPexpect): +## """Raised when a scan buffer fills before matching an expected pattern.""" + +def run (command, timeout=-1, withexitstatus=0, events=None, extra_args=None): + """This function runs the given command; waits for it to finish; + then returns all output as a string. STDERR is included in output. + If the full path to the command is not given then the path is searched. + + Note that lines are terminated by CR/LF (\\r\\n) combination + even on UNIX-like systems because this is the standard for pseudo ttys. + If you set withexitstatus to true, then run will return a tuple of + (command_output, exitstatus). If withexitstatus is false then this + returns just command_output. + + Examples: + Start the apache daemon on the local machine: + from pexpect import * + run ("/usr/local/apache/bin/apachectl start") + Check in a file using SVN: + from pexpect import * + run ("svn ci -m 'automatic commit' my_file.py") + Run a command and capture exit status: + from pexpect import * + (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1) + + Tricky Examples: + The following will run SSH and execute 'ls -l' on the remote machine. + The password 'secret' will be sent if the '(?i)password' pattern is ever seen. + run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'}) + + This will start mencoder to rip a video from DVD. This will also display + progress ticks every 5 seconds as it runs. + from pexpect import * + def print_ticks(d): + print d['event_count'], + run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5) + + The 'events' argument should be a dictionary of patterns and responses. + Whenever one of the patterns is seen in the command out + run() will send the associated response string. Note that you should + put newlines in your string if Enter is necessary. + The responses may also contain callback functions. + Any callback is function that takes a dictionary as an argument. + The dictionary contains all the locals from the run() function, so + you can access the child spawn object or any other variable defined + in run() (event_count, child, and extra_args are the most useful). + A callback may return True to stop the current run process otherwise + run() continues until the next event. + A callback may also return a string which will be sent to the child. + 'extra_args' is not used by directly run(). It provides a way to pass data to + a callback function through run() through the locals dictionary passed to a callback. + """ + if timeout == -1: + child = spawn(command, maxread=2000) + else: + child = spawn(command, timeout=timeout, maxread=2000) + if events is not None: + patterns = events.keys() + responses = events.values() + else: + patterns=None # We assume that EOF or TIMEOUT will save us. + responses=None + child_result_list = [] + event_count = 0 + while 1: + try: + index = child.expect (patterns) + if type(child.after) is types.StringType: + child_result_list.append(child.before + child.after) + else: # child.after may have been a TIMEOUT or EOF, so don't cat those. + child_result_list.append(child.before) + if type(responses[index]) is types.StringType: + child.send(responses[index]) + elif type(responses[index]) is types.FunctionType: + callback_result = responses[index](locals()) + sys.stdout.flush() + if type(callback_result) is types.StringType: + child.send(callback_result) + elif callback_result: + break + else: + raise TypeError ('The callback must be a string or function type.') + event_count = event_count + 1 + except TIMEOUT, e: + child_result_list.append(child.before) + break + except EOF, e: + child_result_list.append(child.before) + break + child_result = ''.join(child_result_list) + if withexitstatus: + child.close() + return (child_result, child.exitstatus) + else: + return child_result + +class spawn: + """This is the main class interface for Pexpect. + Use this class to start and control child applications. + """ + + def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None): + """This is the constructor. The command parameter may be a string + that includes a command and any arguments to the command. For example: + p = pexpect.spawn ('/usr/bin/ftp') + p = pexpect.spawn ('/usr/bin/ssh user@example.com') + p = pexpect.spawn ('ls -latr /tmp') + You may also construct it with a list of arguments like so: + p = pexpect.spawn ('/usr/bin/ftp', []) + p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com']) + p = pexpect.spawn ('ls', ['-latr', '/tmp']) + After this the child application will be created and + will be ready to talk to. For normal use, see expect() and + send() and sendline(). + + The maxread attribute sets the read buffer size. + This is maximum number of bytes that Pexpect will try to read from a TTY at one time. + The default buffer size is 1 (unbuffered). Setting this value higher + will help performance in cases where large amounts of output are read back from the child. + This feature is useful in conjunction with searchwindowsize. + + The searchwindowsize attribute sets the how far back in + the incomming seach buffer Pexpect will search for pattern matches. + Every time Pexpect reads some data from the child it will append the data to + the incomming buffer. The default is to search from the beginning of the + imcomming buffer each time new data is read from the child. + But this is very inefficient if you are running a command that + generates a large amount of data where you want to match + The searchwindowsize does not effect the size of the incomming data buffer. + You will still have access to the full buffer after expect() returns. + + The logfile member turns on or off logging. + All input and output will be copied to the given file object. + Set logfile to None to stop logging. This is the default. + Set logfile to sys.stdout to echo everything to standard output. + The logfile is flushed after each write. + Example 1: + child = pexpect.spawn('some_command') + fout = file('mylog.txt','w') + child.logfile = fout + Example 2: + child = pexpect.spawn('some_command') + child.logfile = sys.stdout + + The delaybeforesend helps overcome weird behavior that many users were experiencing. + The typical problem was that a user would expect() a "Password:" prompt and + then immediately call sendline() to send the password. The user would then + see that their password was echoed back to them. Of course, passwords don't + normally echo. The problem is caused by the fact that most applications + print out the "Password" prompt and then turn off stdin echo, but if you + send your password before the application turned off echo, then you get + your password echoed. Normally this wouldn't be a problem when interacting + with a human at a real heyboard. If you introduce a slight delay just before + writing then this seems to clear up the problem. This was such a common problem + for many users that I decided that the default pexpect behavior + should be to sleep just before writing to the child application. + 1/10th of a second (100 ms) seems to be enough to clear up the problem. + You can set delaybeforesend to 0 to return to the old behavior. + + Note that spawn is clever about finding commands on your path. + It uses the same logic that "which" uses to find executables. + + If you wish to get the exit status of the child you must call + the close() method. The exit or signal status of the child will be + stored in self.exitstatus or self.signalstatus. + If the child exited normally then exitstatus will store the exit return code and + signalstatus will be None. + If the child was terminated abnormally with a signal then signalstatus will store + the signal value and exitstatus will be None. + If you need more detail you can also read the self.status member which stores + the status returned by os.waitpid. You can interpret this using + os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. + """ + self.STDIN_FILENO = pty.STDIN_FILENO + self.STDOUT_FILENO = pty.STDOUT_FILENO + self.STDERR_FILENO = pty.STDERR_FILENO + self.stdin = sys.stdin + self.stdout = sys.stdout + self.stderr = sys.stderr + + self.patterns = None + self.before = None + self.after = None + self.match = None + self.match_index = None + self.terminated = 1 + self.exitstatus = None + self.signalstatus = None + self.status = None + self.flag_eof = 0 + self.pid = None + self.child_fd = -1 # initially closed + self.timeout = timeout + self.delimiter = EOF + self.logfile = logfile + self.maxread = maxread # Max bytes to read at one time into buffer. + self.buffer = '' # This is the read buffer. See maxread. + self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched. + self.delaybeforesend = 0.1 # Sets sleep time used just before sending data to child. + self.softspace = 0 # File-like object. + self.name = '<' + repr(self) + '>' # File-like object. + self.encoding = None # File-like object. + self.closed = 1 # File-like object. + + if type (args) != type([]): + raise TypeError ('The second argument, args, must be a list.') + + if args == []: + self.args = split_command_line(command) + self.command = self.args[0] + else: + self.args = args[:] # work with a copy + self.args.insert (0, command) + self.command = command + + command_with_path = which(self.command) + if command_with_path == None: + raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command) + self.command = command_with_path + self.args[0] = self.command + + self.name = '<' + ' '.join (self.args) + '>' + self.__spawn() + + def __del__(self): + """This makes sure that no system resources are left open. + Python only garbage collects Python objects. OS file descriptors + are not Python objects, so they must be handled explicitly. + If the child file descriptor was opened outside of this class + (passed to the constructor) then this does not close it. + """ + if self.closed: + return + self.close() + + def __str__(self): + """This returns the current state of the pexpect object as a string. + """ + s = [] + s.append(repr(self)) + s.append('version: ' + __version__ + ' (' + __revision__ + ')') + s.append('command: ' + str(self.command)) + s.append('args: ' + str(self.args)) + if self.patterns is None: + s.append('patterns: None') + else: + s.append('patterns:') + for p in self.patterns: + if type(p) is type(re.compile('')): + s.append(' ' + str(p.pattern)) + else: + s.append(' ' + str(p)) + s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:]) + s.append('before (last 100 chars): ' + str(self.before)[-100:]) + s.append('after: ' + str(self.after)) + s.append('match: ' + str(self.match)) + s.append('match_index: ' + str(self.match_index)) + s.append('exitstatus: ' + str(self.exitstatus)) + s.append('flag_eof: ' + str(self.flag_eof)) + s.append('pid: ' + str(self.pid)) + s.append('child_fd: ' + str(self.child_fd)) + s.append('timeout: ' + str(self.timeout)) + s.append('delimiter: ' + str(self.delimiter)) + s.append('logfile: ' + str(self.logfile)) + s.append('maxread: ' + str(self.maxread)) + s.append('searchwindowsize: ' + str(self.searchwindowsize)) + s.append('delaybeforesend: ' + str(self.delaybeforesend)) + return '\n'.join(s) + + def __spawn(self): + """This starts the given command in a child process. + This does all the fork/exec type of stuff for a pty. + This is called by __init__. + """ + # The pid and child_fd of this object get set by this method. + # Note that it is difficult for this method to fail. + # You cannot detect if the child process cannot start. + # So the only way you can tell if the child process started + # or not is to try to read from the file descriptor. If you get + # EOF immediately then it means that the child is already dead. + # That may not necessarily be bad because you may haved spawned a child + # that performs some task; creates no stdout output; and then dies. + + assert self.pid == None, 'The pid member should be None.' + assert self.command != None, 'The command member should not be None.' + + try: + self.pid, self.child_fd = pty.fork() + except OSError, e: + raise ExceptionPexpect('Pexpect: pty.fork() failed: ' + str(e)) + + if self.pid == 0: # Child + try: # Some platforms do not like setwinsize (Cygwin). + self.child_fd = sys.stdout.fileno() # used by setwinsize() + self.setwinsize(24, 80) + except: + pass + # Do not allow child to inherit open file descriptors from parent. + max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0] + for i in range (3, max_fd): + try: + os.close (i) + except OSError: + pass + + # I don't know why this works, but ignoring SIGHUP fixes a + # problem when trying to start a Java daemon with sudo + # (specifically, Tomcat). + signal.signal(signal.SIGHUP, signal.SIG_IGN) + + os.execv(self.command, self.args) + + # Parent + self.terminated = 0 + self.closed = 0 + + def fileno (self): # File-like object. + """This returns the file descriptor of the pty for the child. + """ + return self.child_fd + + def close (self, force=0): # File-like object. + """This closes the connection with the child application. + Note that calling close() more than once is valid. + This emulates standard Python behavior with files. + Set force to 1 if you want to make sure that the child is terminated + (SIGKILL is sent if the child ignores SIGHUP and SIGINT). + """ + if self.child_fd != -1: + self.flush() + os.close (self.child_fd) + self.child_fd = -1 + self.closed = 1 + time.sleep(0.1) # Give kernel time to update process status. + if self.isalive(): + if not self.terminate(force): + raise ExceptionPexpect ('close() could not terminate the child using terminate()') + + def flush (self): # File-like object. + """This does nothing. It is here to support the interface for a File-like object. + """ + pass + + def isatty (self): # File-like object. + """This returns 1 if the file descriptor is open and connected to a tty(-like) device, else 0. + """ + return os.isatty(self.child_fd) + + def setecho (self, on): + """This sets the terminal echo mode on or off. + Note that anything the child sent before the echo will be lost, so + you should be sure that your input buffer is empty before you setecho. + For example, the following will work as expected. + p = pexpect.spawn('cat') + p.sendline ('1234') # We will see this twice (once from tty echo and again from cat). + p.expect (['1234']) + p.expect (['1234']) + p.setecho(0) # Turn off tty echo + p.sendline ('abcd') # We will set this only once (echoed by cat). + p.sendline ('wxyz') # We will set this only once (echoed by cat) + p.expect (['abcd']) + p.expect (['wxyz']) + The following WILL NOT WORK because the lines sent before the setecho + will be lost: + p = pexpect.spawn('cat') + p.sendline ('1234') # We will see this twice (once from tty echo and again from cat). + p.setecho(0) # Turn off tty echo + p.sendline ('abcd') # We will set this only once (echoed by cat). + p.sendline ('wxyz') # We will set this only once (echoed by cat) + p.expect (['1234']) + p.expect (['1234']) + p.expect (['abcd']) + p.expect (['wxyz']) + """ + self.child_fd + new = termios.tcgetattr(self.child_fd) + if on: + new[3] = new[3] | termios.ECHO + else: + new[3] = new[3] & ~termios.ECHO + # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent + # and blocked on some platforms. TCSADRAIN is probably ideal if it worked. + termios.tcsetattr(self.child_fd, termios.TCSANOW, new) + + def read_nonblocking (self, size = 1, timeout = -1): + """This reads at most size characters from the child application. + It includes a timeout. If the read does not complete within the + timeout period then a TIMEOUT exception is raised. + If the end of file is read then an EOF exception will be raised. + If a log file was set using setlog() then all data will + also be written to the log file. + + If timeout==None then the read may block indefinitely. + If timeout==-1 then the self.timeout value is used. + If timeout==0 then the child is polled and + if there was no data immediately ready then this will raise a TIMEOUT exception. + + The "timeout" refers only to the amount of time to read at least one character. + This is not effected by the 'size' parameter, so if you call + read_nonblocking(size=100, timeout=30) and only one character is + available right away then one character will be returned immediately. + It will not wait for 30 seconds for another 99 characters to come in. + + This is a wrapper around os.read(). + It uses select.select() to implement a timeout. + """ + if self.child_fd == -1: + raise ValueError ('I/O operation on closed file in read_nonblocking().') + + if timeout == -1: + timeout = self.timeout + + # Note that some systems such as Solaris do not give an EOF when + # the child dies. In fact, you can still try to read + # from the child_fd -- it will block forever or until TIMEOUT. + # For this case, I test isalive() before doing any reading. + # If isalive() is false, then I pretend that this is the same as EOF. + if not self.isalive(): + r, w, e = select.select([self.child_fd], [], [], 0) # timeout of 0 means "poll" + if not r: + self.flag_eof = 1 + raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.') + elif sys.platform.lower().find('irix') >= 0: + # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive. + # This adds a 2 second delay, but only when the child is terminated + r, w, e = select.select([self.child_fd], [], [], 2) + if not r and not self.isalive(): + self.flag_eof = 1 + raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.') + + r, w, e = select.select([self.child_fd], [], [], timeout) + if not r: + if not self.isalive(): + # Some platforms, such as Irix, will claim that their processes are alive; + # then timeout on the select; and then finally admit that they are not alive. + self.flag_eof = 1 + raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.') + else: + raise TIMEOUT ('Timeout exceeded in read_nonblocking().') + + if self.child_fd in r: + try: + s = os.read(self.child_fd, size) + except OSError, e: # Linux does this + self.flag_eof = 1 + raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.') + if s == '': # BSD style + self.flag_eof = 1 + raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.') + + if self.logfile != None: + self.logfile.write (s) + self.logfile.flush() + + return s + + raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().') + + def read (self, size = -1): # File-like object. + """This reads at most "size" bytes from the file + (less if the read hits EOF before obtaining size bytes). + If the size argument is negative or omitted, + read all data until EOF is reached. + The bytes are returned as a string object. + An empty string is returned when EOF is encountered immediately. + """ + if size == 0: + return '' + if size < 0: + self.expect (self.delimiter) # delimiter default is EOF + return self.before + + # I could have done this more directly by not using expect(), but + # I deliberately decided to couple read() to expect() so that + # I would catch any bugs early and ensure consistant behavior. + # It's a little less efficient, but there is less for me to + # worry about if I have to later modify read() or expect(). + # Note, it's OK if size==-1 in the regex. That just means it + # will never match anything in which case we stop only on EOF. + cre = re.compile('.{%d}' % size, re.DOTALL) + index = self.expect ([cre, self.delimiter]) # delimiter default is EOF + if index == 0: + return self.after ### self.before should be ''. Should I assert this? + return self.before + + def readline (self, size = -1): # File-like object. + """This reads and returns one entire line. A trailing newline is kept in + the string, but may be absent when a file ends with an incomplete line. + Note: This readline() looks for a \\r\\n pair even on UNIX because this is + what the pseudo tty device returns. So contrary to what you may be used to + you will receive a newline as \\r\\n. + An empty string is returned when EOF is hit immediately. + Currently, the size agument is mostly ignored, so this behavior is not + standard for a file-like object. If size is 0 then an empty string is returned. + """ + if size == 0: + return '' + index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF + if index == 0: + return self.before + '\r\n' + else: + return self.before + + def __iter__ (self): # File-like object. + """This is to support interators over a file-like object. + """ + return self + + def next (self): # File-like object. + """This is to support iterators over a file-like object. + """ + result = self.readline() + if result == "": + raise StopIteration + return result + + def readlines (self, sizehint = -1): # File-like object. + """This reads until EOF using readline() and returns a list containing + the lines thus read. The optional "sizehint" argument is ignored. + """ + lines = [] + while 1: + line = self.readline() + if not line: + break + lines.append(line) + return lines + + def write(self, str): # File-like object. + """This is similar to send() except that there is no return value. + """ + self.send (str) + + def writelines (self, sequence): # File-like object. + """This calls write() for each element in the sequence. + The sequence can be any iterable object producing strings, + typically a list of strings. This does not add line separators + There is no return value. + """ + for str in sequence: + self.write (str) + + def send(self, str): + """This sends a string to the child process. + This returns the number of bytes written. + If a log file was set then the data is also written to the log. + """ + time.sleep(self.delaybeforesend) + if self.logfile != None: + self.logfile.write (str) + self.logfile.flush() + c = os.write(self.child_fd, str) + return c + + def sendline(self, str=''): + """This is like send(), but it adds a line feed (os.linesep). + This returns the number of bytes written. + """ + n = self.send(str) + n = n + self.send (os.linesep) + return n + + def sendeof(self): + """This sends an EOF to the child. + This sends a character which causes the pending parent output + buffer to be sent to the waiting child program without + waiting for end-of-line. If it is the first character of the + line, the read() in the user program returns 0, which + signifies end-of-file. This means to work as expected + a sendeof() has to be called at the begining of a line. + This method does not send a newline. It is the responsibility + of the caller to ensure the eof is sent at the beginning of a line. + """ + ### Hmmm... how do I send an EOF? + ###C if ((m = write(pty, *buf, p - *buf)) < 0) + ###C return (errno == EWOULDBLOCK) ? n : -1; + fd = sys.stdin.fileno() + old = termios.tcgetattr(fd) # remember current state + new = termios.tcgetattr(fd) + new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF + try: # use try/finally to ensure state gets restored + termios.tcsetattr(fd, termios.TCSADRAIN, new) + if 'CEOF' in dir(termios): + os.write (self.child_fd, '%c' % termios.CEOF) + else: + os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D + finally: # restore state + termios.tcsetattr(fd, termios.TCSADRAIN, old) + + def eof (self): + """This returns 1 if the EOF exception was raised at some point. + """ + return self.flag_eof + + def terminate(self, force=0): + """This forces a child process to terminate. + It starts nicely with SIGHUP and SIGINT. If "force" is 1 then + moves onto SIGKILL. + This returns true if the child was terminated. + This returns false if the child could not be terminated. + """ + if not self.isalive(): + return 1 + self.kill(signal.SIGHUP) + time.sleep(0.1) + if not self.isalive(): + return 1 + self.kill(signal.SIGCONT) + time.sleep(0.1) + if not self.isalive(): + return 1 + self.kill(signal.SIGINT) + time.sleep(0.1) + if not self.isalive(): + return 1 + if force: + self.kill(signal.SIGKILL) + time.sleep(0.1) + if not self.isalive(): + return 1 + else: + return 0 + return 0 + #raise ExceptionPexpect ('terminate() could not terminate child process. Try terminate(force=1)?') + + def isalive(self): + """This tests if the child process is running or not. + This is non-blocking. If the child was terminated then this + will read the exitstatus or signalstatus of the child. + This returns 1 if the child process appears to be running or 0 if not. + It can take literally SECONDS for Solaris to return the right status. + """ + if self.terminated: + return 0 + + if self.flag_eof: + # This is for Linux, which requires the blocking form of waitpid to get + # status of a defunct process. This is super-lame. The flag_eof would have + # been set in read_nonblocking(), so this should be safe. + waitpid_options = 0 + else: + waitpid_options = os.WNOHANG + + try: + pid, status = os.waitpid(self.pid, waitpid_options) + except OSError, e: # No child processes + if e[0] == errno.ECHILD: + raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?') + else: + raise e + + # I have to do this twice for Solaris. I can't even believe that I figured this out... + # If waitpid() returns 0 it means that no child process wishes to + # report, and the value of status is undefined. + if pid == 0: + try: + pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris! + except OSError, e: # This should never happen... + if e[0] == errno.ECHILD: + raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?') + else: + raise e + + # If pid is still 0 after two calls to waitpid() then + # the process really is alive. This seems to work on all platforms, except + # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking + # take care of this situation (unfortunately, this requires waiting through the timeout). + if pid == 0: + return 1 + + if pid == 0: + return 1 + + if os.WIFEXITED (status): + self.status = status + self.exitstatus = os.WEXITSTATUS(status) + self.signalstatus = None + self.terminated = 1 + return 0 + elif os.WIFSIGNALED (status): + self.status = status + self.exitstatus = None + self.signalstatus = os.WTERMSIG(status) + self.terminated = 1 + return 0 + elif os.WIFSTOPPED (status): + raise ExceptionPexpect ('isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?') + + raise ExceptionPexpect ('isalive() reached unexpected condition where waitpid matched child pid, but status was not matched by WIFEXITED, WIFSIGNALED, or WIFSTOPPED.') + + + def kill(self, sig): + """This sends the given signal to the child application. + In keeping with UNIX tradition it has a misleading name. + It does not necessarily kill the child unless + you send the right signal. + """ + # Same as os.kill, but the pid is given for you. + if self.isalive(): + os.kill(self.pid, sig) + + def compile_pattern_list(self, patterns): + """This compiles a pattern-string or a list of pattern-strings. + Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or + a list of those. Patterns may also be None which results in + an empty list. + + This is used by expect() when calling expect_list(). + Thus expect() is nothing more than:: + cpl = self.compile_pattern_list(pl) + return self.expect_list(clp, timeout) + + If you are using expect() within a loop it may be more + efficient to compile the patterns first and then call expect_list(). + This avoid calls in a loop to compile_pattern_list(): + cpl = self.compile_pattern_list(my_pattern) + while some_condition: + ... + i = self.expect_list(clp, timeout) + ... + """ + if patterns is None: + return [] + if type(patterns) is not types.ListType: + patterns = [patterns] + + compiled_pattern_list = [] + for p in patterns: + if type(p) is types.StringType: + compiled_pattern_list.append(re.compile(p, re.DOTALL)) + elif p is EOF: + compiled_pattern_list.append(EOF) + elif p is TIMEOUT: + compiled_pattern_list.append(TIMEOUT) + elif type(p) is type(re.compile('')): + compiled_pattern_list.append(p) + else: + raise TypeError ('Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p))) + + return compiled_pattern_list + + def expect(self, pattern, timeout = -1, searchwindowsize=None): + """This seeks through the stream until a pattern is matched. + The pattern is overloaded and may take several types including a list. + The pattern can be a StringType, EOF, a compiled re, or + a list of those types. Strings will be compiled to re types. + This returns the index into the pattern list. If the pattern was + not a list this returns index 0 on a successful match. + This may raise exceptions for EOF or TIMEOUT. + To avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to + the pattern list. + + After a match is found the instance attributes + 'before', 'after' and 'match' will be set. + You can see all the data read before the match in 'before'. + You can see the data that was matched in 'after'. + The re.MatchObject used in the re match will be in 'match'. + If an error occured then 'before' will be set to all the + data read so far and 'after' and 'match' will be None. + + If timeout is -1 then timeout will be set to the self.timeout value. + + Note: A list entry may be EOF or TIMEOUT instead of a string. + This will catch these exceptions and return the index + of the list entry instead of raising the exception. + The attribute 'after' will be set to the exception type. + The attribute 'match' will be None. + This allows you to write code like this: + index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT]) + if index == 0: + do_something() + elif index == 1: + do_something_else() + elif index == 2: + do_some_other_thing() + elif index == 3: + do_something_completely_different() + instead of code like this: + try: + index = p.expect (['good', 'bad']) + if index == 0: + do_something() + elif index == 1: + do_something_else() + except EOF: + do_some_other_thing() + except TIMEOUT: + do_something_completely_different() + These two forms are equivalent. It all depends on what you want. + You can also just expect the EOF if you are waiting for all output + of a child to finish. For example: + p = pexpect.spawn('/bin/ls') + p.expect (pexpect.EOF) + print p.before + + If you are trying to optimize for speed then see expect_list(). + """ + compiled_pattern_list = self.compile_pattern_list(pattern) + return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) + + def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1): + """This takes a list of compiled regular expressions and returns + the index into the pattern_list that matched the child's output. + The list may also contain EOF or TIMEOUT (which are not + compiled regular expressions). This method is similar to + the expect() method except that expect_list() does not + recompile the pattern list on every call. + This may help if you are trying to optimize for speed, otherwise + just use the expect() method. This is called by expect(). + If timeout==-1 then the self.timeout value is used. + If searchwindowsize==-1 then the self.searchwindowsize value is used. + """ + self.patterns = pattern_list + + if timeout == -1: + timeout = self.timeout + if timeout != None: + end_time = time.time() + timeout + if searchwindowsize == -1: + searchwindowsize = self.searchwindowsize + + try: + incoming = self.buffer + while 1: # Keep reading until exception or return. + # Sequence through the list of patterns looking for a match. + first_match = -1 + for cre in pattern_list: + if cre is EOF or cre is TIMEOUT: + continue # The patterns for PexpectExceptions are handled differently. + if searchwindowsize is None: # search everything + match = cre.search(incoming) + else: + startpos = max(0, len(incoming) - searchwindowsize) + match = cre.search(incoming, startpos) + if match is None: + continue + if first_match > match.start() or first_match == -1: + first_match = match.start() + self.match = match + self.match_index = pattern_list.index(cre) + if first_match > -1: + self.buffer = incoming[self.match.end() : ] + self.before = incoming[ : self.match.start()] + self.after = incoming[self.match.start() : self.match.end()] + return self.match_index + # No match at this point + if timeout < 0 and timeout is not None: + raise TIMEOUT ('Timeout exceeded in expect_list().') + # Still have time left, so read more data + c = self.read_nonblocking (self.maxread, timeout) + incoming = incoming + c + if timeout is not None: + timeout = end_time - time.time() + except EOF, e: + self.buffer = '' + self.before = incoming + self.after = EOF + if EOF in pattern_list: + self.match = EOF + self.match_index = pattern_list.index(EOF) + return self.match_index + else: + self.match = None + self.match_index = None + raise EOF (str(e) + '\n' + str(self)) + except TIMEOUT, e: + self.before = incoming + self.after = TIMEOUT + if TIMEOUT in pattern_list: + self.match = TIMEOUT + self.match_index = pattern_list.index(TIMEOUT) + return self.match_index + else: + self.match = None + self.match_index = None + raise TIMEOUT (str(e) + '\n' + str(self)) + except Exception: + self.before = incoming + self.after = None + self.match = None + self.match_index = None + raise + + def getwinsize(self): + """This returns the window size of the child tty. + The return value is a tuple of (rows, cols). + """ + if 'TIOCGWINSZ' in dir(termios): + TIOCGWINSZ = termios.TIOCGWINSZ + else: + TIOCGWINSZ = 1074295912L # Assume + s = struct.pack('HHHH', 0, 0, 0, 0) + x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s) + return struct.unpack('HHHH', x)[0:2] + + def setwinsize(self, r, c): + """This sets the window size of the child tty. + This will cause a SIGWINCH signal to be sent to the child. + This does not change the physical window size. + It changes the size reported to TTY-aware applications like + vi or curses -- applications that respond to the SIGWINCH signal. + """ + # Check for buggy platforms. Some Python versions on some platforms + # (notably OSF1 Alpha and RedHat 7.1) truncate the value for + # termios.TIOCSWINSZ. It is not clear why this happens. + # These platforms don't seem to handle the signed int very well; + # yet other platforms like OpenBSD have a large negative value for + # TIOCSWINSZ and they don't have a truncate problem. + # Newer versions of Linux have totally different values for TIOCSWINSZ. + # Note that this fix is a hack. + if 'TIOCSWINSZ' in dir(termios): + TIOCSWINSZ = termios.TIOCSWINSZ + else: + TIOCSWINSZ = -2146929561 + if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2. + TIOCSWINSZ = -2146929561 # Same bits, but with sign. + # Note, assume ws_xpixel and ws_ypixel are zero. + s = struct.pack('HHHH', r, c, 0, 0) + fcntl.ioctl(self.fileno(), TIOCSWINSZ, s) + + def interact(self, escape_character = chr(29)): + """This gives control of the child process to the interactive user + (the human at the keyboard). + Keystrokes are sent to the child process, and the stdout and stderr + output of the child process is printed. + When the user types the escape_character this method will stop. + The default for escape_character is ^] (ASCII 29). + This simply echos the child stdout and child stderr to the real + stdout and it echos the real stdin to the child stdin. + + Note that if you change the window size of the parent + the SIGWINCH signal will not be passed through to the child. + If you want the child window size to change when the parent's + window size changes then do something like the following example: + import pexpect, struct, fcntl, termios, signal, sys + def sigwinch_passthrough (sig, data): + s = struct.pack("HHHH", 0, 0, 0, 0) + a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s)) + global p + p.setwinsize(a[0],a[1]) + p = pexpect.spawn('/bin/bash') # Note this is global + signal.signal(signal.SIGWINCH, sigwinch_passthrough) + p.interact() + """ + # Flush the buffer. + self.stdout.write (self.buffer) + self.stdout.flush() + self.buffer = '' + mode = tty.tcgetattr(self.STDIN_FILENO) + tty.setraw(self.STDIN_FILENO) + try: + self.__interact_copy(escape_character) + finally: + tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode) + + def __interact_writen(self, fd, data): + """This is used by the interact() method. + """ + while data != '' and self.isalive(): + n = os.write(fd, data) + data = data[n:] + def __interact_read(self, fd): + """This is used by the interact() method. + """ + return os.read(fd, 1000) + def __interact_copy(self, escape_character = None): + """This is used by the interact() method. + """ + while self.isalive(): + try: + r, w, e = select.select([self.child_fd, self.STDIN_FILENO], [], []) + except select.errno, e: + if e[0] != errno.EINTR: + raise + if self.child_fd in r: + data = self.__interact_read(self.child_fd) + if self.logfile != None: + self.logfile.write (data) + self.logfile.flush() + os.write(self.STDOUT_FILENO, data) + if self.STDIN_FILENO in r: + data = self.__interact_read(self.STDIN_FILENO) + self.__interact_writen(self.child_fd, data) + if escape_character in data: + break +############################################################################## +# The following methods are no longer supported or allowed.. + def setmaxread (self, maxread): + """This method is no longer supported or allowed. + I don't like getters and setters without a good reason. + """ + raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.') + def expect_exact (self, pattern_list, timeout = -1): + """This method is no longer supported or allowed. + It was too hard to maintain and keep it up to date with expect_list. + Few people used this method. Most people favored reliability over speed. + The implementation is left in comments in case anyone needs to hack this + feature back into their copy. + If someone wants to diff this with expect_list and make them work + nearly the same then I will consider adding this make in. + """ + raise ExceptionPexpect ('This method is no longer supported or allowed.') + def setlog (self, fileobject): + """This method is no longer supported or allowed. + """ + raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.') + +############################################################################## +# End of spawn class +############################################################################## + +def which (filename): + """This takes a given filename; tries to find it in the environment path; + then checks if it is executable. + This returns the full path to the filename if found and executable. + Otherwise this returns None. + """ + # Special case where filename already contains a path. + if os.path.dirname(filename) != '': + if os.access (filename, os.X_OK): + return filename + + if not os.environ.has_key('PATH') or os.environ['PATH'] == '': + p = os.defpath + else: + p = os.environ['PATH'] + + # Oddly enough this was the one line that made Pexpect + # incompatible with Python 1.5.2. + #pathlist = p.split (os.pathsep) + pathlist = string.split (p, os.pathsep) + + for path in pathlist: + f = os.path.join(path, filename) + if os.access(f, os.X_OK): + return f + return None + +def split_command_line(command_line): + """This splits a command line into a list of arguments. + It splits arguments on spaces, but handles + embedded quotes, doublequotes, and escaped characters. + It's impossible to do this with a regular expression, so + I wrote a little state machine to parse the command line. + """ + arg_list = [] + arg = '' + + # Constants to name the states we can be in. + state_basic = 0 + state_esc = 1 + state_singlequote = 2 + state_doublequote = 3 + state_whitespace = 4 # The state of consuming whitespace between commands. + state = state_basic + + for c in command_line: + if state == state_basic or state == state_whitespace: + if c == '\\': # Escape the next character + state = state_esc + elif c == r"'": # Handle single quote + state = state_singlequote + elif c == r'"': # Handle double quote + state = state_doublequote + elif c.isspace(): + # Add arg to arg_list if we aren't in the middle of whitespace. + if state == state_whitespace: + None # Do nothing. + else: + arg_list.append(arg) + arg = '' + state = state_whitespace + else: + arg = arg + c + state = state_basic + elif state == state_esc: + arg = arg + c + state = state_basic + elif state == state_singlequote: + if c == r"'": + state = state_basic + else: + arg = arg + c + elif state == state_doublequote: + if c == r'"': + state = state_basic + else: + arg = arg + c + + if arg != '': + arg_list.append(arg) + return arg_list +