PageRenderTime 145ms CodeModel.GetById 121ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/ss7/m3ua/impl/src/test/java/org/mobicents/protocols/ss7/m3ua/impl/sctp/M3UATransferTest.java

http://mobicents.googlecode.com/
Java | 278 lines | 202 code | 48 blank | 28 comment | 17 complexity | 8f79f4f5d3a9f1e8ded91e060b7a8044 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.m3ua.impl.sctp;
 24
 25import static org.junit.Assert.assertEquals;
 26
 27import java.io.IOException;
 28import java.net.InetAddress;
 29import java.net.InetSocketAddress;
 30import java.net.UnknownHostException;
 31import java.nio.channels.SelectionKey;
 32import java.util.Collection;
 33
 34import org.junit.After;
 35import org.junit.AfterClass;
 36import org.junit.Before;
 37import org.junit.BeforeClass;
 38import org.junit.Test;
 39import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
 40import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
 41import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
 42import org.mobicents.protocols.ss7.m3ua.M3UASelector;
 43import org.mobicents.protocols.ss7.m3ua.M3UAServerChannel;
 44import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
 45import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
 46import org.mobicents.protocols.ss7.m3ua.message.MessageType;
 47import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
 48import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
 49
 50/**
 51 * @author amit bhayani
 52 *
 53 */
 54public class M3UATransferTest {
 55    private InetAddress localhost;
 56    
 57    private int serverPort = 6092;
 58    private int clientPort = 6093;
 59    
 60    private Server server;
 61    private Client client;
 62        
 63    public M3UATransferTest() {
 64    }
 65
 66    @BeforeClass
 67    public static void setUpClass() throws Exception {
 68    }
 69
 70    @AfterClass
 71    public static void tearDownClass() throws Exception {
 72    }
 73
 74    @Before
 75    public void setUp() throws UnknownHostException, IOException {
 76        localhost = InetAddress.getLocalHost();
 77        
 78        client = new Client(localhost, clientPort);
 79        server = new Server(localhost, serverPort);
 80        
 81    }
 82
 83    @After
 84    public void tearDown() {
 85    }
 86
 87    /**
 88     * Test of connect method, of class M3UASocket.
 89     */
 90    @Test
 91    @SuppressWarnings("static-access")
 92    public void testSockets() throws Exception {
 93        System.out.println("Starting server");
 94        server.start();
 95        Thread.currentThread().sleep(100);
 96        
 97        System.out.println("Connecting to server");
 98        client.connect(localhost, serverPort);
 99        System.out.println("Client connected");
100        
101        client.start();
102        
103        Thread.currentThread().sleep(3000);
104        
105        client.stop();
106        server.stop();
107        
108        assertEquals("Hello world", server.getReceivedMessage());
109        assertEquals("Hello world", client.getReceivedMessage());
110    }
111
112
113    private M3UAMessage createMessage(M3UAProvider provider, String text) {
114        PayloadData msg = (PayloadData) provider.getMessageFactory().createMessage(MessageClass.TRANSFER_MESSAGES, MessageType.PAYLOAD);
115        
116        ProtocolData protocolData = provider.getParameterFactory().createProtocolData(1408, 14150, 0, 0, 0, 1, text.getBytes());
117        msg.setData(protocolData);
118        
119        return msg;
120    }
121    
122    private class Client implements Runnable {
123        private M3UAProvider provider;
124        private M3UAChannel channel;
125        private M3UASelector selector;
126        private M3UASelectionKey skey;
127        private volatile boolean started = false;
128        
129        private String rxMessage = "";        
130        private String[] txMessage = new String[]{"Hello", " ", "world"};
131        
132        public Client(InetAddress address, int port) throws IOException {
133            provider = SctpProvider.provider();
134            channel = provider.openChannel();
135            channel.bind(new InetSocketAddress(address, port));
136            
137            selector = provider.openSelector();
138            skey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
139        }
140        
141        @SuppressWarnings("static-access")
142        public void connect(InetAddress endpoint, int port) throws IOException {
143            channel.connect(new InetSocketAddress(endpoint, port));
144            if (channel.isConnectionPending()) {
145                while (!channel.isConnected()) {
146                    channel.finishConnect();
147                    try {
148                        Thread.currentThread().sleep(10);
149                    } catch (Exception e) {
150                    }
151                }
152            }
153        }
154        
155        public void start() {
156            started = true;
157            new Thread(this).start();
158        }
159        
160        public void stop() throws IOException {
161            started = false;
162        }
163        
164        public String getReceivedMessage() {
165            return rxMessage;
166        }
167        
168        public void run() {
169            int i = 0;
170            while (started) {
171                try {
172                    Collection<M3UASelectionKey> keys = selector.selectNow();
173                    for (M3UASelectionKey key : keys) {
174                        M3UAChannel chan = (M3UAChannel) key.channel();
175                        if (key.isReadable()) {
176                            PayloadData msg = (PayloadData) chan.receive();
177                            System.out.println("Receive message " + msg);
178                            rxMessage += new String(msg.getData().getData());
179                        }
180                        
181                        if (key.isWritable() && i < txMessage.length) {
182                            chan.send(createMessage(provider, txMessage[i++]));
183                        }
184                    }
185                } catch (Exception e) {
186                    e.printStackTrace();
187                }
188            }
189            
190            try {
191                skey.cancel();
192                selector.close();
193                channel.close();
194            } catch (IOException e) {
195                e.printStackTrace();
196            }
197        }
198    }
199    
200    private class Server implements Runnable {
201        
202        private M3UAProvider provider;
203        private M3UAServerChannel serverChannel;
204        private M3UAChannel channel;
205        private M3UASelector selector;
206        private M3UASelectionKey skey;
207        
208        private volatile boolean started = false;
209        
210        private String rxMessage = "";        
211        private String[] txMessage = new String[]{"Hello", " ", "world"};
212        
213        public Server(InetAddress address, int port) throws IOException {
214            provider = SctpProvider.provider();
215            serverChannel = provider.openServerChannel();
216            serverChannel.bind(new InetSocketAddress(address, port));
217            
218            selector = provider.openSelector();
219            skey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
220        }
221        
222        public void start() {
223            started = true;
224            new Thread(this).start();
225        }
226        
227        public void stop() throws IOException {
228            started = false;
229        }
230        
231        public String getReceivedMessage() {
232            return rxMessage;
233        }
234        
235        private void accept() throws IOException {
236            channel = serverChannel.accept();
237            skey.cancel();
238            skey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
239        }
240        
241        public void run() {
242            int i = 0;
243            while (started) {
244                try {
245                    Collection<M3UASelectionKey> keys = selector.selectNow();
246                    for (M3UASelectionKey key : keys) {
247                        if (key.isAcceptable()) {
248                            M3UAServerChannel chan = (M3UAServerChannel) key.channel();
249                            accept();
250                        } else if (key.isReadable()) {
251                            M3UAChannel chan = (M3UAChannel) key.channel();
252                            PayloadData msg = (PayloadData) chan.receive();
253                            System.out.println("Receive " + msg);
254                            rxMessage += new String(msg.getData().getData());
255                        } else if (key.isWritable() && i < txMessage.length) {
256                            M3UAChannel chan = (M3UAChannel) key.channel();
257                            chan.send(createMessage(provider, txMessage[i++]));
258                        }
259                    }
260                } catch (IOException e) {
261                    e.printStackTrace();
262                }
263            }
264            
265            try {
266                skey.cancel();
267                if (channel != null) {
268                    channel.close();
269                }
270                serverChannel.close();
271                selector.close();
272            } catch (IOException e) {
273                e.printStackTrace();
274            }
275        }
276
277    }
278}