public class ServerConnection extends Object
ServerConnection
object which can then be used to connect to the PC2 server
via the login(String, String)
method. The PC2 server must already be running, and the local client must have a pc2v9.ini
file specifying valid
server connection information, prior to invoking login(String, String)
method.
This documentation describes the current draft of the PC2 API, which is subject to change.
Modifier and Type | Class and Description |
---|---|
protected class |
ServerConnection.ConnectionEventListener
A Connection Event used by ServerConnection.
|
Modifier and Type | Field and Description |
---|---|
protected edu.csus.ecs.pc2.core.IInternalController |
controller |
protected edu.csus.ecs.pc2.core.model.IInternalContest |
internalContest |
Constructor and Description |
---|
ServerConnection()
Construct a local
ServerConnection object which can subsequently be used to connect to a currently-running PC2 server. |
Modifier and Type | Method and Description |
---|---|
void |
addAccount(String accountTypeName,
String displayName,
String password)
Add a single account (admin feature).
|
void |
addLanguage(String autoFillTitleName)
Add a language.
|
void |
addLanguage(String title,
String compilerCommandLine,
String executionCommandLine,
boolean interpreted,
String executableMask)
Add a language to the contest.
|
void |
addProblem(String title,
String shortName,
File judgesDataFile,
File judgesAnswerFile,
boolean validated,
Properties problemProperties)
Add a Problem definition.
|
String[] |
getAutoFillLanguageList() |
edu.csus.ecs.pc2.api.implementation.Contest |
getContest()
Returns a IContest, if not connected to a server throws a NotLoggedInException.
|
IClient |
getMyClient()
Returns a IClient if logged into a server.
|
String[] |
getProblemPropertyNames()
Get a list of optional Problem settings names.
|
protected String |
getProperty(Properties problemProperties,
String key,
String defaultValue)
Return value for property.
|
boolean |
isLoggedIn()
Is this ServerConnection connected to a server ?
|
boolean |
isValidAccountTypeName(String name) |
boolean |
isValidAutoFillLangauageName(String name) |
IContest |
login(String login,
String password)
Login to the PC2 server represented by this ServerConnection using the specified login account name and password.
|
boolean |
logoff()
Logoff/disconnect from the PC2 server.
|
static void |
main(String[] args) |
void |
setContestLength(long contestLengthSeconds)
Change contest length.
|
void |
setContestTimes(long contestLengthSeconds,
long contestElapsedSeconds,
long contestRemainingSeconds)
Update/set contest clock.
|
protected void |
setPC2Validator(edu.csus.ecs.pc2.core.model.Problem problem)
Define a pc2 validated problem.
|
void |
shutdownAllServers()
Shutdown all servers.
|
void |
shutdownServer()
Shutdown the server.
|
void |
startContestClock()
Start the contest clock.
|
void |
stopContestClock()
Stop the contest clock.
|
void |
submitClarification(IProblem problem,
String question)
Submit a clarification.
|
void |
submitRun(IProblem problem,
ILanguage language,
String mainFileName,
String[] additionalFileNames,
long overrideSubmissionTimeMS,
long overrideRunId)
Submit a run.
|
protected String[] |
validateProperties(Properties properties)
Check property names against valid list of names.
|
protected edu.csus.ecs.pc2.core.IInternalController controller
protected edu.csus.ecs.pc2.core.model.IInternalContest internalContest
public ServerConnection()
ServerConnection
object which can subsequently be used to connect to a currently-running PC2 server.public IContest login(String login, String password) throws LoginFailureException
IContest
object which can then be used to obtain information about the contest being controlled by the server. If the login fails the method throws
LoginFailureException
, in which case the message contained in the exception can be used to determine the nature of the login failure.
Note that invoking login(String, String)
causes an attempt to establish a network connection to a PC2 server using the connection information specified in
the pc2v9.ini
file in the current directory. The PC2 server must already be running prior to invoking login(String, String)
, and the
pc2v9.ini
must specify legitmate server connection information; otherwise, LoginFailureException
is thrown. See the PC2 Contest
Administrator's Guide for information regarding specifying server connection information in pc2v9.ini
files.
The following code snippet shows typical usage for connecting to and logging in to a PC2 server.
String login = "team4"; String password = "team4"; try { ServerConnection serverConnection = new ServerConnection(); IContest contest = serverConnection.login(login, password); // ... code here to invoke methods in "contest"; serverConnection.logoff(); } catch (LoginFailureException e) { System.out.println("Could not login because " + e.getMessage()); } catch (NotLoggedInException e) { System.out.println("Unable to execute API method"); e.printStackTrace(); }
login
- client login name (for example: "team5" or "judge3")password
- password for the login nameLoginFailureException
- if login fails, the message contained in the exception will provide and indication of the reason for the failure.public boolean isValidAccountTypeName(String name)
public void addAccount(String accountTypeName, String displayName, String password) throws Exception
For the input accountTypeName, ex. TEAM, will add the next client number account. If adding a new TEAM account and there are 22 teams, this method will add team23 login account.
The current logged in user must be an Administrator user otherwise a SecurityException will be thrown.
The following code snippet shows a example for a addAccount invocation.
String login = "administrator2"; String password = "administrator2"; try { ServerConnection serverConnection = new ServerConnection(); IContest contest = serverConnection.login(login, password); connection.addAccount("TEAM", "Team Name", "teampassword"); Thread.sleep(1000); // sleep just long enough before logging off serverConnection.logoff(); } catch (LoginFailureException e) { System.out.println("Could not login because " + e.getMessage()); } catch (NotLoggedInException e) { System.out.println("Unable to execute API method"); e.printStackTrace(); }
accountTypeName
- name of account, ex TEAMdisplayName
- title for account/team, if null will be login namepassword
- password for account, must not be null or emptystring (string length==0)Exception
public void submitClarification(IProblem problem, String question) throws Exception
problem
- question
- text of questionException
public void submitRun(IProblem problem, ILanguage language, String mainFileName, String[] additionalFileNames, long overrideSubmissionTimeMS, long overrideRunId) throws Exception
problem
- language
- mainFileName
- additionalFileNames
- overrideSubmissionTimeMS
- an override elapsed time in ms, only works if contest information CCS test mode is set true.Exception
public boolean logoff() throws NotLoggedInException
NotLoggedInException
- if attempt to logoff without being logged inpublic edu.csus.ecs.pc2.api.implementation.Contest getContest() throws NotLoggedInException
NotLoggedInException
- if attempt to invoke this method without being logged inpublic boolean isLoggedIn()
public IClient getMyClient() throws NotLoggedInException
NotLoggedInException
- if attempt to invoke this method without being logged inIContest.getMyClient()
public void startContestClock() throws Exception
Exception
public void stopContestClock() throws Exception
Exception
protected void setPC2Validator(edu.csus.ecs.pc2.core.model.Problem problem)
problem
- public void addProblem(String title, String shortName, File judgesDataFile, File judgesAnswerFile, boolean validated, Properties problemProperties)
title
- - title for problemshortName
- short name/id for problem.judgesDataFile
- - judges input data filejudgesAnswerFile
- - judges answer filevalidated
- - is the problem validated using the pc2 internal validator?problemProperties
- - optional properties, for a list of keys see getProblemPropertyNames()
, null is allowed.APIConstants
protected String getProperty(Properties problemProperties, String key, String defaultValue)
problemProperties
- key
- defaultValue
- protected String[] validateProperties(Properties properties)
properties
- public String[] getProblemPropertyNames()
APIConstants
.APIConstants
public boolean isValidAutoFillLangauageName(String name)
public String[] getAutoFillLanguageList()
public void addLanguage(String autoFillTitleName)
getAutoFillLanguageList()
.autoFillTitleName
- a automatic language fill title namepublic void addLanguage(String title, String compilerCommandLine, String executionCommandLine, boolean interpreted, String executableMask)
String login = "administrator2"; String password = "administrator2"; try { ServerConnection serverConnection = new ServerConnection(); IContest contest = serverConnection.login(login, password); String title = "Ruby"; String compilerCommandLine = "ruby -c {:mainfile}"; String executionCommandLine = "ruby {:mainfile}"; String executableMask = "{:noexe}"; boolean interpreted = true; serverConnection.addLanguage(title, compilerCommandLine, executionCommandLine, interpreted, executableMask); // may need to pause for a second or two for the language to be added. ILanguage[] languages = contest.getLanguages(); System.out.println("Added language " + languages[languages.length - 1].getName()); serverConnection.logoff(); } catch (LoginFailureException e) { System.out.println("Could not login because " + e.getMessage()); } catch (NotLoggedInException e) { System.out.println("Unable to execute API method"); e.printStackTrace(); }
title
- Display Name for the language, ex. JavacompilerCommandLine
- command to compile source codeexecutionCommandLine
- command to execute programinterpreted
- is this an interpreted language like Perl or PythonexecutableMask
- an expected output program namepublic void shutdownServer()
shutdownAllServers()
public void shutdownAllServers()
shutdownServer()
public void setContestTimes(long contestLengthSeconds, long contestElapsedSeconds, long contestRemainingSeconds) throws edu.csus.ecs.pc2.core.exception.IllegalContestState
Contest clock must be stopped to change contest length.
contestLengthSeconds
- number of seconds contest is longcontestElapsedSeconds
- number of seconds elapsed since start of contestcontestRemainingSeconds
- number of seconds until end of contestedu.csus.ecs.pc2.core.exception.IllegalContestState
- thrown if contest clock is started.IContest.isContestClockRunning()
public void setContestLength(long contestLengthSeconds) throws edu.csus.ecs.pc2.core.exception.IllegalContestState
Contest clock must be stopped to change contest length.
contestLengthSeconds
- number of seconds contest is longedu.csus.ecs.pc2.core.exception.IllegalContestState
- thrown if contest clock is started.IContest.isContestClockRunning()
public static void main(String[] args)