| 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | package upid |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "net" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | // UPID is a equivalent of the UPID in libprocess. |
| 28 | type UPID struct { |
| 29 | ID string |
| 30 | Host string |
| 31 | Port string |
| 32 | } |
| 33 | |
| 34 | // Parse parses the UPID from the input string. |
| 35 | func Parse(input string) (*UPID, error) { |
| 36 | upid := new(UPID) |
| 37 | |
| 38 | splits := strings.Split(input, "@") |
| 39 | if len(splits) != 2 { |
| 40 | return nil, fmt.Errorf("Expect one `@' in the input") |
| 41 | } |
| 42 | upid.ID = splits[0] |
| 43 | |
| 44 | if _, err := net.ResolveTCPAddr("tcp4", splits[1]); err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | upid.Host, upid.Port, _ = net.SplitHostPort(splits[1]) |
| 48 | return upid, nil |
| 49 | } |
| 50 | |
| 51 | // String returns the string representation. |
| 52 | func (u UPID) String() string { |
| 53 | return fmt.Sprintf("%s@%s:%s", u.ID, u.Host, u.Port) |
| 54 | } |
| 55 | |
| 56 | // Equal returns true if two upid is equal |
| 57 | func (u *UPID) Equal(upid *UPID) bool { |
| 58 | if u == nil { |
| 59 | return upid == nil |
| 60 | } else { |
| 61 | return upid != nil && u.ID == upid.ID && u.Host == upid.Host && u.Port == upid.Port |
| 62 | } |
| 63 | } |