public override int handleMsg(FcpMessage msg) { Debug.Assert((msg.Type == FcpType.Publish) || msg.Type == FcpType.ExtPublish); //Logger->debug("exec from:{} to:{}", msg.src_uri().c_str(), msg.dst_uri().c_str()); p(msg.Data); return(0); }
protected int handlePublish(FcpMessage fcp) { Debug.Assert(fcp.Type == FcpType.Publish); bool find = false; string uri = fcp.DstUri; foreach (var t in m_table) { var path = m_path.abs_uri(t.Item1); if (Path.inPath(uri, path)) { fcp.Direction = 0; Logger.DebugFormat("%s/%d>>>%s", m_deal, fcp.Type, t.Item1); t.Item2.handleMsg(fcp); find = true; } } if (find) { return(1); } else { return(0); } }
public void publish <T>(string uri, T data) where T : IMessage { FcpMessage fcp = new FcpMessage(); fcp.DstUri = (m_path.abs_uri(uri)); fcp.SrcUri = (m_path.abs_uri()); fcp.Type = (FcpType.Publish); fcp.Data = Encoding.ASCII.GetString(data.ToByteArray()); relType rel = m_path.relation(uri); //if protected unsafe List<Tuple<string, Node_*>> Table { get => table; set => table = value; } if (rel == relType.child) { fcp.Direction = (0); handleMsg(fcp); } else { fcp.Direction = (1); sendMsg(fcp); } Logger.DebugFormat("publishEx -----------------------"); fcp.DstUri = (m_path.abs_uri(uri)); fcp.Direction = (1); fcp.Type = (FcpType.ExtPublish); //额外发送一个消息给master,由master转发给订阅者 sendMsg(fcp); Logger.DebugFormat("publish end-----------------------"); }
public void addSubscribe(string dst_uri, Node_ node) { Logger.DebugFormat("-----------------\nSubscribe from:%s to: %s\n", m_deal, dst_uri); int n = -1; foreach (var t in m_table) { if (Path.belong(t.Item1, "subscriber")) { string number = t.Item1.Split(':')[1]; int num = Int32.Parse(number); n = Math.Max(num, n); } } string call_uri = "subscriber:" + (n + 1); FcpMessage msg = new FcpMessage(); msg.DstUri = (m_path.abs_uri(dst_uri)); msg.SrcUri = (m_path.abs_uri(call_uri)); msg.Type = (FcpType.Subscribe); //msg.Data(""); sendMsg(msg); m_table.Add(new Tuple <string, Node_>(call_uri, node)); }
public override int sendMsg(FcpMessage msg) { Logger.DebugFormat("%s/%d>>>", m_deal, msg.Type); Debug.Assert(msg.Direction == 0); var data = msg.ToByteArray(); String t = data.Length + ":" + data; return(Tx(Encoding.ASCII.GetBytes(t))); }
public override int handleMsg(FcpMessage msg) { Debug.Assert((msg.Type == FcpType.Publish) || msg.Type == FcpType.ExtPublish); T t = Parser.ParseFrom(Encoding.ASCII.GetBytes(msg.Data)); //Logger->debug("exec from:{} to:{}", msg.src_uri().c_str(), msg.dst_uri().c_str()); p(t); return(0); }
protected int handleLocal(FcpMessage fcp) { if (FcpType.Publish == fcp.Type) { return(handlePublish(fcp)); } return(-1); }
public override int sendMsg(FcpMessage msg) { Logger.DebugFormat("<<<%s/%s", m_deal, msg.Type); System.Diagnostics.Debug.Assert(msg.Direction == 1); var output = new MemoryStream(); msg.WriteTo(output); var data = output.ToArray(); String t = data.Length + ":" + Encoding.ASCII.GetString(data); return(Tx(Encoding.ASCII.GetBytes(t))); }
static void Main(string[] args) { Path a = new Path("/"); Console.WriteLine(new Path("/asfd:2/a/b").abs_uri("../c")); Console.WriteLine(new Path("/asfd:2/a/b").abs_uri("./sf/d")); Console.WriteLine(Path.inPath("/a:0/b:2/c", "/a:0/b:2")); FcpMessage fcp = new FcpMessage(); TcpDownNode tcp = new TcpDownNode(); tcp.SetPath("/tcp:0"); //tcp.addNode("/tcp:0", new FunctionalNode<LightMessage>( // (LightMessage l) => // { // var setting = new JsonFormatter.Settings(true); // JsonFormatter json=new JsonFormatter(setting); // Console.WriteLine(json.Format(l)); // })); tcp.addNode("asr", new StringFunctionalNode((String data) => { Console.WriteLine(data); })); tcp.Connect("127.0.0.1:1212"); while (true) { try { tcp.Recv(); } catch { break; } } //fcp.Direction = 1; //fcp.DstUri = "/a/b"; //using (var output = new MemoryStream()) //{ // fcp.WriteTo(output); // var t = fcp.ToByteString(); // var data = output.ToArray(); // var str = Encoding.ASCII.GetString(fcp.ToByteArray()); // var fcp1 = FcpMessage.Parser.ParseFrom(Encoding.ASCII.GetBytes(str)); // Console.WriteLine(fcp1.GetHashCode() == fcp.GetHashCode()); //} }
public override int handleMsg(FcpMessage msg) { if (msg.Direction == 0) { sendMsg(msg); } //下节点无法处理的消息,转交网关 else { if (m_gateway != null) { m_gateway.handleMsg(msg); } } return(0); }
public int Rx(byte[] data) { if (m_buffer.Length == 0 && msg_size == 0) { for (int i = 0; i < data.Length; i++) { if (data[i] == ':') { msg_size = int.Parse(Encoding.ASCII.GetString(m_buffer.ToArray())); m_buffer = new MemoryStream(m_buffer.Capacity); var new_data = new byte[data.Length - i - 1]; Array.Copy(data, i + 1, new_data, 0, data.Length - i - 1); return(Rx(new_data)); } else { m_buffer.WriteByte(data[i]); } } return(0); } else { for (int i = 0; i < data.Length; i++) { m_buffer.WriteByte(data[i]); if (m_buffer.Length == msg_size) { FcpMessage msg = FcpMessage.Parser.ParseFrom(m_buffer.ToArray()); handleMsg(msg); m_buffer = new MemoryStream(m_buffer.Capacity); msg_size = 0; if (data.Length - i - 1 > 0) { var new_data = new byte[data.Length - i - 1]; Array.Copy(data, i + 1, new_data, 0, data.Length - i - 1); return(1 + Rx(new_data)); } else { return(1); } } } return(0); } }
public override int handleMsg(FcpMessage fcp) { string uri = fcp.DstUri; relType rel = m_path.relation(uri); if (rel == relType.parent) { sendMsg(fcp); } else if (rel == relType.self) { handleLocal(fcp); } //与自己与父亲有关 else if (rel == relType.brother) { //handleLocal(fcp); if (fcp.Direction == 1) { sendMsg(fcp); } else { handleLocal(fcp); } } //与自己无关,只与孩子有关 else if (rel == relType.child) { FcpMessage t_fcp = fcp; t_fcp.Direction = 0; handleLocal(t_fcp); } return(0); }
public abstract int handleMsg(FcpMessage msg);
public abstract int sendMsg(FcpMessage msg);