private void innerInvokeHandler(object key, PeerSocket source, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { if (handlers.ContainsKey(key)) { try { handlers[key](source, msgCode, msgId, senderId, receiverId, data); } catch (Exception exc) { source.SendException(msgCode, msgId, exc); } } }
public PeerManager(string parentId,PeerLogDelegate logMethod ) { //we may receive connect request by sockets other than gateway, for example //when a adminConsole blockweb tries to order remote BW to connect to a third BW responseHandlers.SetHandler(PeerMsgDef.ConnectMsgCode, new MessageHandlerDelegate(onConnectRequestReceived)); responseHandlers.SetHandler(PeerMsgDef.DisconnectMsgCode, new MessageHandlerDelegate(onDisconnectReceived)); responseHandlers.SetHandler(PeerMsgDef.HeartbeatMsgCode, new MessageHandlerDelegate(onHeartbeatReceived)); gatewaySocket = new PeerSocket(responseHandlers, parentId, null); heartbeatThread = new Thread(new ThreadStart(heartbeatSender)); heartbeatThread.Start(); this.logMethod = logMethod; logEvent("Create PM(" + parentId + ") - Gateway Address = " + this.Address); }
private void onHeartbeatReceived(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { //renew expire date of peer peersExpireDates[senderId] = DateTime.Now.AddMilliseconds(PeerMsgDef.HeartbeatValidness); logEvent(-2, gatewaySocket.ParentId + " got heartbeat from " + senderId); }
private void onDisconnectReceived(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { //if the sender is my gateway ignore this, as temp sockets will always disconnect from it ASAP if (sender == gatewaySocket) return; //dispose and remove my socket sender.Dispose(); peers.Remove(senderId); peersExpireDates.Remove(senderId); if (PeerDisconnected != null) { //upon disconnect, we know host and port of peer PeerDisconnected(senderId, false, null, -1); } }
private void onConnectRequestReceived(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { //step 1 - read the other-side address string otherHost = (string)data[0]; int otherPort = (int)data[1]; //we use these to store info string otherGatewayHost = (string) data[2]; int otherGatewayPort = (int)data[3]; logEvent("PM (" + gatewaySocket.ParentId + ") - Receive Connect Request - PeerAddress = " + otherHost+":"+otherPort.ToString() + ", PeerId = " + senderId); //step 2 - create a client peer socket and connect to that address PeerSocket myClientSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, senderId, otherHost, otherPort); //step 3 - check if client is connected successfully if (!myClientSocket.IsConnected || (myClientSocket.PeerHost != otherHost) || (myClientSocket.PeerPort != otherPort)) { myClientSocket.Dispose(); myClientSocket = null; return; } //step 4 - add this client socket to peers peers.Add(senderId, myClientSocket); peersExpireDates[senderId] = DateTime.Now.AddMilliseconds(PeerMsgDef.HeartbeatValidness); //step 5 - let sender know that I am connected sender.SendMessageAsync(msgCode, msgId, true); if (PeerConnected != null) { PeerConnected(senderId, true, otherGatewayHost, otherGatewayPort ); } }
public void Dispose() { Disconnect(); gatewaySocket.Dispose(); gatewaySocket = null; heartbeatThread.Abort(); }
public bool Connect(string host, int port, string peerId) { if (gatewaySocket == null) return false; //step 1 - create a server peerSocket PeerSocket myServerSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, peerId); //step 2 - create a temp client peer socket to send my address to the peer's gateway PeerSocket tempClientSocket = new PeerSocket(responseHandlers, gatewaySocket.ParentId, peerId, host, port); //step 3 - use temp client socket to send my server socket address to peer List<object> response = null; try { response = tempClientSocket.SendMessage(PeerMsgDef.ConnectMsgCode, myServerSocket.Host, myServerSocket.Port, gatewaySocket.Host, gatewaySocket.Port); } catch (Exception exc) { myServerSocket.Dispose(); myServerSocket = null; throw; } finally { //step 4 - dispose temp socket tempClientSocket.Dispose(); tempClientSocket = null; } //step 5 - was connect successfull? if (response == null) { myServerSocket.Dispose(); myServerSocket = null; return false; } //step 6 - extract address that peer has prepared for me (peer will connect to my server socket automatically) bool done = (bool)response[0]; //step 7 - check correctness of the address if (!myServerSocket.IsConnected || !done) { myServerSocket.Dispose(); myServerSocket = null; return false; } //step 8 - store peer socket peers.Add(peerId, myServerSocket); peersExpireDates[peerId] = DateTime.Now.AddMilliseconds(PeerMsgDef.HeartbeatValidness); logEvent("PM (" + gatewaySocket.ParentId + ") Connecting- PeerAddress = " + myServerSocket.PeerAddress + " PeerId = " + peerId); return true; }
public void InvokeHandler(Guid targetMsgId, PeerSocket source, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { innerInvokeHandler(targetMsgId, source, msgCode, msgId, senderId, receiverId, data); }
private void onCallWebMethodRequest(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { object result = null; string methodName = data[0].ToString(); logEvent(LogType.PeerRequest, "Peer " + senderId + " sent request: " + msgCode); logEvent(LogType.BlockWeb, "onCallMethodRequest - Name = " + methodName); try { //others are arguments switch (methodName) { case "AddBlock": result = AddBlock((BlockHandle)data[1], (string)data[2]); break; case "GetBlockId": result = getBlockId((string)data[1]); break; case "DeleteBlock": DeleteBlock((string)data[1]); break; case "ReloadBlocks": ReloadBlocks(); break; case "Disconnect": Disconnect((string)data[1]); break; case "Dispose": Dispose(); return; case "GetBlockWebMetaInfo": result = GetBlockWebMetaInfo((BlockWebMetaInfoType)data[1], (string)data[2]); break; case "Connect": result = Connect((string)data[1], (int)data[2], (string)data[3]); break; case "BlockHandles": result = BlockIds; break; case "BlockCount": result = BlockCount; break; case "MigrateBlock": { //create given block and send migrate-done string id = (string)data[0]; BlockHandle blockId = (BlockHandle)data[1]; object customData = data[2]; string response = AddBlock(blockId); innerBlocks[response].OnAfterMigration(customData); result = response; break; } } } catch (System.Exception exc) { logEvent(LogType.Exception, "Error Handling Call on Web Methods (" + methodName + "): " + exc.Message); throw; } sender.SendMessageAsync(msgCode, msgId, result); }
private void onCallBlockMethodRequest(PeerSocket sender, string msgCode, Guid msgId, string senderId, string receiverId, List<object> data) { object result = null; string blockId = (string)data[0]; string methodName = data[1].ToString(); string serviceName = ""; //object[] args = (object[])data[2]; BlockHandle info = getBlockId(blockId); logEvent(LogType.PeerRequest, "Peer " + senderId + " sent request: " + msgCode); logEvent(LogType.ProcessRequest, "onCallBlockMethodRequest - Name = " + methodName + " ID=" + info.ToString()); try { //others are arguments switch (methodName) { case "ProcessRequest": { serviceName = data[2].ToString(); object[] args = (object[])data[3]; logEvent(LogType.ProcessRequest, "onCallBlockMethodRequest - Name = " + methodName + " ID=" + info.ToString() + " service=" + serviceName); result = this[blockId].ProcessRequest(serviceName, args); break; } } } catch (System.Exception exc) { logEvent(LogType.Exception,"Error Handling Call on Block Service (" + methodName + ","+serviceName+"): " + exc.Message); throw; } sender.SendMessageAsync(msgCode, msgId, result); }