    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.activeio.filter;
    19
    20  import java.io.IOException;
    21  import java.net.URI;
    22  import java.net.URISyntaxException;
    23
    24  import org.activeio.AcceptListener;
    25  import org.activeio.AsynchChannelServer;
    26  import org.activeio.Channel;
    27  import org.activeio.ChannelRequestTestSupport;
    28  import org.activeio.ChannelServer;
    29  import org.activeio.RequestChannel;
    30  import org.activeio.RequestListener;
    31  import org.activeio.SynchChannelFactory;
    32  import org.activeio.adapter.AsynchChannelToClientRequestChannel;
    33  import org.activeio.adapter.AsynchChannelToServerRequestChannel;
    34  import org.activeio.adapter.SynchToAsynchChannelAdapter;
    35  import org.activeio.adapter.SynchToAsynchChannelServerAdapter;
    36  import org.activeio.net.SocketMetadata;
    37  import org.activeio.net.SocketSynchChannelFactory;
    38
    39  /***
    40   */
    41  public class PacketAggregatingChannelFilterTest extends ChannelRequestTestSupport {
    42
    43      private URI serverURI;
    44
    45      protected ChannelServer createChannelServer(final RequestListener requestListener) throws IOException, URISyntaxException {
    46
    47          SynchChannelFactory factory = new SocketSynchChannelFactory();
    48
    49          AsynchChannelServer server = new SynchToAsynchChannelServerAdapter(factory.bindSynchChannel(new URI("tcp://localhost:0")));
    50
    51          server.setAcceptListener(new AcceptListener() {
    52              public void onAccept(Channel channel) {
    53
    54                  RequestChannel requestChannel = null;
    55                  try {
    56                  		((SocketMetadata)channel.narrow(SocketMetadata.class)).setTcpNoDelay(true);
    57                      requestChannel =
    58                          new AsynchChannelToServerRequestChannel(
    59                              new PacketAggregatingAsynchChannel(
    60                                      SynchToAsynchChannelAdapter.adapt(channel)));
    61
    62                      requestChannel.setRequestListener(requestListener);
    63                      requestChannel.start();
    64
    65                  } catch (IOException e) {
    66                      if (requestChannel != null)
    67                          requestChannel.dispose();
    68                      else
    69                          channel.dispose();
    70                  }
    71              }
    72
    73              public void onAcceptError(IOException error) {
    74                  error.printStackTrace();
    75              }
    76          });
    77          serverURI = server.getConnectURI();
    78          return server;
    79      }
    80
    81      /***
    82       * @return
    83       * @throws IOException
    84       */
    85      protected RequestChannel createClientRequestChannel() throws IOException {
    86          SynchChannelFactory factory = new SocketSynchChannelFactory();
    87          PacketAggregatingSynchChannel channel = new PacketAggregatingSynchChannel(factory.openSynchChannel(serverURI));
    88  		((SocketMetadata)channel.narrow(SocketMetadata.class)).setTcpNoDelay(true);
    89          return new AsynchChannelToClientRequestChannel(channel);
    90      }
    91
    92  }