PageRenderTime 56ms CodeModel.GetById 17ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/functional/SccpHarness.java

http://mobicents.googlecode.com/
Java | 272 lines | 136 code | 45 blank | 91 comment | 1 complexity | 1d55c9f91a963782ffb5ee61bc7d1109 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.map.functional;
 24
 25import java.io.FileOutputStream;
 26import java.io.IOException;
 27
 28import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
 29import org.mobicents.protocols.ss7.mtp.Mtp3UserPartBaseImpl;
 30import org.mobicents.protocols.ss7.sccp.SccpProvider;
 31import org.mobicents.protocols.ss7.sccp.impl.RemoteSignalingPointCode;
 32import org.mobicents.protocols.ss7.sccp.impl.RemoteSubSystem;
 33import org.mobicents.protocols.ss7.sccp.impl.SccpResource;
 34import org.mobicents.protocols.ss7.sccp.impl.SccpStackImpl;
 35import org.mobicents.protocols.ss7.sccp.impl.router.Router;
 36
 37/**
 38 * @author amit bhayani
 39 * 
 40 */
 41public abstract class SccpHarness {
 42
 43	protected SccpStackImpl sccpStack1 = new SccpStackImpl();
 44	protected SccpProvider sccpProvider1 = sccpStack1.getSccpProvider();
 45
 46	protected SccpStackImpl sccpStack2 = new SccpStackImpl();
 47	protected SccpProvider sccpProvider2 = sccpStack2.getSccpProvider();
 48
 49//	private ConcurrentLinkedQueue<byte[]> data1 = new ConcurrentLinkedQueue<byte[]>();
 50//	private ConcurrentLinkedQueue<byte[]> data2 = new ConcurrentLinkedQueue<byte[]>();
 51
 52	private Mtp3UserPartImpl mtp3UserPart1 = new Mtp3UserPartImpl();
 53	private Mtp3UserPartImpl mtp3UserPart2 = new Mtp3UserPartImpl();
 54
 55	protected Router router1 = new Router();
 56	protected Router router2 = new Router();
 57
 58	protected SccpResource resource1 = new SccpResource();
 59	protected SccpResource resource2 = new SccpResource();
 60
 61	
 62	/**
 63	 * 
 64	 */
 65	public SccpHarness() {
 66		mtp3UserPart1.setOtherPart(mtp3UserPart2);
 67		mtp3UserPart2.setOtherPart(mtp3UserPart1);
 68	}
 69
 70	private void setUpStack1() {
 71		try {
 72			router1.start();
 73			router1.getRules().clear();
 74			router1.getPrimaryAddresses().clear();
 75			router1.getBackupAddresses().clear();
 76		} catch (Exception e) {
 77			// ignore
 78		}
 79
 80		resource1.start();
 81		resource1.getRemoteSpcs().clear();
 82		resource1.getRemoteSsns().clear();
 83
 84		resource1.getRemoteSpcs().put(1, new RemoteSignalingPointCode(2, 0, 0));
 85		resource1.getRemoteSsns().put(1, new RemoteSubSystem(2, 8, 0));
 86
 87		sccpStack1.setRouter(router1);
 88		sccpStack1.setSccpResource(resource1);
 89		sccpStack1.setLocalSpc(1);
 90		sccpStack1.setNi(2);
 91		sccpStack1.setMtp3UserPart(mtp3UserPart1);
 92		sccpStack1.start();
 93	}
 94
 95	private void setUpStack2() {
 96		try {
 97			router2.start();
 98			router2.getRules().clear();
 99			router2.getPrimaryAddresses().clear();
100			router2.getBackupAddresses().clear();
101		} catch (Exception e) {
102			// ignore
103		}
104
105		resource2.start();
106		resource2.getRemoteSpcs().clear();
107		resource2.getRemoteSsns().clear();
108
109		resource2.getRemoteSpcs().put(1, new RemoteSignalingPointCode(1, 0, 0));
110		resource2.getRemoteSsns().put(1, new RemoteSubSystem(1, 8, 0));
111
112		sccpStack2.setRouter(router2);
113		sccpStack2.setSccpResource(resource2);
114		sccpStack2.setLocalSpc(2);
115		sccpStack2.setNi(2);
116		sccpStack2.setMtp3UserPart(mtp3UserPart2);
117		sccpStack2.start();
118	}
119
120	private void tearDownStack1() {
121		router1.getRules().clear();
122		router1.getPrimaryAddresses().clear();
123		router1.getBackupAddresses().clear();
124		router1.stop();
125
126		resource1.getRemoteSpcs().clear();
127		resource1.getRemoteSsns().clear();
128		resource1.stop();
129
130		sccpStack1.stop();
131
132	}
133
134	private void tearDownStack2() {
135		router1.getRules().clear();
136		router1.getPrimaryAddresses().clear();
137		router1.getBackupAddresses().clear();
138		router1.stop();
139
140		resource1.getRemoteSpcs().clear();
141		resource1.getRemoteSsns().clear();
142		resource1.stop();
143
144		sccpStack1.stop();
145
146	}
147
148	public void setUp() {
149		this.setUpStack1();
150		this.setUpStack2();
151	}
152
153	public void tearDown() {
154		this.tearDownStack1();
155		this.tearDownStack2();
156	}
157	
158	/**
159	 * After this method invoking all MTP traffic will be save into the file "MsgLog.txt"
160	 * file format:
161	 * [message][message]...[message]
162	 * [message] ::= { byte-length low byte, byte-length high byte, byte[] message }
163	 */
164	public void saveTrafficInFile() {
165		((Mtp3UserPartImpl) this.mtp3UserPart1).saveTrafficInFile = true;
166		((Mtp3UserPartImpl) this.mtp3UserPart2).saveTrafficInFile = true;
167		
168		try {
169			FileOutputStream fs = new FileOutputStream("MsgLog.txt", false);
170			fs.close();
171		} catch (Exception e) {
172			// TODO Auto-generated catch block
173			e.printStackTrace();
174		}
175	}
176
177	private class Mtp3UserPartImpl extends Mtp3UserPartBaseImpl {
178
179//		private ConcurrentLinkedQueue<byte[]> readFrom;
180//		private ConcurrentLinkedQueue<byte[]> writeTo;
181		
182		private Mtp3UserPartImpl otherPart;
183		
184		private boolean saveTrafficInFile = false;
185
186		Mtp3UserPartImpl() {
187			try {
188				this.start();
189			} catch (Exception e) {
190				// TODO Auto-generated catch block
191				e.printStackTrace();
192			}
193		}
194		
195		public void setOtherPart(Mtp3UserPartImpl otherPart) {
196			this.otherPart = otherPart;
197		}
198
199		@Override
200		public void sendMessage(Mtp3TransferPrimitive msg) throws IOException {
201			if (saveTrafficInFile) {
202				try {
203					byte[] txData = msg.encodeMtp3();
204					FileOutputStream fs = new FileOutputStream("MsgLog.txt", true);
205					int ln = txData.length;
206					fs.write(ln & 0xFF);
207					fs.write(ln >> 8);
208					fs.write(txData);
209					fs.close();
210				} catch (Exception e) {
211					// TODO Auto-generated catch block
212					e.printStackTrace();
213				}
214			}
215
216			this.otherPart.sendTransferMessageToLocalUser(msg, msg.getSls());
217		}
218
219//		@Override
220//		public int read(ByteBuffer b) throws IOException {
221//			int dataRead = 0;
222//			while (!this.readFrom.isEmpty()) {
223//				byte[] rxData = this.readFrom.poll();
224//				System.out.println("Read " + Arrays.toString(rxData));
225//				dataRead = dataRead + rxData.length;
226//				b.put(rxData);
227//			}
228//			return dataRead;
229//		}
230//
231//		@Override
232//		public int write(ByteBuffer b) throws IOException {
233//			int dataAdded = 0;
234//			if (b.hasRemaining()) {
235//				byte[] txData = new byte[b.limit() - b.position()];
236//				b.get(txData);
237//				dataAdded = dataAdded + txData.length;
238//				System.out.println("Write " + Arrays.toString(txData));
239//				this.writeTo.add(txData);
240//				
241//				if (saveTrafficInFile) {
242//					try {
243//						FileOutputStream fs = new FileOutputStream("MsgLog.txt", true);
244//						int ln = txData.length;
245//						fs.write(ln & 0xFF);
246//						fs.write(ln >> 8);
247//						fs.write(txData);
248//						fs.close();
249//					} catch (Exception e) {
250//						// TODO Auto-generated catch block
251//						e.printStackTrace();
252//					}
253//				}
254//			}
255//			return dataAdded;
256//		}
257
258//		@Override
259//		public void execute() throws IOException {
260//			//We dont use this
261//			
262//			try {
263//				//FIXME: this has to be changed to some MTP change!!!
264//				Thread.sleep(100);
265//			} catch( Exception e ) {
266//			}
267//		}
268
269
270	}
271
272}