Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.packetstreamer; |
| 2 | |
| 3 | import net.floodlightcontroller.packetstreamer.thrift.*; |
| 4 | |
| 5 | import java.util.List; |
| 6 | import java.util.ArrayList; |
| 7 | |
| 8 | import org.apache.thrift.TException; |
| 9 | import org.apache.thrift.transport.TFramedTransport; |
| 10 | import org.apache.thrift.transport.TTransport; |
| 11 | import org.apache.thrift.transport.TSocket; |
| 12 | import org.apache.thrift.transport.TTransportException; |
| 13 | import org.apache.thrift.protocol.TBinaryProtocol; |
| 14 | import org.apache.thrift.protocol.TProtocol; |
| 15 | |
| 16 | import org.slf4j.Logger; |
| 17 | import org.slf4j.LoggerFactory; |
| 18 | |
| 19 | /** |
| 20 | * The PacketStreamer Sample Client. |
| 21 | */ |
| 22 | public class PacketStreamerClient { |
| 23 | protected static Logger log = LoggerFactory.getLogger(PacketStreamerClient.class); |
| 24 | |
| 25 | /** |
| 26 | * Main function entry point; |
| 27 | * @param args |
| 28 | */ |
| 29 | public static void main(String [] args) { |
| 30 | try { |
| 31 | int serverPort = Integer.parseInt(System.getProperty("net.floodlightcontroller.packetstreamer.port", "9090")); |
| 32 | TTransport transport; |
| 33 | transport = new TFramedTransport(new TSocket("localhost", serverPort)); |
| 34 | transport.open(); |
| 35 | |
| 36 | |
| 37 | TProtocol protocol = new TBinaryProtocol(transport); |
| 38 | PacketStreamer.Client client = new PacketStreamer.Client(protocol); |
| 39 | |
| 40 | sendPackets(client, (short)2, OFMessageType.PACKET_IN, true); |
| 41 | log.debug("Terminate session1"); |
| 42 | client.terminateSession("session1"); |
| 43 | |
| 44 | transport.close(); |
| 45 | } catch (TException x) { |
| 46 | x.printStackTrace(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Send test packets of the given OFMessageType to the packetstreamer server; |
| 52 | * @param client Packetstreamer client object |
| 53 | * @param numPackets number of test packets to be sent |
| 54 | * @param ofType OFMessageType of the test packets |
| 55 | * @param sync true if send with synchronous interface, false for asynchronous interface |
| 56 | * @throws TException |
| 57 | */ |
| 58 | private static void sendPackets(PacketStreamer.Client client, short numPackets, OFMessageType ofType, boolean sync) |
| 59 | throws TException { |
| 60 | while (numPackets-- > 0) { |
| 61 | Message msg = new Message(); |
| 62 | Packet packet = new Packet(); |
| 63 | |
| 64 | List<String> sids = new ArrayList<String>(); |
| 65 | sids.add("session1"); |
| 66 | sids.add("session2"); |
| 67 | msg.setSessionIDs(sids); |
| 68 | packet.setMessageType(ofType); |
| 69 | long sw_dpid = numPackets/40 + 1; |
| 70 | packet.setSwPortTuple(new SwitchPortTuple(sw_dpid, (short)(numPackets - (sw_dpid-1)*40))); |
| 71 | |
| 72 | String strData = "New data, sequence " + numPackets; |
| 73 | packet.setData(strData.getBytes()); |
| 74 | msg.setPacket(packet); |
| 75 | |
| 76 | try { |
| 77 | if (sync) { |
| 78 | client.pushMessageSync(msg); |
| 79 | log.debug("Send packet sync: " + msg.toString()); |
| 80 | } else { |
| 81 | client.pushMessageAsync(msg); |
| 82 | log.debug("Send packet sync: " + msg.toString()); |
| 83 | } |
| 84 | } catch (TTransportException e) { |
| 85 | log.error(e.toString()); |
| 86 | } |
| 87 | |
| 88 | try { |
| 89 | Thread.sleep(100); |
| 90 | } catch (Exception e) {} |
| 91 | } |
| 92 | } |
| 93 | } |