Example #1
0
        public static void init(ConnectionController cc, RoutingController rc, LinkResourceManager lrm)
        {
            connectionController    = cc;
            routingController       = rc;
            linkResourceManager     = lrm;
            SocketsByAddress        = new Dictionary <SubnetworkAddress, CSocket>();
            SocketsToAnotherDomains = new Dictionary <SubnetworkAddress, int>();
            String parentSubnetworkAddress = Config.getProperty("ParentSubnetworkAddress");

            if (parentSubnetworkAddress != null)
            {
                int parentSubnetworkPort = Config.getIntegerProperty("ParentSubnetworkPort");
                ConnectToParentSubnetwork(IPAddress.Parse(parentSubnetworkAddress), parentSubnetworkPort);
                SendMySubnetworkInformation();
            }
            LoadPortsToAnotherDomains();
            InitListeningCustomSocket();
        }
Example #2
0
 private static void LoadEdgeSNPPs(ConnectionController cc, LinkResourceManager lrm)
 {
     string[] loaded = LoadFile(Config.getProperty("portsToDomainsFile"));
     string[] splitedParameters;
     foreach (string str in loaded)
     {
         if (str[0] != '#')
         {
             splitedParameters = str.Split(' ');
             lrm.AddEdgeSNPP(new SNPP(splitedParameters[MY_SNPP_ADDRESS], Int32.Parse(splitedParameters[MY_SNPP_CAPACITY])));
             cc.AddKeyToDictionary(new SubnetworkAddress(splitedParameters[SUBNET_ADDRESS_POSITION], splitedParameters[SUBNET_MASK_POSITION]));
         }
     }
     foreach (string str in loaded)
     {
         if (str[0] != '#')
         {
             splitedParameters = str.Split(' ');
             cc.AddValueToDictionary(new SubnetworkAddress(splitedParameters[SUBNET_ADDRESS_POSITION], splitedParameters[SUBNET_MASK_POSITION]), new Tuple <IPAddress, IPAddress>(IPAddress.Parse(splitedParameters[MY_SNPP_ADDRESS]), IPAddress.Parse(splitedParameters[EXT_SNPP_ADDRESS])));
         }
     }
 }
Example #3
0
        static void Main(string[] args)
        {
            Console.Title = "Subnetwork " + CustomSocket.Config.getProperty("SubnetworkAddress");
            ConnectionController CC  = new ConnectionController();
            LinkResourceManager  LRM = new LinkResourceManager();
            RoutingController    RC  = new RoutingController(CC.ContainedSubnetworksAddresses, LRM.Links);

            SubnetworkServer.init(CC, RC, LRM);
            LoadEdgeSNPPs(CC, LRM);

            string decision;

            do
            {
                decision = Console.ReadLine().Trim();
                if (decision.StartsWith("kill"))
                {
                    string[] killParams       = decision.Split(' ');
                    string   firstSNPaddress  = killParams[1];
                    string   secondSNPaddress = killParams[2];
                    CustomSocket.LogClass.MagentaLog("Removing link: " + firstSNPaddress + " - " + secondSNPaddress);

                    List <Tuple <string, string, int> > pathsToReroute = CC.GetPathsContainingThisSNP(firstSNPaddress);
                    foreach (var path in pathsToReroute)
                    {
                        CC.DeleteConnection(path.Item1, path.Item2);
                        RC.DeleteLink(firstSNPaddress, secondSNPaddress);
                        CC.ConnectionRequestFromNCC(path.Item1, path.Item2, path.Item3);
                    }
                }
                else if (decision.StartsWith("restore"))
                {
                    string[] restoreParams     = decision.Split(' ');
                    string   firstSNPPaddress  = restoreParams[1];
                    string   secondSNPPaddress = restoreParams[2];
                    RC.RestoreLink(firstSNPPaddress, secondSNPPaddress);
                }
            }while (decision != "exit");
        }
Example #4
0
        static void Main(string[] args)
        {
            try
            {
                subnetwork.readInfo(args[0]);
            }
            catch (Exception e)
            {
            }
            byte[] buffer = new byte[128];
            subnetwork.subClientToCloud.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), (int)subnetwork.cloudPort));
            subnetwork.subClientToCloud.Send(Encoding.ASCII.GetBytes("First Message " + subnetwork.ip.ToString()));
            subnetwork.subClientToCloud.Receive(buffer);
            List <ushort> ports = subnetwork.givePorts(buffer);

            /*foreach(var port in ports)
             * {
             *  LinkResourceManager link = new LinkResourceManager(port);
             *  link.IPofNode = subnetwork.ip;
             *  subnetwork.lrms.Add(link);
             * }*/
            // tworzenie interfaceĆ³w
            //1.znajdz min port
            //0ushort p = ushort.MaxValue;

            while (ports.Count > 0)
            {
                ushort p = ushort.MaxValue;
                foreach (var port in ports)
                {
                    if (port < p)
                    {
                        p = port;
                    }
                }
                ports.Remove(p);
                foreach (var port in ports)
                {
                    if ((port - p) == 1)
                    {
                        Interface           inter = new Interface(subnetwork.ip, p);
                        LinkResourceManager link  = new LinkResourceManager(p);
                        LinkResourceManager link1 = new LinkResourceManager(port);
                        link1.IPofNode = subnetwork.ip;
                        link.IPofNode  = subnetwork.ip;
                        subnetwork.lrmForDomain.Add(link);
                        //subnetwork.lrms
                        subnetwork.interfaces.Add(inter);
                        ports.Remove(port);
                        // p = port;
                        break;
                    }
                }
            }
            List <byte> bufferLRM = new List <byte>();

            bufferLRM.AddRange(Encoding.ASCII.GetBytes("SUBNETWORK-callin " + subnetwork.ip.ToString() + " " + subnetwork.port + " "));
            // int i = 0;
            foreach (LinkResourceManager lrm in subnetwork.lrmForDomain)
            {
                //bufferLRM.Add(0);
                bufferLRM.AddRange(lrm.convertToBytes());
                //  buffer2.AddRange(lrm.convertToBytes());
                // Console.WriteLine((ushort)((buffer2[i + 1] << 8) + buffer2[i]));
                // i += 16;
            }
            subnetwork.subClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), (int)subnetwork.portDomain));

            subnetwork.subClient.Send(bufferLRM.ToArray());
            Thread thread = new Thread(WaitForData);

            thread.Start();
            subnetwork.subServer.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), (int)subnetwork.port));
            // subnetwork.subClient = new Socket(IPAddress.Parse("127.0.0.1").AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            subnetwork.subServer.Listen(50);
            Console.WriteLine((int)subnetwork.portDomain + " " + (int)subnetwork.port);

            while (true)
            {
                subnetwork.subDone.Reset();
                subnetwork.subServer.BeginAccept(new AsyncCallback(AcceptCallBack), subnetwork.subServer);
                subnetwork.subDone.WaitOne();
            }
        }
Example #5
0
        public static void ReceiveCallBack(IAsyncResult asyncResult)
        {
            StateObject state   = (StateObject)asyncResult.AsyncState;
            Socket      handler = state.workSocket; //socket of client
            int         ReadBytes;

            try
            {
                ReadBytes = handler.EndReceive(asyncResult);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, ReadBytes));
            var message = state.sb.ToString().Split(' ');

            if (message[0].Equals("CC-callin"))
            {
                IPAddress ip = IPAddress.Parse(message[1]);
                subnetwork.CC.IPfromSocket.Add(handler, ip);
                subnetwork.CC.SocketfromIP.Add(ip, handler);



                subnetwork.RC.nodesToAlgorithm.Add(IPAddress.Parse(message[1]));
                List <byte> bufferLRM = new List <byte>();
                bufferLRM.AddRange(Encoding.ASCII.GetBytes(message[2]));

                /* for(int j=0; j<bufferLRM.Count;j++)
                 * {
                 *   Console.Write(bufferLRM[j] + " ");
                 *
                 * }*/
                Console.WriteLine();

                /*  ushort port1 = (ushort)((bufferLRM[1] << 8) + bufferLRM[0]);
                 * Console.WriteLine(port1);
                 * Console.WriteLine(bufferLRM.Count);*/
                byte[] buffer = new byte[16];
                int    i      = 0;
                while (i < bufferLRM.Count)
                {
                    buffer = bufferLRM.GetRange(i, 16).ToArray();
                    ushort port = (ushort)((buffer[1] << 8) + buffer[0]);
                    Console.WriteLine(port);
                    LinkResourceManager LRM = LinkResourceManager.returnLRM(buffer);
                    i += 16;
                    Console.WriteLine("Port: " + LRM.port);
                    subnetwork.RC.lrms.Add(LRM);
                }
            }
            if (message[0].Equals("SET-CONNECTION"))
            {
                ushort    portS           = ushort.Parse(message[1]);
                ushort    portF           = ushort.Parse(message[2]);
                int       startSlot       = int.Parse(message[3]);
                int       finishSlot      = int.Parse(message[4]);
                int       speed           = int.Parse(message[5]);
                int       len_from_domain = int.Parse(message[6]);
                ushort    innerPortSource = (ushort)(portS + 1);
                ushort    innerPortDest   = (ushort)(portF + 1);
                ushort    sourceInPort    = 0;
                ushort    sourceOutPort   = 0;
                ushort    destOutPort     = 0;
                ushort    destInPort      = 0;
                IPAddress source          = null;
                IPAddress destination     = null;
                Console.WriteLine("ports from domain: " + portS + " " + portF);
                Console.WriteLine("Start and last slot: " + startSlot + " " + finishSlot);
                Console.WriteLine("Speed: " + speed);
                Console.WriteLine("Length from domain: " + len_from_domain);

                foreach (var cable in subnetwork.RC.cables)
                {
                    if (cable.port1.Equals(innerPortSource))
                    {
                        source       = cable.Node2;
                        sourceInPort = cable.port2;
                    }
                    else if (cable.port2.Equals(innerPortSource))
                    {
                        source       = cable.Node1;
                        sourceInPort = cable.port1;
                    }
                    if (cable.port1.Equals(innerPortDest))
                    {
                        destination = cable.Node2;
                        destOutPort = cable.port2;
                    }
                    else if (cable.port2.Equals(innerPortDest))
                    {
                        destination = cable.Node1;
                        destOutPort = cable.port1;
                    }
                }

                RoutingResult routingResult = subnetwork.RC.SubentDijkstraAlgorithm(source, destination, subnetwork.RC.cables, subnetwork.RC.lrms, speed, startSlot, finishSlot, len_from_domain);

                foreach (var node in routingResult.Path)
                {
                    Cable cable = findCableBetweenNodes(node, destination, subnetwork.RC.cables);
                    if (!cable.Equals(null))
                    {
                        if (cable.Node1.Equals(destination))
                        {
                            destInPort = cable.port1;
                            break;
                        }
                        if (cable.Node2.Equals(destination))
                        {
                            destInPort = cable.port2;
                            break;
                        }
                    }
                }


                List <byte> bufferToSend = new List <byte>();
                int         ct           = 0;
                Socket      destSocket   = subnetwork.CC.SocketfromIP[destination];


                bufferToSend.AddRange(Encoding.ASCII.GetBytes("ACK"));
                bufferToSend.AddRange(BitConverter.GetBytes(startSlot));
                bufferToSend.AddRange(BitConverter.GetBytes(finishSlot));
                bufferToSend.AddRange(BitConverter.GetBytes(destOutPort));
                bufferToSend.AddRange(BitConverter.GetBytes(destInPort));
                destSocket.BeginSend(bufferToSend.ToArray(), 0, bufferToSend.ToArray().Length, 0,
                                     new AsyncCallback(SendCallBack), destSocket);
                bufferToSend.Clear();
                foreach (var cab in routingResult.nodeAndPortsOut)
                {
                    if (cab.Key.Equals(source))
                    {
                        Socket socket1 = subnetwork.CC.SocketfromIP[cab.Key];
                        sourceOutPort = cab.Value;
                        bufferToSend.AddRange(Encoding.ASCII.GetBytes("ACK"));
                        bufferToSend.AddRange(BitConverter.GetBytes(startSlot));
                        bufferToSend.AddRange(BitConverter.GetBytes(finishSlot));
                        bufferToSend.AddRange(BitConverter.GetBytes(sourceOutPort));
                        bufferToSend.AddRange(BitConverter.GetBytes(sourceInPort));
                        socket1.BeginSend(bufferToSend.ToArray(), 0, bufferToSend.ToArray().Length, 0,
                                          new AsyncCallback(SendCallBack), socket1);
                        bufferToSend.Clear();
                        continue;
                    }
                    bool   flaga  = false;
                    Socket socket = subnetwork.CC.SocketfromIP[cab.Key];
                    Console.WriteLine("Adres: " + cab.Key + " port out: " + cab.Value);
                    foreach (var cab1 in routingResult.nodeAndPortsIn)
                    {
                        if (cab1.Key.Equals(cab.Key))
                        {
                            Console.WriteLine("Port in: " + cab1.Value);
                            bufferToSend.AddRange(Encoding.ASCII.GetBytes("ACK"));
                            bufferToSend.AddRange(BitConverter.GetBytes(startSlot));
                            bufferToSend.AddRange(BitConverter.GetBytes(finishSlot));
                            bufferToSend.AddRange(BitConverter.GetBytes(cab.Value));
                            bufferToSend.AddRange(BitConverter.GetBytes(cab1.Value));
                            socket.BeginSend(bufferToSend.ToArray(), 0, bufferToSend.ToArray().Length, 0,
                                             new AsyncCallback(SendCallBack), socket);
                            bufferToSend.Clear();
                            flaga = true;
                            break;
                        }
                    }
                }
                Console.WriteLine("Send");
            }
            state.sb.Clear();
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallBack), state);
        }