Beispiel #1
0
        private static void put_in_table(CliObj values)
        {
            DataRow newrow = table.NewRow();
            newrow[0] = values.id;
            newrow[1] = values.ip;
            newrow[2] = values.nome;
            newrow[3] = values.master;

            table.Rows.Add(newrow);
            _cli_update = true;
        }
Beispiel #2
0
 public static void addCliente(CliObj newcliente)
 {
     clientes.Add(newcliente); num_cli++; set_hasFork(hasfork.Count, true); put_in_table(newcliente);
 }
        public void clientHandler(Object obj)
        {
            TcpClient client = (TcpClient)obj;
            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.ASCII);
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.ASCII);
            Boolean isMaster = false;
            Boolean bClientConnected = true;
            String sData = null;

            while (bClientConnected)
            {
                try
                {
                    sData = sReader.ReadLine();
                    CliObj cliobj;
                    lock (this._lockThis)
                    {
                        if (DataPack._num_clientes > 0)
                        {
                            cliobj = new CliObj(DataPack._num_clientes, ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(), sData, 'C');
                        }
                        else
                        {
                            cliobj = new CliObj(DataPack._num_clientes, ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(), sData, 'M');
                            isMaster = true;
                        }
                        DataPack.addCliente(cliobj);
                    }

                    if(isMaster)
                    {
                        String str = cliobj.id.ToString() + ",M";
                        sWriter.WriteLine(str);
                        sWriter.Flush();
                    }
                    else
                    {
                        CliObj master = DataPack.getMasterClient();
                        String str = cliobj.id.ToString() + ',' + cliobj.master.ToString() + ',' + master.ip.ToString();
                        sWriter.WriteLine(str);

                        while (this._ready.Equals(false)) ;

                        sWriter.WriteLine("GO");
                        sWriter.Flush();

                        sWriter.Close();
                        sReader.Close();
                        client.Close();
                        return;
                    }

                    while (true)
                    {
                        String PedidoMaster = sReader.ReadLine();
                        if(PedidoMaster.Equals("GO"))
                        {

                            List<CliObj> mylist = DataPack._cli_list;

                            // To write to a file, create a StreamWriter object.
                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(client.GetStream(), mylist);

                            this._go = true;
                            this._server.Stop();
                            break;
                        }
                        else
                            if (PedidoMaster.Equals("NUM"))
                            {
                                sWriter.WriteLine(DataPack._num_clientes.ToString());
                                sWriter.Flush();
                            }
                    }

                    TcpChannel tcpChannel = new TcpChannel();
                    ChannelServices.RegisterChannel(tcpChannel, false);

                    Type commonInterfaceType = Type.GetType("ForkManager");

                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(ForkManager),
                        "ForkMaster", WellKnownObjectMode.SingleCall);

                    while (true)
                    {
                        TcpListener atualizador = new TcpListener(IPAddress.Any, this._update_port);
                    }

                }
                catch (System.IO.IOException e)
                {
                    Console.Write(e);
                }
            }
        }