Esempio n. 1
0
 /** {@inheritDoc} */
 public void invoke(String method, Object[] params, IPendingServiceCallback callback) {
   IPendingServiceCall call = new PendingCall(method, params);
   if (callback != null) {
     call.registerCallback(callback);
   }
   invoke(call);
 }
Esempio n. 2
0
 /**
  * When the connection has been closed, notify any remaining pending service calls that they have
  * failed because the connection is broken. Implementors of IPendingServiceCallback may only
  * deduce from this notification that it was not possible to read a result for this service call.
  * It is possible that (1) the service call was never written to the service, or (2) the service
  * call was written to the service and although the remote method was invoked, the connection
  * failed before the result could be read, or (3) although the remote method was invoked on the
  * service, the service implementor detected the failure of the connection and performed only
  * partial processing. The caller only knows that it cannot be confirmed that the callee has
  * invoked the service call and returned a result.
  */
 public void sendPendingServiceCallsCloseError() {
   if (pendingCalls != null && !pendingCalls.isEmpty()) {
     for (IPendingServiceCall call : pendingCalls.values()) {
       call.setStatus(Call.STATUS_NOT_CONNECTED);
       for (IPendingServiceCallback callback : call.getCallbacks()) {
         callback.resultReceived(call);
       }
     }
   }
 }
 public void resultReceived(IPendingServiceCall call) {
   log.debug("resultReceived", call);
   if (call.getResult() instanceof ObjectMap<?, ?>) {
     ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
     if (map.containsKey("code")) {
       String code = (String) map.get("code");
       log.debug("Code: {}", code);
       if (StatusCodes.NS_PLAY_START.equals(code)) {
         subscribed = true;
       }
     }
   }
   wrapped.resultReceived(call);
 }
Esempio n. 4
0
  protected void handleConnect(
      RTMPConnection conn, Channel channel, Header header, Invoke invoke, RTMP rtmp) {
    final IPendingServiceCall call = invoke.getCall();
    // Get parameters passed from client to NetConnection#connection
    final Map<String, Object> params = invoke.getConnectionParams();

    // Get hostname
    String host = getHostname((String) params.get("tcUrl"));

    // App name as path, but without query string if there is one
    String path = (String) params.get("app");
    if (path.indexOf("?") != -1) {
      int idx = path.indexOf("?");
      params.put("queryString", path.substring(idx));
      path = path.substring(0, idx);
    }
    params.put("path", path);

    final String sessionId = null;

    conn.setup(host, path, sessionId, params);

    // check the security constraints
    // send back "ConnectionRejected" if fails.
    if (!checkPermission(conn)) {
      call.setStatus(Call.STATUS_ACCESS_DENIED);
      call.setResult(getStatus(NC_CONNECT_REJECTED));
      Invoke reply = new Invoke();
      reply.setCall(call);
      reply.setInvokeId(invoke.getInvokeId());
      channel.write(reply);
      conn.close();
    } else {
      synchronized (rtmp) {
        // connect the origin
        sendConnectMessage(conn);
        rtmp.setState(RTMP.STATE_EDGE_CONNECT_ORIGIN_SENT);
        Packet packet = new Packet(header);
        packet.setMessage(invoke);
        forwardPacket(conn, packet);
        rtmp.setState(RTMP.STATE_ORIGIN_CONNECT_FORWARDED);
        // Evaluate request for AMF3 encoding
        if (Integer.valueOf(3).equals(params.get("objectEncoding"))) {
          rtmp.setEncoding(Encoding.AMF3);
        }
      }
    }
  }
 /**
  * Handler for pending call result. Dispatches results to all pending call handlers.
  *
  * @param conn Connection
  * @param invoke Pending call result event context
  */
 protected void handlePendingCallResult(RTMPConnection conn, Invoke invoke) {
   final IServiceCall call = invoke.getCall();
   final IPendingServiceCall pendingCall = conn.retrievePendingCall(invoke.getTransactionId());
   if (pendingCall != null) {
     // The client sent a response to a previously made call.
     Object[] args = call.getArguments();
     if (args != null && args.length > 0) {
       // TODO: can a client return multiple results?
       pendingCall.setResult(args[0]);
     }
     Set<IPendingServiceCallback> callbacks = pendingCall.getCallbacks();
     if (!callbacks.isEmpty()) {
       HashSet<IPendingServiceCallback> tmp = new HashSet<IPendingServiceCallback>();
       tmp.addAll(callbacks);
       for (IPendingServiceCallback callback : tmp) {
         try {
           callback.resultReceived(pendingCall);
         } catch (Exception e) {
           log.error("Error while executing callback {}", callback, e);
         }
       }
     }
   }
 }
 public void resultReceived(IPendingServiceCall call) {
   Integer streamIdInt = (Integer) call.getResult();
   log.debug("Stream id: {}", streamIdInt);
   log.debug("Connection: {}", conn);
   log.debug("DeleteStreamCallBack resultReceived - stream id: {}", streamIdInt);
   if (conn != null && streamIdInt != null) {
     log.debug("Deleting net stream");
     conn.removeClientStream(streamIdInt);
     // send a delete notify?
     // NetStreamPrivateData streamData = streamDataMap.get(streamIdInt);
     // streamData.handler.onStreamEvent(notify)
     streamDataMap.remove(streamIdInt);
   }
   wrapped.resultReceived(call);
 }
 public void resultReceived(IPendingServiceCall call) {
   Integer streamIdInt = (Integer) call.getResult();
   log.debug("CreateStreamCallBack resultReceived - stream id: {} call: {}", streamIdInt, call);
   log.debug("Connection: {}", conn);
   if (conn != null && streamIdInt != null) {
     log.debug("Setting new net stream");
     NetStream stream = new NetStream(streamEventDispatcher);
     stream.setConnection(conn);
     stream.setStreamId(streamIdInt);
     conn.addClientStream(stream);
     NetStreamPrivateData streamData = new NetStreamPrivateData();
     streamData.outputStream = conn.createOutputStream(streamIdInt);
     streamData.connConsumer =
         new ConnectionConsumer(
             conn,
             streamData.outputStream.getVideo(),
             streamData.outputStream.getAudio(),
             streamData.outputStream.getData());
     streamDataMap.put(streamIdInt, streamData);
     log.debug("streamDataMap: {}", streamDataMap);
   }
   wrapped.resultReceived(call);
 }
 /** {@inheritDoc} */
 public void messageReceived(RTMPConnection conn, Packet packet) throws Exception {
   log.trace("messageReceived connection: {}", conn.getSessionId());
   if (conn != null) {
     IRTMPEvent message = null;
     try {
       message = packet.getMessage();
       final Header header = packet.getHeader();
       final Number streamId = header.getStreamId();
       final Channel channel = conn.getChannel(header.getChannelId());
       final IClientStream stream = conn.getStreamById(streamId);
       if (log.isTraceEnabled()) {
         log.trace("Message received - header: {}", header);
       }
       // set stream id on the connection
       conn.setStreamId(streamId);
       // increase number of received messages
       conn.messageReceived();
       // set the source of the message
       message.setSource(conn);
       // process based on data type
       final byte headerDataType = header.getDataType();
       if (log.isTraceEnabled()) {
         log.trace("Header / message data type: {}", headerDataType);
       }
       switch (headerDataType) {
         case TYPE_AGGREGATE:
           log.debug(
               "Aggregate type data - header timer: {} size: {}",
               header.getTimer(),
               header.getSize());
         case TYPE_AUDIO_DATA:
         case TYPE_VIDEO_DATA:
           // mark the event as from a live source
           // log.trace("Marking message as originating from a Live source");
           message.setSourceType(Constants.SOURCE_TYPE_LIVE);
           // NOTE: If we respond to "publish" with "NetStream.Publish.BadName",
           // the client sends a few stream packets before stopping. We need to ignore them
           if (stream != null) {
             ((IEventDispatcher) stream).dispatchEvent(message);
           }
           break;
         case TYPE_FLEX_SHARED_OBJECT:
         case TYPE_SHARED_OBJECT:
           onSharedObject(conn, channel, header, (SharedObjectMessage) message);
           break;
         case TYPE_INVOKE:
         case TYPE_FLEX_MESSAGE:
           onCommand(conn, channel, header, (Invoke) message);
           IPendingServiceCall call = ((Invoke) message).getCall();
           if (message.getHeader().getStreamId().intValue() != 0
               && call.getServiceName() == null
               && StreamAction.PUBLISH.equals(call.getServiceMethodName())) {
             if (stream != null) {
               // Only dispatch if stream really was created
               ((IEventDispatcher) stream).dispatchEvent(message);
             }
           }
           break;
         case TYPE_NOTIFY: // like an invoke, but does not return
           // anything and has a invoke / transaction
           // id of 0
         case TYPE_FLEX_STREAM_SEND:
           if (((Notify) message).getData() != null && stream != null) {
             // Stream metadata
             ((IEventDispatcher) stream).dispatchEvent(message);
           } else {
             onCommand(conn, channel, header, (Notify) message);
           }
           break;
         case TYPE_PING:
           onPing(conn, channel, header, (Ping) message);
           break;
         case TYPE_BYTES_READ:
           onStreamBytesRead(conn, channel, header, (BytesRead) message);
           break;
         case TYPE_CHUNK_SIZE:
           onChunkSize(conn, channel, header, (ChunkSize) message);
           break;
         case Constants.TYPE_CLIENT_BANDWIDTH: // onBWDone / peer bw
           log.debug("Client bandwidth: {}", message);
           onClientBandwidth(conn, channel, (ClientBW) message);
           break;
         case Constants.TYPE_SERVER_BANDWIDTH: // window ack size
           log.debug("Server bandwidth: {}", message);
           onServerBandwidth(conn, channel, (ServerBW) message);
           break;
         default:
           log.debug("Unknown type: {}", header.getDataType());
       }
       if (message instanceof Unknown) {
         log.info("Message type unknown: {}", message);
       }
     } catch (Throwable t) {
       log.error("Exception", t);
     }
     // XXX this may be causing 'missing' data if previous methods are
     // not making copies before buffering etc..
     if (message != null) {
       message.release();
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 protected void onCommand(RTMPConnection conn, Channel channel, Header source, ICommand command) {
   log.trace("onCommand: {}, id: {}", command, command.getTransactionId());
   final IServiceCall call = command.getCall();
   final String methodName = call.getServiceMethodName();
   log.debug(
       "Service name: {} args[0]: {}",
       methodName,
       (call.getArguments().length != 0 ? call.getArguments()[0] : ""));
   if ("_result".equals(methodName) || "_error".equals(methodName)) {
     final IPendingServiceCall pendingCall = conn.getPendingCall(command.getTransactionId());
     log.debug("Received result for pending call - {}", pendingCall);
     if (pendingCall != null) {
       if ("connect".equals(methodName)) {
         Integer encoding = (Integer) connectionParams.get("objectEncoding");
         if (encoding != null && encoding.intValue() == 3) {
           log.debug("Setting encoding to AMF3");
           conn.getState().setEncoding(IConnection.Encoding.AMF3);
         }
       }
     }
     handlePendingCallResult(conn, (Invoke) command);
     return;
   }
   // potentially used twice so get the value once
   boolean onStatus = "onStatus".equals(methodName);
   log.debug("onStatus {}", onStatus);
   if (onStatus) {
     Integer streamId = source.getStreamId();
     if (log.isDebugEnabled()) {
       log.debug("Stream id from header: {}", streamId);
       // XXX create better to serialize ObjectMap to Status object
       ObjectMap<?, ?> objMap = (ObjectMap<?, ?>) call.getArguments()[0];
       // should keep this as an Object to stay compatible with FMS3 etc
       log.debug("Client id from status: {}", objMap.get("clientid"));
     }
     if (streamId != null) {
       // try lookup by stream id
       NetStreamPrivateData streamData = streamDataMap.get(streamId);
       // if null return the first one in the map
       if (streamData == null) {
         log.debug("Stream data was not found by id. Map contents: {}", streamDataMap);
         if (!streamDataMap.isEmpty()) {
           streamData = streamDataMap.values().iterator().next();
         }
       }
       if (streamData == null) {
         log.warn("Stream data was null for id: {}", streamId);
       }
       if (streamData != null && streamData.handler != null) {
         log.debug("Got stream data and handler");
         streamData.handler.onStreamEvent((Notify) command);
       }
     }
   }
   // if this client supports service methods, forward the call
   if (serviceProvider == null) {
     // client doesn't support calling methods on him
     call.setStatus(Call.STATUS_METHOD_NOT_FOUND);
     call.setException(new MethodNotFoundException(methodName));
     log.info(
         "No service provider / method not found; to handle calls like onBWCheck, add a service provider");
   } else {
     serviceInvoker.invoke(call, serviceProvider);
   }
   if (call instanceof IPendingServiceCall) {
     IPendingServiceCall psc = (IPendingServiceCall) call;
     Object result = psc.getResult();
     log.debug("Pending call result is: {}", result);
     if (result instanceof DeferredResult) {
       DeferredResult dr = (DeferredResult) result;
       dr.setTransactionId(command.getTransactionId());
       dr.setServiceCall(psc);
       dr.setChannel(channel);
       conn.registerDeferredResult(dr);
     } else if (!onStatus) {
       if ("onBWCheck".equals(methodName)) {
         onBWCheck(call.getArguments().length > 0 ? call.getArguments()[0] : null);
         Invoke reply = new Invoke();
         reply.setCall(call);
         reply.setTransactionId(command.getTransactionId());
         channel.write(reply);
       } else if ("onBWDone".equals(methodName)) {
         onBWDone(call.getArguments().length > 0 ? call.getArguments()[0] : null);
       } else {
         Invoke reply = new Invoke();
         reply.setCall(call);
         reply.setTransactionId(command.getTransactionId());
         log.debug("Sending empty call reply: {}", reply);
         channel.write(reply);
       }
     }
   }
 }