| 
									
										
										
										
											2014-10-23 16:57:54 +01:00
										 |  |  | package p2p | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-13 23:54:34 +01:00
										 |  |  | import "fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | // Protocol represents a P2P subprotocol implementation. | 
					
						
							|  |  |  | type Protocol struct { | 
					
						
							|  |  |  | 	// Name should contain the official protocol name, | 
					
						
							|  |  |  | 	// often a three-letter word. | 
					
						
							|  |  |  | 	Name string | 
					
						
							| 
									
										
										
										
											2014-11-04 13:21:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | 	// Version should contain the version number of the protocol. | 
					
						
							|  |  |  | 	Version uint | 
					
						
							| 
									
										
										
										
											2014-11-04 13:21:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | 	// Length should contain the number of message codes used | 
					
						
							|  |  |  | 	// by the protocol. | 
					
						
							|  |  |  | 	Length uint64 | 
					
						
							| 
									
										
										
										
											2014-11-04 13:21:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | 	// Run is called in a new groutine when the protocol has been | 
					
						
							|  |  |  | 	// negotiated with a peer. It should read and write messages from | 
					
						
							|  |  |  | 	// rw. The Payload for each message must be fully consumed. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// The peer connection is closed when Start returns. It should return | 
					
						
							|  |  |  | 	// any protocol-level error (such as an I/O error) that is | 
					
						
							|  |  |  | 	// encountered. | 
					
						
							|  |  |  | 	Run func(peer *Peer, rw MsgReadWriter) error | 
					
						
							| 
									
										
										
										
											2014-11-04 13:21:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | func (p Protocol) cap() Cap { | 
					
						
							|  |  |  | 	return Cap{p.Name, p.Version} | 
					
						
							| 
									
										
										
										
											2014-10-23 16:57:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | // Cap is the structure of a peer capability. | 
					
						
							|  |  |  | type Cap struct { | 
					
						
							|  |  |  | 	Name    string | 
					
						
							|  |  |  | 	Version uint | 
					
						
							| 
									
										
										
										
											2014-10-23 16:57:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | func (cap Cap) RlpData() interface{} { | 
					
						
							|  |  |  | 	return []interface{}{cap.Name, cap.Version} | 
					
						
							| 
									
										
										
										
											2014-10-23 16:57:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-13 23:54:34 +01:00
										 |  |  | func (cap Cap) String() string { | 
					
						
							|  |  |  | 	return fmt.Sprintf("%s/%d", cap.Name, cap.Version) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:48:49 +01:00
										 |  |  | type capsByName []Cap | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (cs capsByName) Len() int           { return len(cs) } | 
					
						
							|  |  |  | func (cs capsByName) Less(i, j int) bool { return cs[i].Name < cs[j].Name } | 
					
						
							|  |  |  | func (cs capsByName) Swap(i, j int)      { cs[i], cs[j] = cs[j], cs[i] } |