Protocol Machines: A Concise Formal Model and its Automatic Implementation