package com.blankstyle.vertx.php.net;

import com.blankstyle.vertx.php.Handler;

import com.caucho.quercus.env.BooleanValue;
import com.caucho.quercus.env.Callback;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.Value;
import com.caucho.quercus.env.StringValue;

/**
 * A PHP compatible implementation of the Vert.x NetSocket.
 */
public class NetSocket {

  org.vertx.java.core.net.NetSocket socket;

  public NetSocket(org.vertx.java.core.net.NetSocket socket) {
    this.socket = socket;
  }

  /**
   * Returns the local socket address.
   */
  public Value localAddress(Env env) {
    return env.createString(socket.localAddress().toString());
  }

  /**
   * Returns the remote socket address.
   */
  public Value remoteAddress(Env env) {
    return env.createString(socket.remoteAddress().toString());
  }

  /**
   * Sets the socket data handler.
   */
  public NetSocket dataHandler(final Env env, final Callback handler) {
    socket.dataHandler(new Handler<org.vertx.java.core.buffer.Buffer>(env, handler));
    return this;
  }

  /**
   * Pauses producing on the socket.
   */
  public NetSocket pause(Env env) {
    socket.pause();
    return this;
  }

  /**
   * Resumes producing on the socket.
   */
  public NetSocket resume(Env env) {
    socket.resume();
    return this;
  }

  /**
   * Sets the socket end handler.
   */
  public NetSocket endHandler(final Env env, final Callback handler) {
    socket.endHandler(new Handler<Void>(env, handler));
    return this;
  }

  /**
   * Sets the socket drain handler.
   */
  public NetSocket drainHandler(final Env env, final Callback handler) {
    socket.drainHandler(new Handler<Void>(env, handler));
    return this;
  }

  /**
   * Sets the max write queue size.
   */
  public NetSocket setWriteQueueMaxSize(Env env, Value value) {
    socket.setWriteQueueMaxSize(value.toJavaInteger());
    return this;
  }

  /**
   * Indicates whether the write queue is full.
   */
  public BooleanValue writeQueueFull(Env env) {
    return BooleanValue.create(socket.writeQueueFull());
  }

  /**
   * Writes a value to the socket.
   */
  public NetSocket write(Env env, Value value) {
    socket.write(value.toJavaString());
    return this;
  }

  /**
   * Sends a file through the socket.
   */
  public NetSocket sendFile(Env env, StringValue filename) {
    return this;
  }

  /**
   * Sets the socket close handler.
   */
  public void closeHandler(final Env env, final Callback handler) {
    socket.closeHandler(new Handler<Void>(env, handler));
  }

  /**
   * Closes the socket.
   */
  public void close(Env env) {
    socket.close();
  }

  /**
   * Sets the socket exception handler callback.
   */
  public NetSocket exceptionHandler(final Env env, final Callback handler) {
    socket.exceptionHandler(new Handler<Throwable>(env, handler));
    return this;
  }

}

@@ -73,17 +73,179 @@
 andler;%0A
+import com.blankstyle.vertx.php.streams.ExceptionSupport;%0Aimport com.blankstyle.vertx.php.streams.ReadStream;%0Aimport com.blankstyle.vertx.php.streams.WriteStream;
 %0A
-
 import c
@@ -355,16 +355,59 @@
 nv.Env;%0A
+import com.caucho.quercus.env.NumberValue;%0A
 import c
@@ -547,16 +547,16 @@
 et.%0A */%0A
-
 public c
@@ -569,16 +569,102 @@
 etSocket
+ implements ReadStream%3CNetSocket%3E, WriteStream%3CNetSocket%3E, ExceptionSupport%3CNetSocket%3E
  %7B%0A%0A  or
@@ -2086,32 +2086,38 @@
 axSize(Env env, 
+Number
 Value value) %7B%0A 
@@ -2159,19 +2159,11 @@
 e.to
-JavaInteger
+Int
 ());
@@ -2439,32 +2439,49 @@
 env, Value value
+, StringValue enc
 ) %7B%0A    socket.w

Implement ReadStream and WriteStream interfaces in NetSocket.