/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Network { using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Xml.Serialization; using OpenTraffic.Model.Importer.Native; using OpenTraffic.Model.Result; using OpenTraffic.Model.Route; public enum CLIENT_STATUS { UNKNOWN, ALIVE, LOCKED, LOCKED_BY_OTHER }; [Serializable] public class ClientDescription : IDisposable { public int id { get; set; } public float Scale { get; set; } private string _name; private int _port; private IPEndPoint ip; [NonSerialized] private Client client; [XmlAttributeAttribute()] public string name { get { return _name; } set { _name = value; } } [XmlAttributeAttribute()] public int port { get { return _port; } set { _port = value; } } [XmlIgnore] private bool remote = true; private CLIENT_STATUS status; public ClientDescription() { remote = true; } public ClientDescription(string name, int port, float scale, bool remote) { this.remote = remote; this.name = name; this.port = port; this.Scale = scale; } public ClientDescription(string name, int port, float scale) { this.name = name; this.port = port; this.Scale = scale; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { // free managed resources if (client != null) { client.Dispose(); client = null; } } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public Client GetClient() { if (remote) { Network.Client ci = (Network.Client)Activator.GetObject(typeof(Network.Client), "tcp://" + name + ":" + port + "/client" + port + ".rem"); return ci; } else { if (client == null) { client = new Client(port, true); } return client; } } public bool StartSimulation(int SessionId) { Client ci = GetClient(); try { return ci.StartSimulation(SessionId); } catch (Exception) { return false; } } public void StartPreCalculation() { Client ci = GetClient(); ci.StartPrecalculation(); } public void StartGenerateNewRoutes() { Client ci = GetClient(); ci.StartGenerateNewRoutes(); } public void StartIteration() { Client ci = GetClient(); ci.StartIteration(); } public List GetRoutes(Model.TrafficModel m) { List rroutes = new List(); if (m != null) { Dictionary lookup = new Dictionary(); foreach (Model.TrafficModelLink l in m.TrafficModelLinks) { lookup.Add(l.Id, l); } foreach (Model.TrafficModelLink l in m.TrafficModelConnectors) { lookup.Add(l.Id, l); } List routes = GetClient().GetRoutesDescription(); foreach (RouteDescription rd in routes) { rroutes.Add(rd.GetRoute(lookup)); } } return rroutes; } public void AddRoutes(List routes, IDictionary part) { if (routes != null && part != null) { List rds = new List(); foreach (RouteContainer r in routes) { if (r.IsWithinPartion(part, this.id) && !(part[r.Source.Source.Id] == id)) { rds.Add(new RouteDescription(r)); } } GetClient().AddRoutesDescription(rds); } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] public void TransferModel(Model.TrafficModel m, List clients, ResultData res, int id, Model.SimulatorFactory.AbstractSimulatorFactory sf) { Client ci = this.GetClient(); Model.TrafficModel mod = m.MakeClientModel(id); if (!remote) { ci.TransferModel(m, clients, res, id, m.Partition, sf); } else { ci.TransferModel(NativeFileImporter.SerializeRaw(mod), clients, res, id, m.Partition, sf); } } public ResultData FetchResult() { Client cl = GetClient(); return cl.FetchResult(); } public bool IterationFinished() { return this.GetClient().substate == CLIENT_SUBSTATES.ENDED; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public int GetProcentFinished() { return this.GetClient().PercentFinished; } public bool StartRestartIteration() { Client ci = this.GetClient(); return ci.StartRestartIteration(); } public void SetHistoricalResult(ResultData res) { Client ci = this.GetClient(); ci.SetHistoricalResult(res); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public bool GetOnline() { Client ci = this.GetClient(); try { return ci.IsOnline(); } catch (Exception) { return false; } } public void setStatus(CLIENT_STATUS s) { this.status = s; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public CLIENT_STATUS getStatus() { return status; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public IPEndPoint GetIPEndPoint() { if (ip == null) { IPHostEntry iphe = Dns.GetHostEntry(this.name); try { foreach (IPAddress tip in iphe.AddressList) { if (!tip.IsIPv6LinkLocal && !tip.IsIPv6Multicast && !tip.IsIPv6SiteLocal) { ip = new IPEndPoint(tip, port); return ip; } } } catch { return null; } } return ip; } public void Abort() { GetClient().Abort(); } public float MeasureTransferRate(float f) { byte[] bs = new byte[(int)f]; long startTicks = DateTime.Now.Ticks; GetClient().TransferTrash(bs); return (DateTime.Now.Ticks - startTicks) / TimeSpan.TicksPerSecond; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public TrafficProfile GetProfile() { return GetClient().GetProfile(); } public static List Scan() { List list = new List(); System.Net.Sockets.Socket socket = null; try { socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Bind(new IPEndPoint(IPAddress.Any, Client.BROADCAST_PORT)); byte[] bs = new byte[32768]; Dictionary dict = new Dictionary(); for (int i = 0; i < 5; i++) { if (socket.Available > 0) { EndPoint ip = (EndPoint)new IPEndPoint(IPAddress.Any, 0); socket.ReceiveFrom(bs, ref ip); IPEndPoint ipe = (IPEndPoint)ip; if (!dict.ContainsKey(ipe)) { dict.Add(ipe, true); list.Add(new ClientDescription(ipe.Address.ToString(), ipe.Port, 1.0f)); } } System.Threading.Thread.Sleep(300); } } finally { socket.Close(); } return list; } } }