|
| 1 | +/* |
| 2 | + * Copyright 2017 Red Hat Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Threading; |
| 19 | +using System.Collections.ObjectModel; |
| 20 | +using System.Text.RegularExpressions; |
| 21 | + |
| 22 | +using Amqp; |
| 23 | + |
| 24 | +namespace ClientLib |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Connector client class |
| 28 | + /// </summary> |
| 29 | + public class ConnectorClient : CoreClient |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Method for run connector |
| 33 | + /// </summary> |
| 34 | + /// <param name="args">cmd line arguments</param> |
| 35 | + public void Run(string[] args) |
| 36 | + { |
| 37 | + Collection<Connection> connections = new Collection<Connection>(); |
| 38 | + Collection<Session> sessions = new Collection<Session>(); |
| 39 | + Collection<SenderLink> senders = new Collection<SenderLink>(); |
| 40 | + Collection<ReceiverLink> receivers = new Collection<ReceiverLink>(); |
| 41 | + |
| 42 | + ConnectorOptions options = new ConnectorOptions(); |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + //parse args |
| 47 | + this.ParseArguments(args, options); |
| 48 | + |
| 49 | + //create connections |
| 50 | + if (Regex.IsMatch(options.ObjCtrl, "[CESR]")) |
| 51 | + { |
| 52 | + |
| 53 | + for (int i = 0; i < options.MsgCount; i++) |
| 54 | + { |
| 55 | + connections.Add(new Connection(new Address(options.Url))); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + //request to create at least a session (connection needed) |
| 60 | + if (Regex.IsMatch(options.ObjCtrl, "[ESR]")) |
| 61 | + { |
| 62 | + try |
| 63 | + { |
| 64 | + foreach (Connection conn in connections) |
| 65 | + { |
| 66 | + sessions.Add(new Session(conn)); |
| 67 | + } |
| 68 | + } |
| 69 | + catch (AmqpException ae) |
| 70 | + { |
| 71 | + Console.Error.WriteLine(ae.Message); |
| 72 | + for (int i = sessions.Count; i < connections.Count; i++) |
| 73 | + { |
| 74 | + sessions.Add(null); |
| 75 | + Environment.Exit(ReturnCode.ERROR_OTHER); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + string address = options.Address; |
| 80 | + |
| 81 | + if (address != String.Empty) |
| 82 | + { |
| 83 | + if (Regex.IsMatch(options.ObjCtrl, "[S]")) |
| 84 | + { |
| 85 | + try |
| 86 | + { |
| 87 | + int i = 0; |
| 88 | + foreach (Session s in sessions) |
| 89 | + { |
| 90 | + i++; |
| 91 | + if (s != null) |
| 92 | + senders.Add(new SenderLink(s, "sender" + i.ToString(), address)); |
| 93 | + else |
| 94 | + senders.Add(null); |
| 95 | + } |
| 96 | + } |
| 97 | + catch (AmqpException ae) |
| 98 | + { |
| 99 | + Console.Error.WriteLine(ae.Message); |
| 100 | + for (int i = senders.Count; i < sessions.Count; i++) |
| 101 | + { |
| 102 | + senders.Add(null); |
| 103 | + } |
| 104 | + Environment.Exit(ReturnCode.ERROR_OTHER); |
| 105 | + } |
| 106 | + } |
| 107 | + if (Regex.IsMatch(options.ObjCtrl, "[R]")) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + int i = 0; |
| 112 | + foreach (Session s in sessions) |
| 113 | + { |
| 114 | + i++; |
| 115 | + if (s != null) |
| 116 | + receivers.Add(new ReceiverLink(s, "tmp_rcv" + i.ToString(), address)); |
| 117 | + else |
| 118 | + receivers.Add(null); |
| 119 | + } |
| 120 | + } |
| 121 | + catch (AmqpException ae) |
| 122 | + { |
| 123 | + Console.Error.WriteLine(ae.Message); |
| 124 | + for (int i = receivers.Count; i < sessions.Count; i++) |
| 125 | + { |
| 126 | + receivers.Add(null); |
| 127 | + } |
| 128 | + Environment.Exit(ReturnCode.ERROR_OTHER); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + this.exitCode = ReturnCode.ERROR_SUCCESS; |
| 134 | + } |
| 135 | + catch (ArgumentException ex) |
| 136 | + { |
| 137 | + this.ArgumentExceptionHandler(ex, options); |
| 138 | + } |
| 139 | + catch (Exception ex) |
| 140 | + { |
| 141 | + this.OtherExceptionHandler(ex, options); |
| 142 | + } |
| 143 | + finally |
| 144 | + { |
| 145 | + if (options.CloseSleep > 0) |
| 146 | + Thread.Sleep(options.CloseSleep); |
| 147 | + |
| 148 | + foreach (ReceiverLink rec in receivers) |
| 149 | + { |
| 150 | + if (rec != null) |
| 151 | + { |
| 152 | + rec.Close(); |
| 153 | + } |
| 154 | + } |
| 155 | + foreach (SenderLink sen in senders) |
| 156 | + { |
| 157 | + if (sen != null) |
| 158 | + { |
| 159 | + sen.Close(); |
| 160 | + } |
| 161 | + } |
| 162 | + foreach (Session ses in sessions) |
| 163 | + { |
| 164 | + if (ses != null) |
| 165 | + { |
| 166 | + ses.Close(); |
| 167 | + } |
| 168 | + } |
| 169 | + foreach (Connection c in connections) |
| 170 | + { |
| 171 | + if (c != null) |
| 172 | + { |
| 173 | + c.Close(); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + Environment.Exit(this.exitCode); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments