CS计算机代考程序代写 using System;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using JsonSerializer = System.Text.Json.JsonSerializer;

//using Newtonsoft.Json;
//***This is based on native .NET core HTTP services. Arcs and Nodes communicate via POST requests through proxies***
namespace Node
{
public class Message
{
// parameter-less constructor is required by System.Text.Json.JsonSerilizer
public Message()
{
}

public Message(int _time, int _code, int _from, int _to, int _tok, int _pay)
{
(time, code, from, to, tok, pay) = (_time, _code, _from, _to, _tok, _pay);
}

public int time { get; set; }
public int code { get; set; }
public int from { get; set; }
public int to { get; set; }
public int tok { get; set; }
public int pay { get; set; }

public readonly static int CodeReceive = 1;
public readonly static int CodeSend = 2;

public readonly static int TokForward = 1; // fan-out
public readonly static int TokReturn = 2; // fan-in
public readonly static int TokAYT = 0; // ayt
public readonly static int TokSTOP = 3; // stop
}

public class Node
{
public Node(int self, int[] neigh)
{
this.self = self;
this.neigh = neigh;
}

int self;
int[] neigh;
int parent = -1;
int rec = 0;
int size = 1;

public Action SendMessage { get; set; }
public void OnReceive(Message msgs)
{
if (msgs.code == 0) // noop for ping
return;

var clock = msgs.time;
var msgsout = Enumerable.Empty();

if (parent == -1)
{
//msgs = msgs.OrderBy (m => Array.IndexOf (neigh, m.from)) .ToArray (); // -1 from 0
//parent = msgs[0].from;
//if (parent == 0) rec = -1;

var index = Array.IndexOf(neigh, msgs.from);
if (index == -1)
{
parent = 0;
rec += -1;
}
else
{
parent = neigh[index];
}

neigh.Where(n => n != parent).Select(n => new Message(clock, Message.CodeSend, self, n, Message.TokForward, 0)).ToList().ForEach(m => SendMessage(m));
}
else
{
size += msgs.pay;
}

rec++;

if (rec >= neigh.Length)
{
SendMessage(new Message(clock, Message.CodeSend, self, parent, Message.TokReturn, size));
}
}
}

public class NodeProxy
{
private Node node;

public NodeProxy(Node node)
{
this.node = node;
this.node.SendMessage = SendMessage;
}

public void Handle(Message message)
{
this.node.OnReceive(message);
}

public async void SendMessage(Message message)
{
await Common.PostMessage(message, true);
}
}

public class Common
{
public static void Print(Message message)
{
Console.WriteLine($” {message.time,3} {(message.code == 1 ? ” ” : “”)}{message.code,3}{(message.code == 2 ? ” ” : “”)} {message.from,3} {message.to,3} {message.tok,3} {message.pay,3}”);
}

public static async Task PostMessage(Message message, bool postToArcs = false)
{
//var json = JsonConvert.SerializeObject(message);
var json = JsonSerializer.Serialize(message);
var data = new StringContent(json, Encoding.UTF8, “application/json”);

var port = postToArcs ? 8080 : message.to + 8080;
var url = $”http://localhost:{port}/”;
using var client = new HttpClient();

return await client.PostAsync(url, data);
}
}

class Program
{
static void Main(string[] args)
{
var cmd = String.Join(” “, args).Split(“timeout”);
var nodeDetail = cmd[0].Trim().Split(” “).Select(s => int.Parse(s)).ToList();
var timeout = int.Parse(cmd[1].Trim());

Node node = new Node(nodeDetail[0], nodeDetail.Skip(1).ToArray());
NodeProxy nodeProxy = new NodeProxy(node);

// Node listening at 808n
HttpListener listener = new HttpListener();
var port = nodeDetail[0] + 8080;
var address = $”http://localhost:{port}/”;
listener.Prefixes.Add(address);
listener.Start();
Console.WriteLine($”Listening at {address}…”);

while (true)
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;

string content;
using (Stream receiveStream = request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
content = readStream.ReadToEnd();
}
}
// Console.WriteLine($”Recived request for {request.Url}”);
//var message = JsonConvert.DeserializeObject(content);
var message = JsonSerializer.Deserialize(content);
Common.Print(message);

HttpListenerResponse response = context.Response;
string responseString = “OK”;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();

if (message.code == 3) // stop
{
break;
}

nodeProxy.Handle(message);
}
listener.Stop();

}
}
}