Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "chronos-invitation.h" |
| 12 | #include "exception.h" |
| 13 | #include "logging.h" |
| 14 | |
| 15 | using namespace std; |
| 16 | using namespace ndn; |
| 17 | |
| 18 | INIT_LOGGER("ChronosInvitation"); |
| 19 | |
| 20 | ChronosInvitation::ChronosInvitation(const ndn::Name& interestName) |
| 21 | : m_interestName(interestName) |
| 22 | { |
| 23 | if(interestName.get(0).toUri() != string("ndn") |
| 24 | || interestName.get(1).toUri() != string("broadcast") |
| 25 | || interestName.get(2).toUri() != string("chronos") |
| 26 | || interestName.get(3).toUri() != string("invitation")) |
| 27 | throw LnException("Wrong ChronosInvitation Name"); |
| 28 | |
| 29 | int i = 4; |
| 30 | int size = interestName.size(); |
| 31 | |
| 32 | string chatroomStr("chatroom"); |
| 33 | int inviteeBegin = 4; |
| 34 | for(; i < size; i++) |
| 35 | if(interestName.get(i).toUri() == chatroomStr) |
| 36 | break; |
| 37 | |
| 38 | if(i >= size) |
| 39 | throw LnException("Wrong ChronosInvitation Name, No chatroom tag"); |
| 40 | m_inviteeNameSpace = interestName.getSubName(inviteeBegin, i - inviteeBegin); |
| 41 | |
| 42 | _LOG_DEBUG(m_inviteeNameSpace.toUri()); |
| 43 | |
| 44 | string inviterPrefixStr("inviter-prefix"); |
| 45 | int chatroomBegin = (++i); |
| 46 | for(; i < size; i++) |
| 47 | if(interestName.get(i).toUri() == inviterPrefixStr) |
| 48 | break; |
| 49 | |
| 50 | if(i > size) |
| 51 | throw LnException("Wrong ChronosInvitation Name, No inviter-prefix tag"); |
| 52 | m_chatroom = interestName.getSubName(chatroomBegin, i - chatroomBegin); |
| 53 | |
| 54 | _LOG_DEBUG(m_chatroom.toUri()); |
| 55 | |
| 56 | string inviterStr("inviter"); |
| 57 | int inviterPrefixBegin = (++i); |
| 58 | for(; i < size; i++) |
| 59 | if(interestName.get(i).toUri() == inviterStr) |
| 60 | break; |
| 61 | |
| 62 | if(i > size) |
| 63 | throw LnException("Wrong ChronosInvitation Name, No inviter tag"); |
| 64 | m_inviterPrefix = interestName.getSubName(inviterPrefixBegin, i - inviterPrefixBegin); |
| 65 | |
| 66 | _LOG_DEBUG(m_inviterPrefix.toUri()); |
| 67 | |
| 68 | int inviterCertBegin = (++i); |
| 69 | m_inviterCertificateName = interestName.getSubName(inviterCertBegin, size - 1 - inviterCertBegin); |
| 70 | |
| 71 | _LOG_DEBUG(m_inviterCertificateName.toUri()); |
| 72 | |
| 73 | string signature = interestName.get(-1).toBlob(); |
| 74 | m_signatureBits.insert(m_signatureBits.end(), signature.begin(), signature.end()); |
| 75 | |
| 76 | string keyStr("KEY"); |
| 77 | int keyId = 0; |
| 78 | for(; keyId < m_inviterCertificateName.size(); keyId++) |
| 79 | if(m_inviterCertificateName.get(keyId).toUri() == keyStr) |
| 80 | break; |
| 81 | if(keyId >= m_inviterCertificateName.size()) |
| 82 | throw LnException("Wrong ChronosInvitation Name, no KEY tag in inviter Certificate Name"); |
| 83 | m_inviterNameSpace = m_inviterCertificateName.getSubName(0, keyId); |
| 84 | |
| 85 | string signedName = interestName.getSubName(0, size - 1).toUri(); |
| 86 | m_signedBlob.insert(m_signedBlob.end(), signedName.begin(), signedName.end()); |
| 87 | } |
| 88 | |
| 89 | ChronosInvitation::ChronosInvitation(const ChronosInvitation& invitation) |
| 90 | : m_interestName(invitation.m_interestName) |
| 91 | , m_inviteeNameSpace(invitation.m_inviteeNameSpace) |
| 92 | , m_chatroom(invitation.m_chatroom) |
| 93 | , m_inviterPrefix(invitation.m_inviterPrefix) |
| 94 | , m_inviterCertificateName(invitation.m_inviterCertificateName) |
| 95 | , m_signatureBits(invitation.m_signatureBits) |
| 96 | , m_inviterNameSpace(invitation.m_inviterNameSpace) |
| 97 | , m_signedBlob(invitation.m_signedBlob) |
| 98 | {} |