Changeset 421
- Timestamp:
- 07/16/07 19:33:07 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
experiments/jruby-1/src/com/rubyeventmachine/EM.java
r415 r421 1 /** 2 * $Id$ 3 * 4 * Author:: Francis Cianfrocca (gmail: blackhedd) 5 * Homepage:: http://rubyeventmachine.com 6 * Date:: 15 Jul 2007 7 * 8 * See EventMachine and EventMachine::Connection for documentation and 9 * usage examples. 10 * 11 * 12 *---------------------------------------------------------------------------- 13 * 14 * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved. 15 * Gmail: blackhedd 16 * 17 * This program is free software; you can redistribute it and/or modify 18 * it under the terms of either: 1) the GNU General Public License 19 * as published by the Free Software Foundation; either version 2 of the 20 * License, or (at your option) any later version; or 2) Ruby's License. 21 * 22 * See the file COPYING for complete licensing information. 23 * 24 *--------------------------------------------------------------------------- 25 * 26 * 27 */ 28 1 29 package com.rubyeventmachine; 2 30 … … 22 50 private boolean bRunReactor; 23 51 private long BindingIndex; 52 private ByteBuffer EmptyByteBuffer; 53 24 54 25 55 public EM() { … … 27 57 Connections = new TreeMap<String, EventableChannel>(); 28 58 BindingIndex = 100000; 59 EmptyByteBuffer = ByteBuffer.allocate(0); 29 60 } 30 61 31 62 /** 32 * Intended to be overridden. This is a stub. 33 * @param a1 34 * @param a2 35 * @param a3 36 * @param a4 63 * Intended to be overridden in languages (like Ruby) that can't handle ByteBuffer. This is a stub. 64 * @param sig 65 * @param eventType 66 * @param data 37 67 */ 38 public void eventCallback (String a1, int a2, String a3, int a4) { 39 System.out.println ("Callback "+a1+" "+a2+" "+a3+" "+a4); 68 public void stringEventCallback (String sig, int eventType, String data) { 69 System.out.println ("Default event callback: " + sig + " " + eventType + " " + data); 70 } 71 72 /** 73 * This is a stub, intended to be overridden in Java code. 74 * @param sig 75 * @param eventType 76 * @param data 77 */ 78 public void eventCallback (String sig, int eventType, ByteBuffer data) { 79 stringEventCallback (sig, eventType, new String (data.array(), data.position(), data.limit())); 80 40 81 } 41 82 … … 66 107 EventableChannel ec = new EventableChannel (sn, b, mySelector); 67 108 Connections.put(b, ec); 68 eventCallback ((String)k.attachment(), EM_CONNECTION_ACCEPTED, b, b.length()); 109 //eventCallback ((String)k.attachment(), EM_CONNECTION_ACCEPTED, b, b.length()); 110 eventCallback ((String)k.attachment(), EM_CONNECTION_ACCEPTED, ByteBuffer.wrap(b.getBytes())); 69 111 } 70 112 } … … 76 118 if (r > 0) { 77 119 bb.flip(); 78 eventCallback (((EventableChannel)k.attachment()).getBinding(), EM_CONNECTION_READ, new String(bb.array(), 0, bb.limit()), bb.limit()); 120 //eventCallback (((EventableChannel)k.attachment()).getBinding(), EM_CONNECTION_READ, new String(bb.array(), 0, bb.limit()), bb.limit()); 121 eventCallback (((EventableChannel)k.attachment()).getBinding(), EM_CONNECTION_READ, bb); 79 122 } 80 123 else { 81 124 String b = ((EventableChannel)k.attachment()).getBinding(); 82 eventCallback (b, EM_CONNECTION_UNBOUND, "", 0); 125 //eventCallback (b, EM_CONNECTION_UNBOUND, "", 0); 126 eventCallback (b, EM_CONNECTION_UNBOUND, EmptyByteBuffer); // TODO, have this BB around somewhere, don't generate garbage. 83 127 Connections.remove(b); 84 128 sn.close(); … … 97 141 if (k.isConnectable()) { 98 142 EventableChannel ec = (EventableChannel)k.attachment(); 99 if (ec.finishConnecting()) 100 eventCallback (ec.getBinding(), EM_CONNECTION_COMPLETED, "", 0); 143 if (ec.finishConnecting()) { 144 //eventCallback (ec.getBinding(), EM_CONNECTION_COMPLETED, "", 0); 145 eventCallback (ec.getBinding(), EM_CONNECTION_COMPLETED, EmptyByteBuffer); 146 } 101 147 else { 102 148 Connections.remove (ec.getBinding()); 103 149 k.channel().close(); 104 eventCallback (ec.getBinding(), EM_CONNECTION_UNBOUND, "", 0); 150 //eventCallback (ec.getBinding(), EM_CONNECTION_UNBOUND, "", 0); 151 eventCallback (ec.getBinding(), EM_CONNECTION_UNBOUND, EmptyByteBuffer); 105 152 } 106 153 } … … 134 181 break; 135 182 String s = Timers.remove(k); 136 eventCallback ("", 100, s, s.length()); 183 //eventCallback ("", 100, s, s.length()); 184 eventCallback ("", EM_TIMER_FIRED, ByteBuffer.wrap(s.getBytes())); 137 185 } 138 186 } experiments/jruby-1/src/com/rubyeventmachine/EMTest.java
r415 r421 9 9 import org.junit.Test; 10 10 import java.io.*; 11 import java.nio.*; 12 11 13 12 14 public class EMTest { 13 15 14 16 class ShortTimer extends EM { 15 public void eventCallback (String a1, int a2, String a3, int a4) { 17 /* 18 public void xeventCallback (String a1, int a2, String a3, int a4) { 16 19 System.out.println ("Short Callback "+a1+" "+a2+" "+a3+" "+a4); 20 this.stop(); 21 }*/ 22 public void eventCallback (String sig, int eventCode, ByteBuffer data) { 23 System.out.println ("Short Callback "+sig+" "+eventCode+" "+data); 17 24 this.stop(); 18 25 }