View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.transport.vmpipe;
    21
    22  import java.util.concurrent.BlockingQueue;
    23  import java.util.concurrent.LinkedBlockingQueue;
    24  import java.util.concurrent.locks.Lock;
    25  import java.util.concurrent.locks.ReentrantLock;
    26
    27  import org.apache.mina.core.filterchain.IoFilterChain;
    28  import org.apache.mina.core.service.DefaultTransportMetadata;
    29  import org.apache.mina.core.service.IoHandler;
    30  import org.apache.mina.core.service.IoProcessor;
    31  import org.apache.mina.core.service.IoService;
    32  import org.apache.mina.core.service.IoServiceListenerSupport;
    33  import org.apache.mina.core.service.TransportMetadata;
    34  import org.apache.mina.core.session.AbstractIoSession;
    35  import org.apache.mina.core.session.IoSession;
    36  import org.apache.mina.core.write.WriteRequestQueue;
    37
    38  /**
    39   * A {@link IoSession} for in-VM transport (VM_PIPE).
    40   *
    41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    42   */
    43  class VmPipeSession extends AbstractIoSession {
    44
    45      static final TransportMetadata METADATA = new DefaultTransportMetadata("mina", "vmpipe", false, false,
    46              VmPipeAddress.class, VmPipeSessionConfig.class, Object.class);
    47
    48      private final IoServiceListenerSupport serviceListeners;
    49
    50      private final VmPipeAddress localAddress;
    51
    52      private final VmPipeAddress remoteAddress;
    53
    54      private final VmPipeAddress serviceAddress;
    55
    56      private final VmPipeFilterChain filterChain;
    57
    58      private final VmPipeSession remoteSession;
    59
    60      private final Lock lock;
    61
    62      final BlockingQueue<Object> receivedMessageQueue;
    63
    64      /*
    65       * Constructor for client-side session.
    66       */
    67      VmPipeSession(IoService service, IoServiceListenerSupport serviceListeners, VmPipeAddress localAddress,
    68              IoHandler handler, VmPipe remoteEntry) {
    69          super(service);
    70          config = new DefaultVmPipeSessionConfig();
    71          this.serviceListeners = serviceListeners;
    72          lock = new ReentrantLock();
    73          this.localAddress = localAddress;
    74          remoteAddress = serviceAddress = remoteEntry.getAddress();
    75          filterChain = new VmPipeFilterChain(this);
    76          receivedMessageQueue = new LinkedBlockingQueue<Object>();
    77
    78          remoteSession = new VmPipeSession(this, remoteEntry);
    79      }
    80
    81      /*
    82       * Constructor for server-side session.
    83       */
    84      private VmPipeSession(VmPipeSession remoteSession, VmPipe entry) {
    85          super(entry.getAcceptor());
    86          config = new DefaultVmPipeSessionConfig();
    87          serviceListeners = entry.getListeners();
    88          lock = remoteSession.lock;
    89          localAddress = serviceAddress = remoteSession.remoteAddress;
    90          remoteAddress = remoteSession.localAddress;
    91          filterChain = new VmPipeFilterChain(this);
    92          this.remoteSession = remoteSession;
    93          receivedMessageQueue = new LinkedBlockingQueue<Object>();
    94      }
    95
    96      @Override
    97      public IoProcessor<VmPipeSession> getProcessor() {
    98          return filterChain.getProcessor();
    99      }
    100
    101     IoServiceListenerSupport getServiceListeners() {
    102         return serviceListeners;
    103     }
    104
    105     public VmPipeSessionConfig getConfig() {
    106         return (VmPipeSessionConfig) config;
    107     }
    108
    109     public IoFilterChain getFilterChain() {
    110         return filterChain;
    111     }
    112
    113     public VmPipeSession getRemoteSession() {
    114         return remoteSession;
    115     }
    116
    117     public TransportMetadata getTransportMetadata() {
    118         return METADATA;
    119     }
    120
    121     public VmPipeAddress getRemoteAddress() {
    122         return remoteAddress;
    123     }
    124
    125     public VmPipeAddress getLocalAddress() {
    126         return localAddress;
    127     }
    128
    129     @Override
    130     public VmPipeAddress getServiceAddress() {
    131         return serviceAddress;
    132     }
    133
    134     void increaseWrittenBytes0(int increment, long currentTime) {
    135         super.increaseWrittenBytes(increment, currentTime);
    136     }
    137
    138     WriteRequestQueue getWriteRequestQueue0() {
    139         return super.getWriteRequestQueue();
    140     }
    141
    142     Lock getLock() {
    143         return lock;
    144     }
    145 }