Skip to content

Commit

Permalink
Run-time parameter for client connection port.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Aug 26, 2021
1 parent f76f7a4 commit 72c2d4b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Compiler/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1650,22 +1650,22 @@ class listen(base.IOInstruction):
""" Open a server socket on a party-specific port number and listen for
client connections (non-blocking).
:param: port number (int)
:param: port number (regint)
"""
__slots__ = []
code = base.opcodes['LISTEN']
arg_format = ['int']
arg_format = ['ci']

class acceptclientconnection(base.IOInstruction):
""" Wait for a connection at the given port and write socket handle
to clear integer register.
:param: client id destination (regint)
:param: port number (int)
:param: port number (regint)
"""
__slots__ = []
code = base.opcodes['ACCEPTCLIENTCONNECTION']
arg_format = ['ciw', 'int']
arg_format = ['ciw', 'ci']

class closeclientconnection(base.IOInstruction):
""" Close connection to client.
Expand Down
10 changes: 10 additions & 0 deletions Compiler/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,16 @@ def get_player_id():
playerid(res._v)
return res

def listen_for_clients(port):
""" Listen for clients on specific port. """
instructions.listen(regint.conv(port))

def accept_client_connection(port):
""" Listen for clients on specific port. """
res = regint()
instructions.acceptclientconnection(res, regint.conv(port))
return res

def break_point(name=''):
"""
Insert break point. This makes sure that all following code
Expand Down
2 changes: 1 addition & 1 deletion Processor/ExternalClients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void ExternalClients::start_listening(int portnum_base)
client_connection_servers[portnum_base] = new AnonymousServerSocket(portnum_base + get_party_num());
client_connection_servers[portnum_base]->init();
cerr << "Start listening on thread " << this_thread::get_id() << endl;
cerr << "Party " << get_party_num() << " is listening on port " << (portnum_base + get_party_num())
cerr << "Party " << get_party_num() << " is listening on port " << (portnum_base + get_party_num())
<< " for external client connections." << endl;
}

Expand Down
10 changes: 6 additions & 4 deletions Processor/Instruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case RAND:
case DABIT:
case SHUFFLE:
case ACCEPTCLIENTCONNECTION:
r[0]=get_int(s);
r[1]=get_int(s);
break;
Expand All @@ -154,6 +155,7 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case NPLAYERS:
case THRESHOLD:
case PLAYERID:
case LISTEN:
case CLOSECLIENTCONNECTION:
r[0]=get_int(s);
break;
Expand Down Expand Up @@ -234,7 +236,6 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case LDINT:
case INPUTMASK:
case GINPUTMASK:
case ACCEPTCLIENTCONNECTION:
case INV2M:
case CONDPRINTSTR:
case CONDPRINTSTRB:
Expand All @@ -248,7 +249,6 @@ void BaseInstruction::parse_operands(istream& s, int pos, int file_pos)
case JMP:
case START:
case STOP:
case LISTEN:
case PRINTFLOATPREC:
n = get_int(s);
break;
Expand Down Expand Up @@ -545,6 +545,7 @@ int BaseInstruction::get_reg_type() const
case CONVCBIT:
case CONVCBITVEC:
case INTOUTPUT:
case ACCEPTCLIENTCONNECTION:
return INT;
case PREP:
case GPREP:
Expand Down Expand Up @@ -1137,12 +1138,13 @@ inline void Instruction::execute(Processor<sint, sgf2n>& Proc) const
// ***
case LISTEN:
// listen for connections at port number n
Proc.external_clients.start_listening(n);
Proc.external_clients.start_listening(Proc.read_Ci(r[0]));
break;
case ACCEPTCLIENTCONNECTION:
{
// get client connection at port number n + my_num())
int client_handle = Proc.external_clients.get_client_connection(n);
int client_handle = Proc.external_clients.get_client_connection(
Proc.read_Ci(r[1]));
if (Proc.P.my_num() == 0)
{
octetStream os;
Expand Down
6 changes: 2 additions & 4 deletions Programs/Source/bankers_bonus.mpc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""

from Compiler.types import sint, regint, Array, MemValue
from Compiler.instructions import listen, acceptclientconnection
from Compiler.library import print_ln, do_while, for_range
from Compiler.util import if_else

Expand All @@ -26,8 +25,7 @@ if len(program.args) > 1:
n_rounds = int(program.args[1])

def accept_client():
client_socket_id = regint()
acceptclientconnection(client_socket_id, PORTNUM)
client_socket_id = accept_client_connection(PORTNUM)
last = regint.read_from_socket(client_socket_id)
return client_socket_id, last

Expand Down Expand Up @@ -81,7 +79,7 @@ def main():
"""Listen in while loop for players to join a game.
Once maxiumum reached or have notified that round finished, run comparison and return result."""
# Start listening for client socket connections
listen(PORTNUM)
listen_for_clients(PORTNUM)
print_ln('Listening for client connections on base port %s', PORTNUM)

def game_loop(_=None):
Expand Down

0 comments on commit 72c2d4b

Please sign in to comment.