PageRenderTime 32ms CodeModel.GetById 15ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/signals/natives/NATSeconds.java

http://ambienttalk.googlecode.com/
Java | 128 lines | 61 code | 15 blank | 52 comment | 0 complexity | d80f746c7349c5f0e143a798c1105567 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATSeconds.java created on Apr 14, 2008
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.signals.natives;
 29
 30import java.util.Timer;
 31import java.util.TimerTask;
 32
 33import edu.vub.at.exceptions.InterpreterException;
 34import edu.vub.at.objects.ATNumber;
 35import edu.vub.at.objects.grammar.ATSymbol;
 36import edu.vub.at.objects.natives.NATByRef;
 37import edu.vub.at.objects.natives.NATMethodInvocation;
 38import edu.vub.at.objects.natives.NATNumber;
 39import edu.vub.at.objects.natives.NATTable;
 40import edu.vub.at.objects.natives.NATText;
 41import edu.vub.at.objects.natives.grammar.AGSymbol;
 42
 43/**
 44 * Seconds is the builtin behavior representing the number of seconds that have elapsed since the
 45 * AmbientTalk virtual machine has been started.
 46 *  
 47 * @author smostinc
 48 */
 49public class NATSeconds extends NATBehavior {
 50
 51	private static final ATSymbol _TICK_ = AGSymbol.jAlloc("tick");
 52	private static final TimerSource _TIMER_ = new TimerSource();
 53	
 54	private static final ThreadLocal _SECONDS_ = new ThreadLocal() {
 55		protected Object initialValue() {
 56			try {
 57				return new NATSeconds(_TIMER_, new SecondsConsumer());
 58			} catch (InterpreterException e) {
 59				throw new RuntimeException(e);
 60			}
 61		};
 62	};
 63	
 64	public static NATSeconds instance() {
 65		return (NATSeconds) _SECONDS_.get();
 66	}
 67	
 68	/**
 69	 * TimerSource is a natively implemented event source which generates time tick events.
 70	 * The Event Source is shared between all actors on a single virtual machine, yet all
 71	 * actors have a separate seconds object, to represent the time. Due to synchronization
 72	 * when propagating events, the timer cannot cause concurrency within a single actor.
 73	 * 
 74	 * @author smostinc
 75	 */
 76	private static class TimerSource extends NATEventSource {
 77		
 78		private Timer 	timer_;
 79
 80		public TimerSource() {
 81			super(0);
 82			
 83						timer_	= new Timer();
 84			TimerTask	task_	= new TimerTask() {
 85				public void run() {
 86					try {
 87						propagateNewMessage(new NATMethodInvocation(
 88								_TICK_,
 89								NATTable.EMPTY,
 90								NATTable.EMPTY));
 91					} catch (InterpreterException e) {
 92						// Failed to construct a <-tick() message
 93						e.printStackTrace();
 94					}
 95				};
 96			};
 97				
 98			timer_.schedule(task_, 1000, 1000);
 99		}
100	}
101		
102	/**
103	 * <code>
104	 * def SecondsConsumer := object: {
105	 *   def seconds := 0;
106	 *   def tick() {
107	 *     seconds := seconds.inc();
108	 *   }
109	 * }  
110	 * </code>  
111	 */
112	public static class SecondsConsumer extends NATByRef {
113		ATNumber seconds_ = NATNumber.ZERO;
114				
115		public ATNumber base_tick() throws InterpreterException {
116			seconds_ = seconds_.base_inc();
117			return seconds_;
118		}
119		
120		public NATText meta_print() throws InterpreterException {
121			return NATText.atValue("<object:"+this.hashCode()+">");
122		}
123	}
124	
125	NATSeconds(TimerSource theTimer, SecondsConsumer theConsumer) throws InterpreterException {
126		super(theTimer, NATNumber.ZERO, theConsumer);
127	}
128}