Пример #1
0
        public static void Start()
        {
            BinaryFormatter bf = new BinaryFormatter ();
            MemoryStream ms = new MemoryStream ();
            //int numberofobjects = 30;
            //string[] str = new string[]{"ImageTarget", "ImageTarget (1)", "ImageTarget (2)"};
            for(int i = 0; i<numberofobjects; i++){
                Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect (new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234));
                Random rndweight=new Random();
                int length = rndweight.Next(100,10000);
                byte[] obj = new byte[length];
                Random rnd = new Random ();
                rnd.NextBytes (obj);
                double xi = 1000 * rnd.NextDouble();
                double yi = 1000 * rnd.NextDouble();
                InsertQuery iq = new InsertQuery(new AskObject(new float[]{(float)xi,(float)yi},1,i,obj,0));

                ms = new MemoryStream();
                bf.Serialize(ms, iq);
                socket.Send(ms.ToArray());
                byte[] instream = new byte[100000];
                socket.Receive (instream);
                ms = new MemoryStream (instream);
                object obj2 = bf.Deserialize (ms);
                if (obj2 is BoolIntResult) {
                    BoolIntResult or = (BoolIntResult) obj2;
                    objectX[i]=(float) xi;
                    objectY[i] = (float)yi;

                } else {
                    Console.WriteLine("Wrong kind of object.");
                }
                socket.Close();
            }
        }
Пример #2
0
    Tuple<bool, int> InsertObject(InsertQuery insertQuery)
    {
        AskObject obj = insertQuery.askObject;
            float[] coord = obj.position;

            lock (engaged) {
                try {
                    KdTreeNode<float, int>[] neighbors = KDTree.RadialSearch (coord, 2 * maxRadius, 100);

                    if (neighbors.Length >= 1) {
                        return new Tuple<bool, int> (false, -1);
                    }

                    obj.objectId = maxObjectId + 1;
                    bool ifadd = KDTree.Add (obj.position, obj.objectId);
                    if (!ifadd) {
                        Console.WriteLine ("Object already present.");
                        return new Tuple<bool, int> (false, -3);
                    }
                    idMap.Add (obj.objectId, obj);
                    maxObjectId++;

                } catch (Exception e) {
                    Console.WriteLine (e.Message);
                    return new Tuple<bool, int> (false, -2);
                }
            }
            //KdTreeNode<float, int>[] myNeigh = KDTree.RadialSearch (new float[2] { 0.0f, 0.0f }, 1000000, 100);
            //Console.WriteLine ("Tree has " + myNeigh.Length.ToString () + " Children");
            return new Tuple<bool, int> (true, insertQuery.askObject.objectId);
    }