Exemplo n.º 1
0
        private void serverMessageHandler(object networkDataStream)
        {
            Queue <Payload> payloadQueue  = new Queue <Payload>();
            NetworkStream   networkStream = (NetworkStream)networkDataStream;

            try
            {
                while (true)
                {
                    if (networkStream == null)
                    {
                        var e = DisconnectEvent;
                        DisconnectEventArgs args = new DisconnectEventArgs();
                        if (e != null)
                        {
                            e.Invoke(args);
                        }
                        break;
                    }
                    while (networkStream.DataAvailable)
                    {
                        byte[] buffer    = new byte[commUtil.payloadSize];
                        int    readBytes = 0;
                        readBytes = networkStream.Read(buffer, 0, commUtil.payloadSize);
                        Payload output = null;
                        if (readBytes == commUtil.payloadSize)
                        {
                            string serializedData = Encoding.UTF8.GetString(buffer);
                            output = commUtil.preparePayload(serializedData);
                            payloadQueue.Enqueue(output);
                        }
                        if (output != null && output.command != CommandType.STATUS)
                        {
                            Debug.Print("Receiving " + Enum.GetName(typeof(CommandType), output.command));
                        }
                    }
                    while (payloadQueue.Count != 0)
                    {
                        Payload payload = null;
                        if (payloadQueue != null && payloadQueue.Count > 0)
                        {
                            payload = payloadQueue.Dequeue();
                        }

                        if (payload.command != CommandType.STATUS)
                        {
                            Debug.Print("Printing Client " + payload.command);
                        }
                        switch (payload.command)
                        {
                        case CommandType.DOWNLOAD:
                        case CommandType.APPEND_ASSEMBLY:
                            commUtil.StoreAssembly(payload.assemblyName, payload.assemblyBytes, payload.isAppend, payload.remainingPayloads);
                            break;

                        case CommandType.EXECUTE:
                        case CommandType.APPEND_EXECUTE:
                            executeTask(payload);
                            break;

                        case CommandType.RESULT:
                        case CommandType.APPEND_RESULT:
                            commUtil.storeResult(payload.assemblyName, payload.runId, payload.jsonOutput, payload.isAppend, payload.remainingPayloads);
                            break;

                        case CommandType.CLIENT_REGISTRTION:
                            setClientID(payload);
                            break;
                        }
                    }
                    Thread.Sleep(150);
                }
            }
            catch (ObjectDisposedException)
            {
                var e = DisconnectEvent;
                DisconnectEventArgs args = new DisconnectEventArgs();
                if (e != null)
                {
                    e.Invoke(args);
                }
            }
        }
Exemplo n.º 2
0
        private void processClient(object clientInfo)
        {
            TcpClient        client        = (TcpClient)clientInfo;
            NetworkStream    networkStream = client.GetStream();
            clientStatistics stats         = new clientStatistics(clientList);
            Queue <Payload>  payloadQueue  = new Queue <Payload>();
            string           ClientID      = "Not Registered";

            lock (clientList)
            {
                clientList.Add(stats);
                clientList.Sort();
            }

            while (true)
            {
                if (!client.Connected)
                {
                    var e = DisconnectEvent;
                    DisconnectEventArgs args = new DisconnectEventArgs();
                    args.ClientId = ClientID;
                    if (e != null)
                    {
                        e.Invoke(args);
                    }
                    break;
                }
                while (networkStream.DataAvailable)
                {
                    byte[] buffer    = new byte[commUtil.payloadSize];
                    int    readBytes = 0;
                    readBytes = networkStream.Read(buffer, 0, commUtil.payloadSize);
                    Payload output = null;
                    if (readBytes == commUtil.payloadSize)
                    {
                        string serializedData = Encoding.UTF8.GetString(buffer);
                        output = commUtil.preparePayload(serializedData);
                        payloadQueue.Enqueue(output);
                    }
                    if (output != null && output.command != CommandType.STATUS)
                    {
                        Debug.Print("Receiving " + Enum.GetName(typeof(CommandType), output.command));
                    }
                }
                while (payloadQueue.Count != 0)
                {
                    Payload payload = null;
                    if (payloadQueue != null && payloadQueue.Count > 0)
                    {
                        payload = payloadQueue.Dequeue();
                    }

                    switch (payload.command)
                    {
                    case CommandType.REQUEST:
                    case CommandType.APPEND_REQUEST:
                        sendToExecuteQueue(payload);
                        break;

                    case CommandType.RESULT:
                    case CommandType.APPEND_RESULT:
                        sendToResultQueue(payload);
                        break;

                    case CommandType.REGISTER_CLIENT:
                        ClientID = doRegisterClient(payload, networkStream);
                        break;

                    case CommandType.REGISTER_ASSEMBLY:
                        doRegisterAssembly(payload, networkStream);
                        break;

                    case CommandType.UPLOAD_ASSEMBLY:
                    case CommandType.APPEND_ASSEMBLY:
                        doSaveAssembly(payload);
                        break;

                    case CommandType.DOWNLOAD:
                        doSendAssembly(payload, networkStream);
                        break;

                    case CommandType.DOWNLOAD_EXECUTE:
                        doExecuteAfterSend(payload, networkStream);
                        break;

                    case CommandType.STATUS:
                        updateStatus(payload, stats);
                        break;
                    }
                }
                //Console.WriteLine(payload.clientId + " " + payload.command + " " + payload.cpuUsage + " " + payload.memUsage);
                Thread.Sleep(150);
            }
        }