예제 #1
0
        public void serverReadRequest()
        {
            Console.WriteLine("clientReceiveRequest 1 " + server_facet);

            byte[] buffer = requestQueue.Take();
            int    nbytes = buffer.Length;

            Console.WriteLine("clientReceiveRequest 2 " + nbytes + " bytes received.");

            Monitor.Enter(sync);
            try
            {
                // Aguarda uma resposta proveniente do outro root parceiro.
                byte[] messageSide1_enveloped_raw = new byte[nbytes];
                Console.WriteLine("clientReceiveRequest 2-1 nbytes=" + nbytes);

                // TODO: otimizar isso ...
                for (int i = 0; i < nbytes; i++)
                {
                    messageSide1_enveloped_raw[i] = buffer[i];
                }
                Tuple <EnvelopType, byte[]> messageSide1_enveloped = (Tuple <EnvelopType, byte[]>)ByteArrayToObject(messageSide1_enveloped_raw);

                EnvelopType envelop     = messageSide1_enveloped.Item1;
                EnvelopKey  envelop_key = new EnvelopKey(envelop);

                // Coloca a resposta no "conjunto de respostas pendentes de requisição"
                if (!reply_pending_list.ContainsKey(envelop_key))
                {
                    reply_pending_list [envelop_key] = new Queue <byte[]>();
                }

                reply_pending_list [envelop_key].Enqueue(messageSide1_enveloped.Item2);

                Console.WriteLine("clientReceiveRequest 3 " + envelop.Item1 + "," + envelop_key);
                foreach (EnvelopKey ek in request_pending_list.Keys)
                {
                    Console.WriteLine("key: " + ek);
                }

                // Busca, no "conjunto de requisições pendentes de resposta", a requisição correspondente a resposta.
                if (request_pending_list.ContainsKey(envelop_key))
                {
                    Console.WriteLine("clientReceiveRequest 3-1" + " / " + envelop_key);
                    AutoResetEvent sync_send = request_pending_list[envelop_key];
                    //Monitor.Pulse (sync_send);
                    sync_send.Set();
                    request_pending_list.Remove(envelop_key);
                    Console.WriteLine("clientReceiveRequest 3-2" + " / " + envelop_key);
                }
            }
            finally
            {
                Monitor.Exit(sync);
            }
            Console.WriteLine("clientReceiveRequest 4");
        }
        public override bool Equals(object obj)
        {
            EnvelopKey fooItem = obj as EnvelopKey;

            return(fooItem.ToString().Equals(this.ToString()));
        }
        public byte[] clientSendRequestAnyTag(EnvelopType envelop, byte[] messageSide1, ref int envelop_tag)
        {
            EnvelopKey envelop_key = new EnvelopKey(envelop);

            Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 1" + " / " + envelop_key + " -- " + instance_name);

            byte[] messageSide2 = null;
            Monitor.Enter(sync);
            try
            {
                // envia a requisição para o root parceiro
                int facet = envelop.Item3;
                Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag send to facet " + facet + " - nofsockets=" + client_socket_facet.Count + " / " + envelop_key + " -- " + instance_name);
                foreach (int f in client_socket_facet.Keys)
                {
                    Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag --- FACET KEY=" + f);
                }
                Socket socket = client_socket_facet [facet];
                byte[] messageSide1_enveloped_raw = ObjectToByteArray(new Tuple <EnvelopType, byte[]> (envelop, messageSide1));
                Int32  length = messageSide1_enveloped_raw.Length;
                byte[] messageSide1_enveloped_raw_ = new byte[4 + length];
                BitConverter.GetBytes(length).CopyTo(messageSide1_enveloped_raw_, 0);
                Array.Copy(messageSide1_enveloped_raw, 0, messageSide1_enveloped_raw_, 4, length);

                socket.Send(messageSide1_enveloped_raw_);

                Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 2 nbytes=" + messageSide1_enveloped_raw.Length + " / " + envelop_key);

                // Verifica se já há resposta para a requisição no "conjunto de respostas pendentes de requisição"
                if (!reply_pending_list.ContainsKey(envelop_key))
                {
                    Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 3 - BEFORE WAIT " + envelop_key);
                    // Se não houver, coloca um item no "conjunto de requisições pendentes de resposta" e espera.

                    if (!request_pending_list.ContainsKey(envelop_key))
                    {
                        request_pending_list [envelop_key] = new Dictionary <int, Queue <AutoResetEvent> >();
                    }

                    if (!request_pending_list [envelop_key].ContainsKey(-1))
                    {
                        request_pending_list [envelop_key][-1] = new Queue <AutoResetEvent>();
                        request_pending_list [envelop_key][-1].Enqueue(new AutoResetEvent(false));
                    }

                    AutoResetEvent sync_send = request_pending_list [envelop_key][-1].Peek();

                    //request_pending_list [envelop_key][envelop_tag] = sync_send;
                    Monitor.Exit(sync);
                    Console.WriteLine("clientSendRequestAny - WAIT / " + unit.CID.getInstanceName() + "/" + sync_send.GetHashCode() + " BEFORE !!! ");
                    sync_send.WaitOne();
                    Console.WriteLine("clientSendRequestAny - WAIT / " + unit.CID.getInstanceName() + "/" + sync_send.GetHashCode() + " AFTER !!! ");
                    Monitor.Enter(sync);
                    Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 3 - AFTER WAIT " + envelop_key);
                }
                Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 4" + " / " + envelop_key);

                int[] keys_vector = new int[reply_pending_list[envelop_key].Keys.Count];
                reply_pending_list[envelop_key].Keys.CopyTo(keys_vector, 0);

                envelop_tag = keys_vector[0];
                Queue <byte[]> pending_replies = reply_pending_list [envelop_key][envelop_tag];
                Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequestAnyTag 5 -- pending_replies.Count = " + pending_replies.Count);
                if (pending_replies.Count > 0)
                {
                    messageSide2 = reply_pending_list[envelop_key][envelop_tag].Dequeue();
                }

                if (pending_replies.Count == 0)
                {
                    reply_pending_list[envelop_key].Remove(envelop_tag);
                }

                if (reply_pending_list[envelop_key].Count == 0)
                {
                    reply_pending_list.Remove(envelop_key);
                }

                //reply_pending_list.Remove(envelop_key);
            }
            finally
            {
                Monitor.Exit(sync);
            }

            Trace.WriteLineIf(unit.TraceFlag == true, server_facet + "/" + rank + ": clientSendRequest 5");
            // retorna a menagem ...
            return(messageSide2);
        }
예제 #4
0
        public byte[] clientSendRequest(EnvelopType envelop, byte[] messageSide1)
        {
            EnvelopKey envelop_key = new EnvelopKey(envelop);

            Console.WriteLine("clientSendRequest 1" + " / " + envelop_key);

            byte[] messageSide2 = null;
            Monitor.Enter(sync);
            try
            {
                // envia a requisição para o root parceiro
                int facet = envelop.Item3;
                Console.WriteLine("clientSendRequest send to facet " + facet + " - nofsockets=" + client_socket_facet.Count + " / " + envelop_key);
                Socket socket = client_socket_facet [facet];
                byte[] messageSide1_enveloped_raw = ObjectToByteArray(new Tuple <EnvelopType, byte[]> (envelop, messageSide1));
                Int32  length = messageSide1_enveloped_raw.Length;
                byte[] messageSide1_enveloped_raw_ = new byte[4 + length];
                BitConverter.GetBytes(length).CopyTo(messageSide1_enveloped_raw_, 0);
                Array.Copy(messageSide1_enveloped_raw, 0, messageSide1_enveloped_raw_, 4, length);

                socket.Send(messageSide1_enveloped_raw_);

                Console.WriteLine("clientSendRequest 2 nbytes=" + messageSide1_enveloped_raw.Length + " / " + envelop_key);

                // Verifica se já há resposta para a requisição no "conjunto de respostas pendentes de requisição"
                if (!reply_pending_list.ContainsKey(envelop_key) ||
                    (reply_pending_list.ContainsKey(envelop_key) && reply_pending_list [envelop_key].Count == 0))
                {
                    Console.WriteLine("clientSendRequest 3 - BEFORE WAIT " + envelop_key);
                    // Se não houver, coloca um item no "conjunto de requisições pendentes de resposta" e espera.

                    if (!request_pending_list.ContainsKey(envelop_key))
                    {
                        request_pending_list [envelop_key] = new AutoResetEvent(false);
                    }

                    AutoResetEvent sync_send = request_pending_list [envelop_key];

                    request_pending_list [envelop_key] = sync_send;
                    Monitor.Exit(sync);
                    sync_send.WaitOne();
                    Monitor.Enter(sync);
                    Console.WriteLine("clientSendRequest 3 - AFTER WAIT " + envelop_key);
                }
                Console.WriteLine("clientSendRequest 4" + " / " + envelop_key);

                Queue <byte[]> pending_replies = reply_pending_list [envelop_key];
                Console.WriteLine("clientSendRequest 5 -- pending_replies.Count = " + pending_replies.Count);
                if (pending_replies.Count > 0)
                {
                    messageSide2 = reply_pending_list[envelop_key].Dequeue();
                }

                //reply_pending_list.Remove(envelop_key);
            }
            finally
            {
                Monitor.Exit(sync);
            }

            Console.WriteLine("clientSendRequest 5");
            // retorna a menagem ...
            return(messageSide2);
        }